How to generate a SHA256 and SHA512 hash from a String in Java

The SHA256 can be generated using the MessageDigest class in the jdk.

Syntax:

  MessageDigest digest = MessageDigest.getInstance("SHA-256");
  digest.reset();
  digest.update(input.getBytes("utf8"));
  toReturn = String.format("%064x", new BigInteger(1, digest.digest()));

A SHA-512 can also be generated without extra java libraries:

Syntax:

  MessageDigest digest = MessageDigest.getInstance("SHA-512");
  digest.reset();
  digest.update(input.getBytes("utf8"));
  toReturn = String.format("%0128x", new BigInteger(1, digest.digest()));

The following code creates a SHA256 and a SHA512 using the "this is an example" as an input and the SHA-2 Hash functions. First 2 methods are created one for each of the hash function. The first one is for SHA-256 and generates the hash using the MessageDigest class. The second method is created the same way but using SHA-512. The validate the output we also generate the hash with DigestUtils class from Apache commons. All the geneated hash are written in the console.

1- Create the following java file:

import java.math.BigInteger;
import java.security.MessageDigest;

public class SHA2 {

    public static String getSHA256(String input){

	String toReturn = null;
	try {
	    MessageDigest digest = MessageDigest.getInstance("SHA-256");
	    digest.reset();
	    digest.update(input.getBytes("utf8"));
	    toReturn = String.format("%064x", new BigInteger(1, digest.digest()));
	} catch (Exception e) {
	    e.printStackTrace();
	}
	
	return toReturn;
    }

    public static String getSHA512(String input){

	String toReturn = null;
	try {
	    MessageDigest digest = MessageDigest.getInstance("SHA-512");
	    digest.reset();
	    digest.update(input.getBytes("utf8"));
	    toReturn = String.format("%0128x", new BigInteger(1, digest.digest()));
	} catch (Exception e) {
	    e.printStackTrace();
	}
	
	return toReturn;
    }
    
    public static void main(String[] argv) {

	String inputValue = "this is an example";

	// With the java libraries
	String sha256 = getSHA256(inputValue);

	System.out.println("The SHA-256 of \"" + inputValue + "\" is:");
	System.out.println(sha256);
	System.out.println();

	// With Apache commons
	sha256 = org.apache.commons.codec.digest.DigestUtils.sha256Hex(inputValue);

	System.out.println("The SHA-256 of \"" + inputValue + "\" is:");
	System.out.println(sha256);
	System.out.println();

	// With the java libraries
	String sha512 = getSHA512( inputValue );

	System.out.println("The SHA-512 of \"" + inputValue + "\" is:");
	System.out.println(sha512);
	System.out.println();

	// With Apache commons
	sha512 = org.apache.commons.codec.digest.DigestUtils.sha512Hex(inputValue);

	System.out.println("The SHA-512 of \"" + inputValue + "\" is:");
	System.out.println(sha512);

    }
}

The output will be:

The SHA-256 of "this is an example13" is:
066f61646a92c8cf04943577b3c4d9ee6e5d125fe850b4c8c4d315ddd3aa1f50

The SHA-256 of "this is an example13" is:
066f61646a92c8cf04943577b3c4d9ee6e5d125fe850b4c8c4d315ddd3aa1f50

The SHA-512 of "this is an example4" is:
05c1649335528b45ab4965619db876e5090e42dfc526b486dde9368df2b77830c4e523cee1fd0054d9009729a6a11e697a1dd121e36be3ada593b8d76cb80ced

The SHA-512 of "this is an example4" is:
05c1649335528b45ab4965619db876e5090e42dfc526b486dde9368df2b77830c4e523cee1fd0054d9009729a6a11e697a1dd121e36be3ada593b8d76cb80ced

References:

Java 8
SHA-2 cryptographic hash functions

Share this article:

Recent Comments