Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4726
  • Last Modified:

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

  • 2
1 Solution
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.
mak730Author Commented:
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?
So far, there is not common standard for JCE(Java Cryptography Extension) implementation. The implementation (the jar file you choose ) might not cover SHA-256 algorithm.

Check here, and choose the one that suit you most

As far as i know, SHA-1 and SHA-256 is quite similar. They only differs in the message size.

If you choose SHA-1, you would have more implementation choices since most implementation covered SHA-1


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now