-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMD5LIB.java
More file actions
38 lines (31 loc) · 1.19 KB
/
MD5LIB.java
File metadata and controls
38 lines (31 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;
public class MD5LIB {
public static String getMd5(String input) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
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);
}
}
// Driver code
public static void main(String args[]) throws NoSuchAlgorithmException {
System.out.println("Enter the text:");
Scanner s = new Scanner(System.in);
String txt_ip = s.nextLine();
System.out.println("Your HashCode Generated by MD5 for " + txt_ip + "is: " + getMd5(txt_ip));
}
}