Helius
2020-12-09 660fba5b40303dd661afcc2e2bd54a18d4f53c5c
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
76
77
78
package com.matrix.core.tools;
 
import java.security.MessageDigest;
 
/**
 * MD5工具类
 * 
 * @author jiangyouyao
 * @email 512061637@qq.com
 * @date 2019年2月25日
 */
public class MD5Util {
 
    /**
     * 把字节数组转成16进位制数
     * 
     * @author jiangyouyao
     * @email 512061637@qq.com
     * @date 2019年2月25日
     * @param bytes
     * @return
     */
    public static String bytesToHex(byte[] bytes) {
        StringBuffer md5str = new StringBuffer();
        // 把数组每一字节换成16进制连成md5字符串
        int digital;
        for (int i = 0; i < bytes.length; i++) {
            digital = bytes[i];
            if (digital < 0) {
                digital += 256;
            }
            if (digital < 16) {
                md5str.append("0");
            }
            md5str.append(Integer.toHexString(digital));
        }
        return md5str.toString();
    }
 
    /**
     * 把字节数组转换成md5
     * 
     * @author jiangyouyao
     * @email 512061637@qq.com
     * @date 2019年2月25日
     * @param input
     * @return
     */
    public static String bytesToMD5(byte[] input) {
        String md5str = null;
        try {
            // 创建一个提供信息摘要算法的对象,初始化为md5算法对象
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 计算后获得字节数组
            byte[] buff = md.digest(input);
            // 把数组每一字节换成16进制连成md5字符串
            md5str = bytesToHex(buff);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return md5str;
    }
 
    /**
     * 把字符转换成md5
    * @author jiangyouyao
    * @email 512061637@qq.com
    * @date 2019年2月25日
    * @param str
    * @return
     */
    public static String strToMD5(String str) {
        byte[] input = str.getBytes();
 
        return bytesToMD5(input);
    }
 
}