计算md5值,并转换成32位

我没有系统学习相关知识,以后如果有机会,会系统研究下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22

public static String md5_32Encrypt(byte[] bytes) {
    MessageDigest md5 = null;
    try {
        md5 = MessageDigest.getInstance("MD5");
    } catch (Exception e) {
        e.printStackTrace();
        throw new BusinessException(PICTURE_UPLOAD_WRONG);
    }

    byte[] md5Bytes = md5.digest(bytes);
    StringBuilder sb = new StringBuilder();
    for (byte md5Byte : md5Bytes) {
        int val = ((int) md5Byte) & 0xff;
        if (val < 16) {
            sb.append("0");
        }
        sb.append(Integer.toHexString(val));
    }
    return sb.toString();
}

参考资料

  1. java MD5加密(32位MD5加密,16位MD5加密后base64后URL编码,16位MD5base64编码)