1 /**
2 * HMAC算法加密
3 * @param message 待加密信息
4 * @param key 密钥
5 * @return
6 */
7 public static String HmacSHA256(byte[] message, byte[] key){
8 long begin = System.currentTimeMillis();
9 try {
10 Mac hmacSha256Mac = Mac.getInstance("HMACSha256");
11 SecretKeySpec secretKey = new SecretKeySpec(key, "HMACSha256");
12 hmacSha256Mac.init(secretKey);
13 byte[] result = hmacSha256Mac.doFinal(message);
14 long end = System.currentTimeMillis();
15 return Base64.encodeBase64String(result);
16 } catch (Exception e) {
17 e.printStackTrace();
18 return "";
19 }
20 }