java生成HMACSHA256的方法

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("UTF-8"), "HmacSHA256");

       sha256_HMAC.init(secret_key);

       byte[] array = sha256_HMAC.doFinal(data.getBytes("UTF-8"));

       StringBuilder sb = new StringBuilder();

       for (byte item : array) {

           sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));

       }

       return sb.toString().toUpperCase();

   }

 

喜欢这篇文章?欢迎打赏~~

 

posted @ 2019-09-26 14:58  苍青浪  阅读(17970)  评论(0编辑  收藏  举报