SHA -256 Hash in Java Spring boot
In Cryptography, SHA is cryptographic hash function which takes input as 20 Bytes and rendered the hash value in hexadecimal number, 40 digits long approx.
Message Digest Class:
To calculate cryptographic hashing value in Java, MessageDigest Class is used, under the package java.security.
MessagDigest Class provides following cryptographic hash function to find hash value of a text, they are:
MD5
SHA-1
SHA-256
This Algorithms are initialized in static method called getInstance(). After selecting the algorithm it calculate the digest value and return the results in byte array.
BigInteger class is used, which converts the resultant byte array into its sign-magnitude representation. This representation is converted into hex format to get the MessageDigest
hello world : b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
Javaskool.com : e02fb13635717a1b4ed00f066bcb9257a35324d103910e566039596745b92fc7
James Bond : 80ae44e3fa55f4bb5a593e7406147b59a801feab279dc2154e0d5eb9f757dd4c
package com.javaskool;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
// Java program to calculate SHA hash value
class GenerateSHA256InSpringBoot {
public static byte[] getSHA(String input) throws NoSuchAlgorithmException
{
// Static getInstance method is called with hashing SHA
MessageDigest md = MessageDigest.getInstance("SHA-256");
// digest() method called
// to calculate message digest of an input
// and return array of byte
return md.digest(input.getBytes(StandardCharsets.UTF_8));
}
public static String toHexString(byte[] hash)
{
// Convert byte array into signum representation
/*
* Translates the sign-magnitude representation of a BigInteger into
* aBigInteger. The sign is represented as an integer signum value: -1
* fornegative, 0 for zero, or 1 for positive. The magnitude is a byte arrayin
* big-endian byte-order: the most significant byte is in the zeroth element. A
* zero-length magnitude array is permissible, and willresult in a BigInteger
* value of 0, whether signum is -1, 0 or 1
*/
BigInteger number = new BigInteger(1, hash);
// Convert message digest into hex value
StringBuilder hexString = new StringBuilder(number.toString(16));
// Pad with leading zeros
while (hexString.length() < 64)
{
hexString.insert(0, '0');
}
return hexString.toString();
}
// Driver code
public static void main(String args[])
{
try
{
System.out.println("HashCode Generated by SHA-256 for:");
String s2 = "hello world";
System.out.println("\n" + s2 + " : " + toHexString(getSHA(s2)));
String s1 = "Javaskool.com";
System.out.println("\n" + s1 + " : " + toHexString(getSHA(s1)));
String s3 = "James Bond";
System.out.println("\n" + s3 + " : " + toHexString(getSHA(s3)));
}
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown for incorrect algorithm: " + e);
}
}
}
Output:
HashCode Generated by SHA-256 for:
hello world : b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
Javaskool.com : e02fb13635717a1b4ed00f066bcb9257a35324d103910e566039596745b92fc7
James Bond : 80ae44e3fa55f4bb5a593e7406147b59a801feab279dc2154e0d5eb9f757dd4c
Recent Comments