Open In App

SHA-384 Hash In Java

Last Updated : 27 Sep, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The SHA-2 family of cryptographic hash functions consists of six hash functions. These are:

  1. SHA-224, with 224 bit hash values
  2. SHA-256, with 256 bit hash values
  3. SHA-384, with 384 bit hash values
  4. SHA-512, with 512 bit hash values
  5. SHA-512/224, with 512 bit hash values
  6. SHA-512/256, with 512 bit hash values
  7. 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 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 as follows:

    • MD2
    • MD5
    • SHA-1
    • SHA-224
    • SHA-256
    • SHA-384
    • SHA-512

    These algorithms are initialized in static method called getInstance(). After selecting the algorithm the message digest value is calculated and the results are 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:

    Input : hello world
    Output :
    fdbd8e75a67f29f701a4e040385e2e23986303ea10239211af907fcbb83578b3e417cb71ce646efd0819dd8c088de1bd

    Input : GeeksForGeeks
    Output :
    19cc78d220368a892cc9c54d2f43d5e1823534f3e22b0d475de18e030e7c4b411704c79e9600bb93399688e7f09bb226

    Program: Below program shows the implementation of SHA-384 hash in Java.




    // Java program to calculate SHA-384 hash value
      
    import java.math.BigInteger;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
      
    public class GFG {
        public static String encryptThisString(String input)
        {
            try {
                // getInstance() method is called with algorithm SHA-384
                MessageDigest md = MessageDigest.getInstance("SHA-384");
      
                // digest() method is called
                // to calculate message digest of the input string
                // returned as 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);
      
                // Add preceding 0s to make it 32 bit
                while (hashtext.length() < 32) {
                    hashtext = "0" + hashtext;
                }
      
                // return the HashText
                return hashtext;
            }
      
            // For specifying wrong message digest algorithms
            catch (NoSuchAlgorithmException e) {
                throw new RuntimeException(e);
            }
        }
      
        // Driver code
        public static void main(String args[]) throws 
                                     NoSuchAlgorithmException
        {
      
            System.out.println("HashCode Generated by SHA-384 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-384 for: 
    
    GeeksForGeeks : 19cc78d220368a892cc9c54d2f43d5e1823
    534f3e22b0d475de18e030e7c4b411704c79e9600bb93399688
    e7f09bb226
    
    hello world : fdbd8e75a67f29f701a4e040385e2e2398630
    3ea10239211af907fcbb83578b3e417cb71ce646efd0819dd8c
    088de1bd
    

    Application:

    • Cryptography
    • Data Integrity


    Like Article
    Suggest improvement
    Previous
    Next
    Share your thoughts in the comments

Similar Reads