15832144755
2022-01-06 7b4c8991dca9cf2a809a95e239d144697d3afb56
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
package com.hxzkoa.util;
 
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
 
/**
 * 采用MD5算法加密的工具类
 */
public final class Md5Util {
    private static final Log _log = LogFactory.getLog(Md5Util.class);
    /**
     * 盐/料,给传入的密码加点料, 目的是让它无法破解。
     */
    private static String SALT = "Twitter\u63A8\u51FA\u7684\u5F00\u6E90web\u5F00\u53D1\u5DE5\u5177\u5305\u9664\u4E86\u767B\u5F55\u6A21\u5757\u4E4B\u5916\u7684\u9875\u9762\u4E8B\u4EF6\u90FD\u5728\u8FD9\u4E2Ajs\u91CC\u5B9A\u4E49";
 
    public static String encodePasswd(String passwd) {
        passwd = passwd + SALT;
        try {
            MessageDigest md = MessageDigest.getInstance("md5");
            byte[] digest = md.digest(passwd.getBytes("UTF-8"));
            return toHexString(digest);
        } catch (NoSuchAlgorithmException e) {
            _log.error(e.getMessage(), e);
        } catch (UnsupportedEncodingException e) {
            _log.error(e.getMessage(), e);
        }
        return passwd;
    }
 
    public static String md5Encode(String str, String charset)
            throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest md = MessageDigest.getInstance("md5");
        byte[] digest = md.digest(str.getBytes(charset));
        return toHexString(digest);
    }
 
    public static String toHexString(byte[] byteArr) {
        StringBuilder buf = new StringBuilder();
        char[] cs = new char[2];
        for (byte b : byteArr) {
            Arrays.fill(cs, '0');
            String hex = Integer.toHexString(b & 0xff);
            System.arraycopy(hex.toCharArray(), 0, cs,
                    cs.length - hex.length(), hex.length());
            buf.append(new String(cs));
        }
        return buf.toString();
    }
 
}