MD5 Hash in Java

 Hello Guys , 

    In Cryptography, MD5 widely used in Cryptographic hash function. MD5 produces a hash 128 bit.

The MD5 Stands for Message Digest.MD5 is Hashing algorithm is a cryptographic function that accepts a message of any length as input and returns as output a fixed-length digest value to be used for authentica- ting original message.

The MD5 function was originally designed for use a secure cryptographic function for authenticating Digital Signature.



Message Digest Class :

  • To calculate cryptographic hashing value in Java, MessageDigest Class is used, under the package java.security.

  • MessageDigest Class provides following cryptographic hash function to find hash value of a text, they are:

                  MD5

                  SHA-1

                  SHA-256

This Algorithms are initialized in static method called getInstance(). After selecting the algorithm it calculate the digest value and the return the results in byte array.


MD5 PROGRAM :

import java.io.*;
import java.util.*;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

// Java Program to calculate MD5 hash value;

public class MD5{
  
      public static String getMD5(String input)
      {
          try {
                 // Static getinstance function is called hashing MD5
               
                 MessageDigest md = MessageDigest.getInstance("MD5");
               
                 // digest function called calculate message digest
                 // of input digest() and return to 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){
                  throw new RuntimeException(e);
                }
                
      }
    public static void main(String args[])throws NoSuchAlgorithmException
    {
        System.out.print("Enter the word :");   
        Scanner scan = new Scanner(System.in);
        String s = scan.next();
        System.out.println(s+ " : " + getMD5(s));
    }

}   




Output :

Enter the word :infopsh
infopsh : 1d876a8c50f765b3ff69f5e0eede653a

Enter the word :Ps_Info
Ps_Info : 6ac274128ea03a0560266aeef7253b71




If you liked this post do comment ,share and promote the post 🙏 . Please stay with us and support.  🙏








    


Post a Comment

0 Comments