Click here to Skip to main content
15,911,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I'm developing a Java project that requires the implementation of Java Hashing algorithms such as MD5 or SHA-256. The current implementation according to the Java API is:
Java
MessageDigest md = MessageDigest.getInstance("SHA");
 try {
     md.update(toChapter1);
     MessageDigest tc1 = md.clone();
     byte[] toChapter1Digest = tc1.digest();
     md.update(toChapter2);
     ...etc.
 } catch (CloneNotSupportedException cnse) {
     throw new DigestException("couldn't make digest of partial content");
 }


How do I implement the complete solution to store the hashed value in a MySQL database?
Posted

1 solution

You can use SHA-256 to create digest from string.
Below is code snippet for you.
String password = "password";
 
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(password.getBytes());
 
byte data[] = md.digest();
 
//convert the byte to hex format
StringBuffer sb = new StringBuffer();
for (int i = 0; i < data.length; i++) {
    sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
}
 
System.out.println("Digest in Hex: " + sb.toString());
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900