-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathGenerateMd5.java
35 lines (27 loc) · 1.58 KB
/
GenerateMd5.java
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
package Generate_MD5;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class GenerateMd5 {
public static void main(String[] args) throws NoSuchAlgorithmException {
System.out.println("Generate a MD5 hash");
System.out.println("\n# # # SECURITY WARNING: This code is provided for achieve # # #");
System.out.println("# # # compatibility between different programming languages. # # #");
System.out.println("# # # It is NOT SECURE - DO NOT USE THIS CODE ANY LONGER ! # # #");
System.out.println("# # # The hash algorithm MD5 is BROKEN. # # #");
System.out.println("# # # DO NOT USE THIS CODE IN PRODUCTION # # #\n");
String plaintext = "The quick brown fox jumps over the lazy dog";
System.out.println("plaintext: " + plaintext);
byte[] md5Value = calculateMd5(plaintext);
System.out.println("md5Value (hex) length: " + md5Value.length + " data: " + bytesToHex(md5Value));
}
private static byte[] calculateMd5 (String string) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("MD5");
return md.digest(string.getBytes(StandardCharsets.UTF_8));
}
private static String bytesToHex(byte[] bytes) {
StringBuffer result = new StringBuffer();
for (byte b : bytes) result.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
return result.toString();
}
}