MD5生成消息摘要

public class MD5Util {
    
    private static final String MD5 = "MD5";
    
    private static final int THIRTY_TWO = 32;
    
    private static final int SHIFT = 4;
    
    private static final byte BYTE_F = 0xf;
    
    private static final char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    public static String genMD5(String str, String encoding) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest md = MessageDigest.getInstance(MD5);
        md.update(str.getBytes(encoding));
        byte[] digests = md.digest();
        char[] chs = new char[THIRTY_TWO];
        int p = 0;
        for (int i = 0; i < digests.length; i++) {
            byte digest = digests[i];
            chs[p++] = hexChars[digest >>> SHIFT & BYTE_F];
            chs[p++] = hexChars[digest & BYTE_F];
        }
        return new String(chs);
    }
}

你可能感兴趣的:(java,MD5)