[Java的写法]之MD5对字符串签名操作

对字符串签名后,1:长字符串变为32位字符:aacfbe08d042fddd8ee778b148efc923 2 : 只要长字符串内容不变,签名后得到的32位字符不变。适合用来做ID等。 private String genKeyId(String keyStr) { return Md5Utils.getStringMD5(keyStr); }

Md5Utils 类如下:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.lang3.StringUtils;

public class Md5Utils {
    protected static char[] hexDigits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    protected static MessageDigest messagedigest = null;

    public Md5Utils() {      }
    static {
        try {
            messagedigest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException var1) {
            var1.printStackTrace();
        }

    }

    public static String getFileMD5String(File file) throws IOException {
        InputStream fis = new FileInputStream(file);
        byte[] buffer = new byte[1024];
        boolean var3 = false;

        int numRead;
        while((numRead = fis.read(buffer)) > 0) {
            messagedigest.update(buffer, 0, numRead);
        }

        fis.close();
        return bufferToHex(messagedigest.digest());
    }

    public static String getStringMD5(String str) {
        if (StringUtils.isEmpty(str)) {
            return "";
        } else {
            byte[] buffer = str.getBytes();
            messagedigest.update(buffer);
            return bufferToHex(messagedigest.digest());
        }
    }

    public static String bufferToHex(byte[] bytes) {
        return bufferToHex(bytes, 0, bytes.length);
    }

    private static String bufferToHex(byte[] bytes, int m, int n) {
        StringBuffer stringbuffer = new StringBuffer(2 * n);
        int k = m + n;

        for(int l = m; l < k; ++l) {
            appendHexPair(bytes[l], stringbuffer);
        }

        return stringbuffer.toString();
    }

    private static void appendHexPair(byte bt, StringBuffer stringbuffer) {
        char c0 = hexDigits[(bt & 240) >> 4];
        char c1 = hexDigits[bt & 15];
        stringbuffer.append(c0);
        stringbuffer.append(c1);
    }

    private static final String toHex(byte[] hash) {
        if (hash == null) {
            return null;
        } else {
            StringBuffer buf = new StringBuffer(hash.length * 2);

            for(int i = 0; i < hash.length; ++i) {
                if ((hash[i] & 255) < 16) {
                    buf.append("0");
                }

                buf.append(Long.toString((long)(hash[i] & 255), 16));
            }

            return buf.toString();
        }
    }

    public static String hash(String s) {
        try {
            return new String(toHex(getStringMD5(s).getBytes("UTF-8")).getBytes("UTF-8"), "UTF-8");
        } catch (Exception var2) {
            return s;
        }
    }

    public static void main(String[] args) {
        System.out.println(getStringMD5("admin123"));
        System.out.println(hash("123456"));
    }


}

评论

暂无评论