sunshine
2024-11-05 1bd51ea22b75760704843d9bed886a7262bb1cb1
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
package com.nova.sankuai.domain.api.jiniu;
 
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
 
/**
 * 类描述:对密码进行加密包装处理<br/>
 * 创建人:gaoqiang<br/>
 * 创建时间:2015-4-17下午6:30:20
 */
public class EncryptUtil {
 
    /**
     * 方法描述:md5加密
     * @param inputText
     * @return
     * String
     */
    public static String md5(String inputText){
        return encrypt(inputText, "md5");
    }
 
    /**
     * 方法描述:<br/>
     * @return
     * String
     */
    private static String encrypt(String inputText, String algorithmName){
        if (inputText == null || "".equals(inputText.trim())) {
            throw new IllegalArgumentException("请输入要加密的内容");
        }
        if (algorithmName == null || "".equals(algorithmName.trim())) {
            algorithmName = "md5";
        }
        String encryptText = null;
        try {
            MessageDigest m = MessageDigest.getInstance(algorithmName);
            m.update(inputText.getBytes(StandardCharsets.UTF_8));
            return hex(m.digest());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     *
     * 方法描述:返回十六进制字符串
     * @param arr
     * @return
     * String
     */
    private static String hex(byte[] arr) {
        StringBuffer sb = new StringBuffer();
        for (byte b : arr) {
            sb.append(Integer.toHexString((b & 0xFF) | 0x100), 1, 3);
        }
        return sb.toString();
    }
 
 
 
 
}