• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

MessageDigest with SHA 256 algorithm fails

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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.
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Security;


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);
System.out.println("SHA-1---->"+digest);
digest.update("MY TEST DATA".getBytes());
byte[] result = digest.digest();
System.out.println("Digest value"+result.toString());

MessageDigest dig = MessageDigest.getInstance(b);
System.out.println("SHA256----->"+dig.toString());
}
catch (NoSuchAlgorithmException e){
e.printStackTrace();
}
}
}

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

Digest value[B@13e8d89
java.security.NoSuchAlgorithmException: SHA256 MessageDigest not available
at java.security.Security.getEngineClassName(Unknown Source)
at java.security.Security.getEngineClassName(Unknown Source)
at java.security.Security.getImpl(Unknown Source)
at java.security.MessageDigest.getInstance(Unknown Source)
at message.main(message.java:20)

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?

Regards,
Anup
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
reply
    Bookmark Topic Watch Topic
  • New Topic