欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【工具】加密方式

程序员文章站 2024-01-14 11:05:46
...
private static String md5(String tmp) {
    String result=null;
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] buf = md.digest(tmp.getBytes());
        if (buf != null && buf.length != 0) {
            StringBuilder out = new StringBuilder();
            for (byte b : buf) {
                out.append(HEX[b >> 4 & 15]).append(HEX[b & 15]);
            }
            result=out.toString();
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return result;
}
private static String hmacSHA1(String var1, String var2, String charset) {
    String result = null;
    try {
        SecretKeySpec signingKey = new SecretKeySpec(var2.getBytes(charset), "HmacSHA1");
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(signingKey);
        byte[] buf = mac.doFinal(var1.getBytes(charset));
        if (buf != null && buf.length != 0) {
            StringBuilder out = new StringBuilder();
            for (byte b : buf) {
                out.append(HEX[b >> 4 & 15]).append(HEX[b & 15]);
            }
            result = out.toString();
        }
    } catch (NoSuchAlgorithmException | InvalidKeyException | UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return result;
}
相关标签: # 工具类集合