-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMD5.java
43 lines (38 loc) · 1.07 KB
/
MD5.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
36
37
38
39
40
41
42
43
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* DateTime: 2016/9/17 17:18
* 功能:
* 思路:
*/
public class MD5 {
//测试
public static void main(String[] args) {
System.out.println("md5:"+md5("YEN"));
}
/**
* 获取特定字符串的MD5码
* eg:md5("YEN") 输出:0d9962cb9239354e6da0ca55ebd0b93c
* @param str
* @return
*/
public static String md5(String str){
try {
MessageDigest md=MessageDigest.getInstance("MD5");
md.update(str.getBytes());
byte[] b=md.digest();
int temp;
StringBuffer sb=new StringBuffer("");
for ( int offset = 0; offset <b.length ; offset++ ) {
temp=b[offset];
if(temp<0) temp+=256;
if(temp<16) sb.append("0");
sb.append(Integer.toHexString(temp));
}
str=sb.toString();
} catch ( NoSuchAlgorithmException e ) {
e.printStackTrace();
}
return str;
}
}