MD5.java
1.17 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
package com.yoho.trace.utils;
import org.apache.commons.codec.Charsets;
import org.apache.commons.codec.binary.Hex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* MD5 工具类
* Created by chzhang@yoho.cn on 2015/11/6.
*/
public class MD5 {
private static final Logger logger = LoggerFactory.getLogger(MD5.class);
/**
* 对消息计算MD5, 结果用Hex(十六进制)编码
*
* @param message 消息
* @return MD5之后的结果
*/
public static String md5(String message) {
return new String(Hex.encodeHex(md5Digest(message)));
}
/**
* 计算MD5
*
* @param message 原始消息
* @return MD5之后的记过
*/
private static byte[] md5Digest(String message) {
byte[] md5Bytes = null;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md5Bytes = md.digest(message.getBytes(Charsets.UTF_8));
} catch (NoSuchAlgorithmException e) {
logger.error("md5 error: NoSuchAlgorithmException");
}
return md5Bytes;
}
}