SHA Hashing in Java

How to generate SHA  Hash For a given String in java

import java.security.MessageDigest;
import javax.xml.bind.DatatypeConverter;

public class SHAExample {
public static void main(String[] args) {
 String message="Hello World";

 try
 {
 MessageDigest digest = MessageDigest.getInstance("SHA-256"); // hashing algorithm of your choice
 byte[] encBytes = digest.digest(message.getBytes("UTF-8"));
 String hash = DatatypeConverter.printHexBinary(encBytes);
 System.out.println("Default : "+hash);
 System.out.println("LowerCase: "+hash.toLowerCase());

 }catch(Exception ex)
 {
 ex.printStackTrace();
 }
 }
}

Output:

Default : A591A6D40BF420404A011733CFB7B190D62C65BF0BCDA32B57B277D9AD9F146E
LowerCase: a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e
Advertisements
This entry was posted in Java. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s