首页 > 编程语言 > 详细

java对字符串进行hmacsha256加密的方法

时间:2020-06-30 21:13:47      阅读:701      评论:0      收藏:0      [点我收藏+]

data是要加密的数据(字符串),key是密钥(字符串)。

public static String HmacSHA256(String data, String key) throws Exception {
    Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
    SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
    sha256_HMAC.init(secret_key);
    byte[] array = sha256_HMAC.doFinal(data.getBytes(StandardCharsets.UTF_8));
    StringBuilder sb = new StringBuilder();
    for (byte item : array) {
        sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
    }

    return sb.toString().toUpperCase();
}

一般应用在WebApi接口加密中。

 

"最怕你一生碌碌无为,还要安慰自己平凡可贵。"

java对字符串进行hmacsha256加密的方法

原文:https://www.cnblogs.com/yanggb/p/13211684.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!