Java MD-5 HackerRank Solution with Explanation

Java MD-5 HackerRank Solution

Hello Friends, How are you? Today I will solve the HackerRank Java SHA-256 Problem with a straightforward explanation. In this article, you will get more than one approach to solve this problem. So let's start-

{tocify} $title={Table of Contents}

Java MD-5 HackerRank Solution

HackerRank Java MD-5 Solution - Problem Statement

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.

Constraints

6 <= |s| <= 20
String s consists of English alphabetic letters (i.e., [a - zA - Z]  and/or decimal digits (i.e., 0 through 9) only.

Output Format

Print the MD-5 encryption value of s on a new line.

Sample Input 0

HelloWorld {codeBox}

Sample Output 0

68e109f0f40ca72a15e05cc22786f8e6{codeBox}

Sample Input 1

Javarmi123{codeBox}

Sample Output 1

2da2d1e0ce7b4951a858ed2d547ef485{codeBox}

Java MD-5 Hacker Rank Solution

Approach I: Java MD-5 HackerRank Java Solution

// ========================
//       Information
// ========================

// Name: Java MD-5 HackerRank Problem
// Direct Link: https://www.hackerrank.com/challenges/java-md5/problem
// Difficulty: Medium
// Max Score: 30
// Language: Java 8

// ========================
//         Solution Start
// ========================

// Java MD-5 - Hacker Rank Solution Start

import java.util.Scanner;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Solution {
    public static void main(String[] args) throws NoSuchAlgorithmException {
        /* Read and save the input String */
        Scanner scan = new Scanner(System.in);
        String str = scan.next();
        scan.close();
        
        /* Encode the String using MD5 */
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(str.getBytes());
        byte[] digest = md.digest();
        
        /* Print the encoded value in hexadecimal */
        for (byte b : digest) {
            System.out.format("%02x", b);
        }
    }
}

// Java MD-5 Hacker Rank Solution END
// MyEduWaves

Approach II: Java MD-5 HackerRank Java Solution

// ========================
//       Information
// ========================

// Name: Java MD-5 HackerRank Problem
// Direct Link: https://www.hackerrank.com/challenges/java-md5/problem
// Difficulty: Medium
// Max Score: 30
// Language: Java 8

// ========================
//         Solution Start
// ========================

// Java MD-5 - Hacker Rank Solution Start

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.security.MessageDigest;

public class Solution {

  private static String convertByteToHex(byte[] byteData) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < byteData.length; i++) {
      sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
    }
    return sb.toString();
  }

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String word = sc.next();
    MessageDigest md;
    try {
      md = MessageDigest.getInstance("MD5");
      System.out.println(convertByteToHex(md.digest(word.getBytes())));
    } catch (Exception e) { }
  }
}

// Java MD-5 Hacker Rank Solution END
// MyEduWaves


Disclaimer: The above Problem ( Java MD-5 ) is generated by Hackerrank but the Solution is Provided by MyEduWaves. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the contact form.

I hope you have understood the solution to this HackerRank Problem. All these three solutions will pass all the test cases. Now visit Java MD-5 Hackerrank Problem and try to solve it again.

All the Best!

Post a Comment

Previous Post Next Post