Open In App
Related Articles

SHA-256 Hash in Java

Improve Article
Save Article
Like Article

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


Input : hello world
Output: b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9

Input : GeeksForGeeks
Output: 112e476505aab51b05aeb2246c02a11df03e1187e886f7c55d4e9935c290ade

Input : K1t4fo0V
Output: 0a979e43f4874eb24b740c0157994e34636eed0425688161cc58e8b26b1dcf4e


import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
// Java program to calculate SHA hash value
class GFG2 {
    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
        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[])
            System.out.println("HashCode Generated by SHA-256 for:");
            String s1 = "GeeksForGeeks";
            System.out.println("\n" + s1 + " : " + toHexString(getSHA(s1)));
            String s2 = "hello world";
            System.out.println("\n" + s2 + " : " + toHexString(getSHA(s2)));
            String s3 = "K1t4fo0V";
            System.out.println("\n" + s3 + " : " + toHexString(getSHA(s3)));
        // For specifying wrong message digest algorithms
        catch (NoSuchAlgorithmException e) {
            System.out.println("Exception thrown for incorrect algorithm: " + e);


HashCode Generated by SHA-256 for:

GeeksForGeeks : 112e476505aab51b05aeb2246c02a11df03e1187e886f7c55d4e9935c290ade

hello world : b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9

K1t4fo0V : a979e43f4874eb24b740c0157994e34636eed0425688161cc58e8b26b1dcf4e


  1. Cryptography
  2. Data Integrity


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 29 Apr, 2022
Like Article
Save Article
Similar Reads
Related Tutorials