Skip to content

Instantly share code, notes, and snippets.

@kkokey
Forked from tsprates/HmacSHA512.java
Last active January 6, 2022 10:21
Show Gist options
  • Save kkokey/b8be65d9e1a4edc336dc to your computer and use it in GitHub Desktop.
Save kkokey/b8be65d9e1a4edc336dc to your computer and use it in GitHub Desktop.
/**
* HmacSHA512
*
* @see <http://drumcoder.co.uk/blog/2011/apr/21/calculate-hmac-hash-java/>
* @param String value
* @param String secret
* @return String
*/
private String buildHmacSignature(String value, String secret) {
String result;
try {
Mac hmacSHA512 = Mac.getInstance("HmacSHA512");
SecretKeySpec secretKeySpec = new SecretKeySpec(secret.getBytes(),
"HmacSHA512");
hmacSHA512.init(secretKeySpec);
byte[] digest = hmacSHA512.doFinal(value.getBytes());
BigInteger hash = new BigInteger(1, digest);
result = hash.toString(16);
if ((result.length() % 2) != 0) {
result = "0" + result;
}
} catch (IllegalStateException | InvalidKeyException | NoSuchAlgorithmException ex) {
throw new RuntimeException("Problemas calculando HMAC", ex);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment