xiaoyong931011
2022-08-25 e49a408a275464cfb4718f8af35604f13767f585
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
package cc.mrbird.febs.pay.util;
 
import cc.mrbird.febs.common.exception.JPException;
import cc.mrbird.febs.pay.model.Algorithm;
import cc.mrbird.febs.pay.model.CommonConst;
import cn.hutool.core.util.StrUtil;
 
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
 
/**
 * MD5工具类
 * @author chenyf
 * @date 2018-12-15
 */
public class MD5Util {
 
    /**
     * 生成16进制的MD5字符串
     * @param str
     * @return
     */
    public static String getMD5Hex(String str) {
        return HEXUtil.encode(getMD5(str), true);
    }
 
    public static byte[] getMD5(String str) {
        MessageDigest messageDigest;
        try {
            messageDigest = MessageDigest.getInstance(Algorithm.MD5);
            messageDigest.reset();
            if (StrUtil.isNotEmpty(str)) {
                messageDigest.update(str.getBytes(CommonConst.ENCODING_UTF_8));
            }
        } catch (NoSuchAlgorithmException e) {
            throw new JPException("生成MD5信息时异常", e);
        } catch (UnsupportedEncodingException e) {
            throw new JPException("生成MD5信息时异常", e);
        }
 
        return messageDigest.digest();
    }
}