Meaningless Drivel is fun!
The moose likes Java in General and the fly likes MessageDigest with SHA 256 algorithm fails Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "MessageDigest with SHA 256 algorithm fails" Watch "MessageDigest with SHA 256 algorithm fails" New topic

MessageDigest with SHA 256 algorithm fails

Anup Bansal
Ranch Hand

Joined: Sep 12, 2006
Posts: 69

I need to generate a digest value for securing my message. I am using the follwing piece of code to generate the digest value using the MessageDigest class.

public class message {

static String a = "SHA-1";
static String b = "SHA-256";

public static void main (String arg[]){

try {
MessageDigest digest = MessageDigest.getInstance(a);
digest.update("MY TEST DATA".getBytes());
byte[] result = digest.digest();
System.out.println("Digest value"+result.toString());

MessageDigest dig = MessageDigest.getInstance(b);
catch (NoSuchAlgorithmException e){

The output of this program is:
SHA-1---->SHA-1 Message Digest from SUN, <initialized>

Digest value[B@13e8d89 SHA256 MessageDigest not available
at Source)
at Source)
at Source)
at Source)
at message.main(

The digest value is properly generated for the SHA-1 algorithm but it does not work for SHA-256 algorithm.
I thought this to be a version problem. The version of JDK I was initially using was 1.4.1.

I found out that SHA-256 is only suported in JDK version 1.4.2 and later.
I have tested the above code using JDK version 1.4.2 and JDK 1.5.
The output is the same.

Can anyone please tell me where have I gone wrong?

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42958
Both digests work fine for me with both JDK 1.4.2_09 and JDK 1.5.0_06 on OS X. Which JDK versions are you using?
I agree. Here's the link:
subject: MessageDigest with SHA 256 algorithm fails
It's not a secret anymore!