SHA-512 Hash In Java
Last Updated :
13 Mar, 2024
The SHA-2 family of cryptographic hash functions consists of six hash functions. These are:
- SHA-224, with 224-bit hash values
- SHA-256, with 256-bit hash values
- SHA-384, with 384-bit hash values
- SHA-512, with 512-bit hash values
- SHA-512/224, with 512-bit hash values
- SHA-512/256, with 512-bit hash values
Among these, SHA-256 and SHA-512 are the most commonly accepted and used hash functions computed with 32-bit and 64-bit words, respectively. SHA-224 and SHA-384 are truncated versions of SHA-256 and SHA-512 respectively, computed with different initial values. To calculate the cryptographic hashing value in Java,
MessageDigest Class is used, under the package java.security. MessagDigest Class provides the following cryptographic hash function to find the hash value of a text as follows:
- MD2
- MD5
- SHA-1
- SHA-224
- SHA-256
- SHA-384
- SHA-512
These algorithms are initialized in a static method called getInstance(). After selecting the algorithm the message digest value is calculated and the results is returned as a byte array. BigInteger class is used, to convert the resultant byte array into its signum representation. This representation is then converted into a hexadecimal format to get the expected MessageDigest.
Examples for SHA-512 Hash in Java
Input: hello world
Output: 309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f
Input: GeeksForGeeks
Output: acc10c4e0b38617f59e88e49215e2e894afaee5ec948c2af6f44039f03c9fe47a9210e01d5cd926c142bdc9179c2ad30f927a8faf69421ff60a5eaddcf8cb9c
Program to Calculate SHA-512 Hash Value
Below program shows the implementation of SHA-512 hash function:
Java
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class GFG {
public static String encryptThisString(String input)
{
try {
MessageDigest md = MessageDigest.getInstance( "SHA-512" );
byte [] messageDigest = md.digest(input.getBytes());
BigInteger no = new BigInteger( 1 , messageDigest);
String hashtext = no.toString( 16 );
while (hashtext.length() < 32 ) {
hashtext = "0" + hashtext;
}
return hashtext;
}
catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
public static void main(String args[]) throws NoSuchAlgorithmException
{
System.out.println( "HashCode Generated by SHA-512 for: " );
String s1 = "GeeksForGeeks" ;
System.out.println( "\n" + s1 + " : " + encryptThisString(s1));
String s2 = "hello world" ;
System.out.println( "\n" + s2 + " : " + encryptThisString(s2));
}
}
|
Output
HashCode Generated by SHA-512 for:
GeeksForGeeks : acc10c4e0b38617f59e88e49215e2e894afaee5
ec948c2af6f44039f03c9fe47a9210e01d5cd926c142bdc9179c2ad
30f927a8faf69421ff60a5eaddcf8cb9c
hello world : 309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee5
11a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cf
d830e81f605dcf7dc5542e93ae9cd76f
Application
Application of SHA-512 in the real world are mentioned below:
- Cryptography
- Data Integrity
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...