提交 | 用户 | age
|
5c5945
|
1 |
package com.hx.mp.util; |
E |
2 |
|
|
3 |
import java.security.MessageDigest; |
|
4 |
|
|
5 |
public class MD5Util { |
|
6 |
|
|
7 |
private static String byteArrayToHexString(byte b[]) { |
|
8 |
StringBuffer resultSb = new StringBuffer(); |
|
9 |
for (int i = 0; i < b.length; i++) |
|
10 |
resultSb.append(byteToHexString(b[i])); |
|
11 |
|
|
12 |
return resultSb.toString(); |
|
13 |
} |
|
14 |
|
|
15 |
private static String byteToHexString(byte b) { |
|
16 |
int n = b; |
|
17 |
if (n < 0) |
|
18 |
n += 256; |
|
19 |
int d1 = n / 16; |
|
20 |
int d2 = n % 16; |
|
21 |
return hexDigits[d1] + hexDigits[d2]; |
|
22 |
} |
|
23 |
|
|
24 |
public static String MD5Encode(String origin, String charsetname) { |
|
25 |
String resultString = null; |
|
26 |
try { |
|
27 |
resultString = new String(origin); |
|
28 |
MessageDigest md = MessageDigest.getInstance("MD5"); |
|
29 |
if (charsetname == null || "".equals(charsetname)) |
|
30 |
resultString = byteArrayToHexString(md.digest(resultString |
|
31 |
.getBytes())); |
|
32 |
else |
|
33 |
resultString = byteArrayToHexString(md.digest(resultString |
|
34 |
.getBytes(charsetname))); |
|
35 |
} catch (Exception exception) { |
|
36 |
} |
|
37 |
return resultString; |
|
38 |
} |
|
39 |
|
|
40 |
private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5", |
|
41 |
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; |
|
42 |
|
|
43 |
} |