Skip to content

Commit ed97e40

Browse files
Add MD5 hash function implementation for generating message digests
1 parent 06c65e3 commit ed97e40

File tree

2 files changed

+146
-0
lines changed
  • src
    • main/java/com/thealgorithms/hashes
    • test/java/com/thealgorithms/hashes

2 files changed

+146
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.thealgorithms.hashes;
2+
3+
import java.math.BigInteger;
4+
import java.security.MessageDigest;
5+
import java.security.NoSuchAlgorithmException;
6+
import java.util.Scanner;
7+
8+
/**
9+
* The MD5 class provides a method to generate the MD5 hash for a given string.
10+
* MD5 (Message Digest Algorithm 5) is a widely used cryptographic hash function that produces
11+
* a 128-bit (16-byte) hash value. It is commonly used for checksums and data integrity,
12+
* although it is no longer considered secure for cryptographic purposes.
13+
*
14+
* @see <a href="https://en.wikipedia.org/wiki/MD5">MD5 on Wikipedia</a>
15+
*/
16+
public class MD5 {
17+
18+
/**
19+
* Generates the MD5 hash of the input string.
20+
*
21+
* @param input the string to be hashed
22+
* @return the MD5 hash as a hexadecimal string
23+
*/
24+
public static String getMd5(String input) {
25+
try {
26+
// Create a MessageDigest instance for MD5
27+
MessageDigest md = MessageDigest.getInstance("MD5");
28+
29+
// Compute the MD5 digest of the input string
30+
byte[] messageDigest = md.digest(input.getBytes());
31+
32+
// Convert the byte array into a BigInteger to get the hash in hexadecimal format
33+
BigInteger no = new BigInteger(1, messageDigest);
34+
35+
// Convert the BigInteger to a hexadecimal string
36+
String hashtext = no.toString(16);
37+
38+
// Pad with leading zeros to ensure the hash is 32 characters long
39+
while (hashtext.length() < 32) {
40+
hashtext = "0" + hashtext;
41+
}
42+
43+
// Return the generated MD5 hash
44+
return hashtext;
45+
} catch (NoSuchAlgorithmException e) {
46+
// Handle the case where MD5 algorithm is not available
47+
throw new RuntimeException(e);
48+
}
49+
}
50+
51+
/**
52+
* The main method that takes user input, computes its MD5 hash, and prints the result.
53+
*
54+
* @param args command-line arguments
55+
* @throws NoSuchAlgorithmException if the MD5 algorithm is not available
56+
*/
57+
public static void main(String args[]) throws NoSuchAlgorithmException {
58+
// Create a Scanner object to read input from the user
59+
Scanner scanner = new Scanner(System.in);
60+
61+
// Prompt the user to enter a string to hash
62+
System.out.print("Enter the string to hash: ");
63+
64+
// Read the input string from the user
65+
String s = scanner.nextLine();
66+
67+
// Compute the MD5 hash of the input string and print it
68+
System.out.println("Your HashCode Generated by MD5 is: " + getMd5(s));
69+
70+
// Close the Scanner object
71+
scanner.close();
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.thealgorithms.hashes;
2+
3+
import java.math.BigInteger;
4+
import java.security.MessageDigest;
5+
import java.security.NoSuchAlgorithmException;
6+
import java.util.Scanner;
7+
8+
/**
9+
* The MD5 class provides a method to generate the MD5 hash for a given string.
10+
* MD5 (Message Digest Algorithm 5) is a widely used cryptographic hash function that produces
11+
* a 128-bit (16-byte) hash value. It is commonly used for checksums and data integrity,
12+
* although it is no longer considered secure for cryptographic purposes.
13+
*
14+
* @see <a href="https://en.wikipedia.org/wiki/MD5">MD5 on Wikipedia</a>
15+
*/
16+
public class MD5 {
17+
18+
/**
19+
* Generates the MD5 hash of the input string.
20+
*
21+
* @param input the string to be hashed
22+
* @return the MD5 hash as a hexadecimal string
23+
*/
24+
public static String getMd5(String input) {
25+
try {
26+
// Create a MessageDigest instance for MD5
27+
MessageDigest md = MessageDigest.getInstance("MD5");
28+
29+
// Compute the MD5 digest of the input string
30+
byte[] messageDigest = md.digest(input.getBytes());
31+
32+
// Convert the byte array into a BigInteger to get the hash in hexadecimal format
33+
BigInteger no = new BigInteger(1, messageDigest);
34+
35+
// Convert the BigInteger to a hexadecimal string
36+
String hashtext = no.toString(16);
37+
38+
// Pad with leading zeros to ensure the hash is 32 characters long
39+
while (hashtext.length() < 32) {
40+
hashtext = "0" + hashtext;
41+
}
42+
43+
// Return the generated MD5 hash
44+
return hashtext;
45+
} catch (NoSuchAlgorithmException e) {
46+
// Handle the case where MD5 algorithm is not available
47+
throw new RuntimeException(e);
48+
}
49+
}
50+
51+
/**
52+
* The main method that takes user input, computes its MD5 hash, and prints the result.
53+
*
54+
* @param args command-line arguments
55+
* @throws NoSuchAlgorithmException if the MD5 algorithm is not available
56+
*/
57+
public static void main(String args[]) throws NoSuchAlgorithmException {
58+
// Create a Scanner object to read input from the user
59+
Scanner scanner = new Scanner(System.in);
60+
61+
// Prompt the user to enter a string to hash
62+
System.out.print("Enter the string to hash: ");
63+
64+
// Read the input string from the user
65+
String s = scanner.nextLine();
66+
67+
// Compute the MD5 hash of the input string and print it
68+
System.out.println("Your HashCode Generated by MD5 is: " + getMd5(s));
69+
70+
// Close the Scanner object
71+
scanner.close();
72+
}
73+
}

0 commit comments

Comments
 (0)