Md5Util

作者: 翼徳 | 来源:发表于2018-03-16 15:21 被阅读19次

MD5 工具类

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Md5Util {

    private Md5Util() {
    }

    private static final String ALGORITHM_NAME = "MD5";

    private static final char[] HEX_CHAR_ARRAY = "0123456789ABCDEF".toCharArray();

    public static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = HEX_CHAR_ARRAY[v >>> 4];
            hexChars[j * 2 + 1] = HEX_CHAR_ARRAY[v & 0x0F];
        }
        return new String(hexChars);
    }

    public static final byte[] digest(byte[] content) {
        try {
            MessageDigest md5 = MessageDigest.getInstance(ALGORITHM_NAME);
            return md5.digest(content);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    public static final String digestHex(byte[] content) {
        return bytesToHex(digest(content));
    }

}

相关文章

网友评论

      本文标题:Md5Util

      本文链接:https://www.haomeiwen.com/subject/vefrqftx.html