Java - MD5消息摘要算法

in TCEHJava with 0 comment
package com.back.web;

import java.security.MessageDigest;

/**
 * MD5签名
 * 
 * @author Back
 *
 */
public class MD5Test {
    private static final char[] HEX_CHARS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e',
            'f' };
    private static final char[] DIGITS_LOWER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
            'e', 'f' };

    private static final char[] DIGITS_UPPER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
            'E', 'F' };

    /**
     * 
     * @param inputStr
     *            待签名字符串
     * @param charsetName
     * @return
     * @throws Exception
     */
    public static String md5Encode(String inputStr, String charsetName) throws Exception {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        byte[] byteArray = inputStr.getBytes(charsetName);
        byte[] md5Bytes = md5.digest(byteArray);

        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();
    }

    /**
     * org.springframework.util.DigestUtils 中的实现方法
     */
    public static String md5EncodeBySpringUtils(String inputStr, String charsetName) throws Exception {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        byte[] byteArray = inputStr.getBytes(charsetName);
        byte[] md5Bytes = md5.digest(byteArray);

        char chars[] = new char[32];
        for (int i = 0; i < chars.length; i = i + 2) {
            byte b = md5Bytes[i / 2];
            chars[i] = HEX_CHARS[(b >>> 0x4) & 0xf];
            chars[i + 1] = HEX_CHARS[b & 0xf];
        }
        return new String(chars);
    }

    /**
     * org.apache.commons.codec.digest.DigestUtils 中的实现方法
     */
    public static String md5EncodeByApacheUtils(String inputStr, String charsetName, boolean toLowerCase)
            throws Exception {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        byte[] byteArray = inputStr.getBytes(charsetName);
        byte[] md5Bytes = md5.digest(byteArray);

        final int l = md5Bytes.length;
        final char[] out = new char[l << 1];
        char[] toDigits = DIGITS_UPPER;
        if (toLowerCase) {
            toDigits = DIGITS_LOWER;
        }
        for (int i = 0, j = 0; i < l; i++) {
            out[j++] = toDigits[(0xF0 & md5Bytes[i]) >>> 4];
            out[j++] = toDigits[0x0F & md5Bytes[i]];
        }
        return new String(out);
    }

    public static void main(String[] args) throws Exception {
        System.err.println(md5Encode("123", "utf-8"));
        System.err.println(md5EncodeBySpringUtils("123", "utf-8"));
        System.err.println(md5EncodeByApacheUtils("123", "utf-8", true));
    }

}
Comments are closed.