SHA-256 Hash in Java

Definition:
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:

  1. MD5
  2. SHA-1
  3. 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



Examples:

Input : hello world
Output : b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9

Input : GeeksForGeeks
Output : 112e476505aab51b05aeb2246c02a11df03e1187e886f7c55d4e9935c290ade
filter_none

edit
close

play_arrow

link
brightness_4
code

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
  
// Java program to calculate SHA hash value
  
class GFG {
    public static String getSHA(String input)
    {
  
        try {
  
            // 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
            byte[] messageDigest = md.digest(input.getBytes());
  
            // Convert byte array into signum representation
            BigInteger no = new BigInteger(1, messageDigest);
  
            // Convert message digest into hex value
            String hashtext = no.toString(16);
  
            while (hashtext.length() < 32) {
                hashtext = "0" + hashtext;
            }
  
            return hashtext;
        }
  
        // For specifying wrong message digest algorithms
        catch (NoSuchAlgorithmException e) {
            System.out.println("Exception thrown"
                               + " for incorrect algorithm: " + e);
  
            return null;
        }
    }
  
    // Driver code
    public static void main(String args[]) throws NoSuchAlgorithmException
    {
        System.out.println("HashCode Generated by SHA-256 for:");
  
        String s1 = "GeeksForGeeks";
        System.out.println("\n" + s1 + " : " + getSHA(s1));
  
        String s2 = "hello world";
        System.out.println("\n" + s2 + " : " + getSHA(s2));
    }
}

chevron_right


Output:

HashCode Generated by SHA-256 for:

GeeksForGeeks : 112e476505aab51b05aeb2246c02a11df03e1187e886f7c55d4e9935c290ade

hello world : b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9

Application:

  1. Cryptography
  2. Data Integrity


My Personal Notes arrow_drop_up

सर्वशक्तिशाली इकलौता

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.