Java SHA Hashing with Example

In this tutorial, we will learn about the security package and  MessageDigest class in Java and SHA algorithm.

What is SHA?

Secure Hash Algorithm(SHA) is a cryptographic hash function that takes input message of any size and returns fixed bytes of string as an output. SHA-256 is a one-way function that converts a string of any length into a string of 256 bits.

SHA in Java

We will be using the MessageDigest class for the implementation of SHA in Java.

How to implement a MessageDigest class?

MessageDigest class is present in the java. security package. There are a few steps that we must follow in order to work with the MessageDigest class.

  1. Creating the object of MessageDigest class
    A static method getInstance(String  Algo-type) is used to create the object of MessageDigest class where we specify the type of Algorithm we are working with. Here we are using SHA as a hashing fuction so our parameter will be “SHA-256”. SHA-256 is mostly used to provide higher security.
    The algorithms that can have functionality are:
    >MD2
    >MD5
    >SHA-1
    >SHA-256
    >SHA-384
    >SHA-512

    //Object creation of MessageDigest class 
    MessageDigest dig=MessageDigest.getInstance("SHA-256");
  2. Updating the MessageDigest
    If there are multiple blocks of data then we use the update method of the MessageDigest class. After that, we can use the digest()  function to finishing the computation of the message digest.

    //Updating the digest using the byte form of the message.
     dig.update(msg.getBytes());
     dig.update(msg1.getBytes());
     dig.digest();
  3. When to use a digest() method?
    The digest method is used directly without an update for a single block of data. Whenever multiple blocks of data are present then we use the update method and finally call the digest().
    Syntax:
    byte[]   digest(byte[]   input_msg);
  4. After getting the byte form of the message to convert the byte form into Hexadecimal format using HexString of Integer class.
    StringBuffer hex = new StringBuffer(); 
    
    for (int i = 0;i<byt.length;i++) 
    {
     //converting into hexadecimal format
     hex.append(Integer.toHexString( byt[i]));
    }

    Here is the Java Code

import java.util.Scanner;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
class Secure
{
    public static void encrypting(String msg) throws NoSuchAlgorithmException
    {
        try
        {
            //Object creation of MessageDigest class
            MessageDigest dig=MessageDigest.getInstance("SHA-256");

            //Updating the digest using the  byte form of meassage.
            dig.update(msg.getBytes());


            //hashing operations are perormed 
            byte[] byt= dig.digest();

           //Creating a bufffer to store the String  form of hexadecimal number
            StringBuffer hex = new StringBuffer();

            for (int i = 0;i<byt.length;i++)
           {
               //converting into hexadecimal format
             hex.append(Integer.toHexString( byt[i]));
           }
          
          System.out.println("Hex format : " + hex.toString());    
        }
       
        
        catch(Exception e)
        {
            System.out.println(e);
        }
                
    }
    
    public static void main(String args[]) throws NoSuchAlgorithmException
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the message: ");
        String  msg=sc.nextLine();
        encrypting(msg);
        
    }
}
Output:
Enter the message: 
Codespeedy
Hex format : 63f40457d25ffffffd0ffffffb5ffffffc76743affffffb1562cffffff89ffffffd1ffffff8a6638507dffffffdf6359ffffffecffffffd1ffffffe3ffffffd9ffffff8f44ffffff9b
Enter the message: 
codespeedy
Hex format : 598ffffffee68ffffffc8ffffffeaffffffc1ffffffd259ffffffde9ffffffcb2dffffffb715ffffffbc4119ffffffc6ffffffaaffffffccffffffe8ffffff9a246a257513fffffff20ffffff97ffffffb5

Notice: Check the input in both cases.

Leave a Reply

Your email address will not be published. Required fields are marked *