Skip to content

Instantly share code, notes, and snippets.

@tsprates
Created May 28, 2014 16:44
Show Gist options
  • Save tsprates/edf76c8202aa8cb6c935 to your computer and use it in GitHub Desktop.
Save tsprates/edf76c8202aa8cb6c935 to your computer and use it in GitHub Desktop.
Hmac SHA512
/**
* 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;
}
@kkokey
Copy link

kkokey commented Jul 7, 2015

Good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment