无关风月
2024-08-19 5a22de857470ed18e993260e2cf3a38a1a43de7e
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package com.ruoyi.common.core.utils;
 
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
 
public class MD5Util {
 
 /**
     * 给指定字符串按照md5算法去加密
     *
     * @param psd 需要加密的密码   加盐处理
     *
     * @return md5后的字符串
     */
    public static String encoder(String psd) {
        try {
            //加盐处理
            psd = psd + "mobilesafe";
            //1,指定加密算法类型
            MessageDigest digest = MessageDigest.getInstance("MD5");
            //2,将需要加密的字符串中转换成byte类型的数组,然后进行随机哈希过程
            byte[] bs = digest.digest(psd.getBytes());
//          System.out.println(bs.length);
            //3,循环遍历bs,然后让其生成32位字符串,固定写法
            //4,拼接字符串过程
            StringBuffer stringBuffer = new StringBuffer();
            for (byte b : bs) {
                int i = b & 0xff;
                //int类型的i需要转换成16机制字符
                String hexString = Integer.toHexString(i);
//              System.out.println(hexString);
                if (hexString.length() < 2) {
                    hexString = "0" + hexString;
                }
                stringBuffer.append(hexString);
            }
            //5,打印测试
            System.out.println(stringBuffer.toString());
            return stringBuffer.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
 
    /**
     * MD5加密字符串
     *
     * @param str
     * @return
     */
    public static String getMD5(String str) {
        if (StringUtils.isNotEmpty(str)) {
            try {
                MessageDigest md = MessageDigest.getInstance("MD5");
                md.update(str.getBytes());
                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));
                }
                //32位加密(小写)
            return buf.toString();
            //32位加密(大写)
            //return buf.toString().toUpperCase();
            // 16位的加密(小写)
            //return buf.toString().substring(8, 24);
            // 16位的加密(大写)
            //return buf.toString().substring(8, 24).toUpperCase();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
        }
        return "";
    }
 
    /**
     * 获取32位大写
     *
     * @param str
     * @return
     */
    public static String getMD5_32_upper(String str) {
        if (StringUtils.isNotEmpty(str))
            return getMD5(str).toUpperCase();
        return "";
    }
 
    /**
     * 获取32位小写
     *
     * @param str
     * @return
     */
    public static String getMD5_32_lower(String str) {
        if (StringUtils.isNotEmpty(str))
            return getMD5(str).toLowerCase();
        return "";
    }
 
    /**
     * 获取16位大写
     *
     * @param str
     * @return
     */
    public static String getMD5_16_upper(String str) {
        if (StringUtils.isNotEmpty(str))
            return getMD5(str).substring(8, 24).toUpperCase();
        return "";
    }
 
    /**
     * 获取16位小写
     *
     * @param str
     * @return
     */
    public static String getMD5_16_lower(String str) {
        if (StringUtils.isNotEmpty(str))
            return getMD5(str).substring(8, 24).toLowerCase();
        return "";
    }
 
}