Link to home
Start Free TrialLog in
Avatar of mak730
mak730Flag for Mexico

asked on

SHA256 in Java

I've found a JavaScript that implements SHA256, the encoded64 string that it returns is fully compatible with the result I've got with C# cryptograhpy API, so I can hash strings on web browser, send them to the server and compare against C# hashed strings.

Now I'm traying to do the same with Java but the API seems to produce a very different result. I thought that, if sha256 is a "standard" algoritm, results has to be the same.

What can be happened? Are there any JavaScript "compatible" with Java API?

I'm using MessageDigest.getInstance("SHA-256") to do the hashing

Thanks
Avatar of doraemon-nolife
doraemon-nolife

Could you give more details on your code ?

Right now, i assume that you should do the followings:

...

hash = MessageDigest.getInstance("SHA-256");
...
// Calculate the hash
byte[] digest = hash.digest(input);
...
// Base64-encode it before returning
return Base64.encodeBytes(digest);


If this is what you have done you should get the same result.
Avatar of mak730

ASKER

Yes it is exactly what I'm doing, but, for documentation that I read, it seems (I'm not sure) that Java does not have the SHA256 algorithm so I guess it is taken SHA1. Do you know if the framework has, in fact,  built in support for SHA256?
ASKER CERTIFIED SOLUTION
Avatar of doraemon-nolife
doraemon-nolife

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial