· 7 years ago · Oct 02, 2018, 09:48 AM
1public static String hashHmacSha256(String key, String message) {
2 try {
3 Mac sha256Hmac = Mac.getInstance("HmacSHA256");
4 SecretKeySpec secretKey = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
5 sha256Hmac.init(secretKey);
6
7 byte[] hash = sha256Hmac.doFinal(message.getBytes());
8 return Hex.encodeHexString(hash);
9
10// return Base64.encodeBase64String(sha256Hmac.doFinal(message.getBytes("UTF-8")));
11 } catch (NoSuchAlgorithmException | InvalidKeyException | UnsupportedEncodingException e) {
12 LOGGER.error("exception occured when encording HmacSHA256 hash : "+e.getMessage());
13 return null;
14 }
15 }