-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMD5.java
More file actions
37 lines (32 loc) · 1.11 KB
/
MD5.java
File metadata and controls
37 lines (32 loc) · 1.11 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
package io.itjun.basic.math;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Locale;
/**
* 字符串转md5
*/
public class MD5 {
public static void main(String[] args) throws NoSuchAlgorithmException {
String password = "123456";
System.out.println(MD5.get(password));
}
public static String get(String str) {
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] bytes = md5.digest(str.getBytes());
StringBuilder builder = new StringBuilder();
for (byte b : bytes) {
int value = b & 0xff;
if (value >> 4 == 0) {// 运算符右移补0
builder.append("0").append(Integer.toHexString(value));
} else {
builder.append(Integer.toHexString(value));
}
}
return builder.toString().toLowerCase(Locale.ROOT);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
}
}