MD5Coder.java
2.29 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.glsx.common.utils;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
public class MD5Coder {
/**
* md5加密方法
* @param str
* @return
*/
public static String encrypt(String str) {
try {
byte b[] = Encrypter.encryptMD5(str.getBytes("utf-8"));
StringBuffer buf = new StringBuffer();
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = Integer.toHexString(b[n] & 0XFF);
if (stmp.length() == 1)
buf.append("0").append(stmp);
else
buf.append(stmp);
}
return buf.toString();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public static String MD516(String sourceStr) {
String result = "";
try {
MessageDigest md = MessageDigest.getInstance("MD5");
try {
md.update(sourceStr.getBytes("GBK"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
result = buf.toString();
result=result.toString().substring(8,24);
} catch (NoSuchAlgorithmException e) {
System.out.println(e);
}
return result;
}
/**
* 获取MD5加密的sign
*
* @param params
* @return
*/
public static String getParamsMD5Sign(Map<Object, Object> params) {
StringBuffer paramsStr = new StringBuffer();
for (Map.Entry<Object, Object> param : params.entrySet()) {
paramsStr.append(param.getKey()).append(param.getValue());
}
return MD5Coder.encrypt(paramsStr.toString()).toUpperCase();
}
}