Java SHA Hashing Example
The SHA hash functions are a set of cryptographic hash functions designed by the National Security Agency (NSA) and published by the NIST as a U.S. Federal Information Processing Standard. SHA stands for Secure Hash Algorithm. The three SHA algorithms are structured differently and are distinguished as SHA-0, SHA-1, and SHA-2. The SHA-2 family uses an identical algorithm with a variable digest size which is distinguished as SHA-224, SHA-256, SHA-384, and SHA-512.
SHA-2 is believe the most secure hashing algorithm as this article is written, here are few examples for the SHA implementation. The possible MessageDigest algorithm are SHA-1, SHA-256, SHA-384, and SHA-512, you can check the reference for the detail.
1. File checksum with SHA-256
It will use SHA-256 hashing algorithm to generate a checksum for file “c:\\loging.log”.
package com.mkyong.test; import java.io.FileInputStream; import java.security.MessageDigest; public class SHACheckSumExample { public static void main(String[] args)throws Exception { MessageDigest md = MessageDigest.getInstance("SHA-256"); FileInputStream fis = new FileInputStream("c:\\loging.log"); byte[] dataBytes = new byte[1024]; int nread = 0; while ((nread = fis.read(dataBytes)) != -1) { md.update(dataBytes, 0, nread); }; byte[] mdbytes = md.digest(); //convert the byte to hex format method 1 StringBuffer sb = new StringBuffer(); for (int i = 0; i < mdbytes.length; i++) { sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1)); } System.out.println("Hex format : " + sb.toString()); //convert the byte to hex format method 2 StringBuffer hexString = new StringBuffer(); for (int i=0;i<mdbytes.length;i++) { hexString.append(Integer.toHexString(0xFF & mdbytes[i])); } System.out.println("Hex format : " + hexString.toString()); } }
Output
Hex format : 21a57f2fe765e1ae4a8bf15d73fc1bf2a533f547f2343d12a499d9c0592044d4 Hex format : 21a57f2fe765e1ae4a8bf15d73fc1bf2a533f547f2343d12a499d9c0592044d4
2. Hashing String with SHA-256
It will use SHA-256 hashing algorithm to generate a hash value for a password “123456″.
package com.mkyong.test; import java.security.MessageDigest; public class SHAHashingExample { public static void main(String[] args)throws Exception { String password = "123456"; MessageDigest md = MessageDigest.getInstance("SHA-256"); md.update(password.getBytes()); byte byteData[] = md.digest(); //convert the byte to hex format method 1 StringBuffer sb = new StringBuffer(); for (int i = 0; i < byteData.length; i++) { sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1)); } System.out.println("Hex format : " + sb.toString()); //convert the byte to hex format method 2 StringBuffer hexString = new StringBuffer(); for (int i=0;i<byteData.length;i++) { String hex=Integer.toHexString(0xff & byteData[i]); if(hex.length()==1) hexString.append('0'); hexString.append(hex); } System.out.println("Hex format : " + hexString.toString()); } }
Output
Hex format : 8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92 Hex format : 8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92
Reference
- http://en.wikipedia.org/wiki/SHA_hash_functions
- http://java.sun.com/j2se/1.4.2/docs/guide/security/CryptoSpec.html#AppA
Thank you so much, this tutorial is very useful and clear.
Thanks, this helped me out a lot!
Hi,
Can you please tell me that how can we convert the SHA-256 Hash into simple text, using javascript, c#.net?
Thanks
[...] came across this article which gives a simple means to perform File Checksum and Hashing String in [...]
why don’t you use DigestInputStream?
[...] To hash string with SHA or MD5 algorithm, refer to this Java SHA example or using Jacksum, third-party Java library. For readability, we will use Jacksum to perform [...]
Another byte to hex format method:
I saw this idea in the comments at: http://www.spiration.co.uk/post/1199/Java-md5-example-with-MessageDigest
-oops should have been:
Sorry about that!
Wrong:(2. Hashing String with SHA-256)
System.out.println(Hex format : ” + hexString.toString());
Right
System.out.println(“Hex format : ” + hexString.toString());
Article is updated, thanks for point out the typo mistake.