Java MD5 : Hacker Rank Solution : Digit Wood

Java
,

MD5 (Message-Digest algorithm 5) is a widely-used cryptographic hash function with a 128-bit hash value. Here are some common uses for MD5:

  • To store a one-way hash of a password.
  • To provide some assurance that a transferred file has arrived intact.

MD5 is one in a series of message digest algorithms designed by Professor Ronald Rivest of MIT (Rivest, 1994); however, the security of MD5 has been severely compromised, most infamously by the Flame malware in 2012. The CMU Software Engineering Institute essentially considers MD5 to be “cryptographically broken and unsuitable for further use”.

Given an alphanumeric string, s, denoting a password, compute and print its MD5 encryption value.

Input Format

A single alphanumeric string denoting s.

Output Format

Print the MD5 encryption value of s on a new line.

Sample Input 0

Sample Output 0

Sample Input 1

Sample Output 1

SOLUTION:-

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

public class Solution {

    public static void main(String[] args) {
     
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        sc.close();
        String output = "";
        try{
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] digest = md.digest(input.getBytes());
            BigInteger bigInteger = new BigInteger(1,digest);
            
            String hash = bigInteger.toString(16);
            while(hash.length() < 32){
                hash += "0" + hash;
            }

            output = hash;
            System.out.println(output);
        }
        catch(NoSuchAlgorithmException ex){
            System.out.println(ex.getMessage());
        }
    }
}

FOLLOW FOR MORE QUESTIONS AND SOLUTIONS | DIGIT WOOD

Leave a Reply

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