ab
2024-11-05 bead00668eebce8d39d027515d564376de2f5978
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
package com.nova.sankuai.security;
 
import io.swagger.annotations.ApiModel;
 
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
 
/**
 * <p>
 * MD5加密
 * </p>
 *
 * @author: zcj  2022/2/15
 */
@ApiModel(value = "MD5加密")
public class MD5Encryption {
 
    public static final String[] STR_DIGITS = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
 
 
    /**
     * 手机号MD5加密
     * @param phone
     * @return 32位小写
     */
    public static String getMD5(String phone) {
        StringBuffer phoneSB = new StringBuffer();
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] digest = md.digest(phone.getBytes());
            for (byte b : digest) {
                phoneSB.append(byteToArrayString(b));
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return phoneSB.toString();
    }
 
    private static String byteToArrayString(byte b) {
        int iRet = b;
        if (iRet < 0) {
            iRet += 256;
        }
        int iD1 = iRet / 16;
        int iD2 = iRet % 16;
        return STR_DIGITS[iD1] + STR_DIGITS[iD2];
    }
}