-
-
Save kkokey/b8be65d9e1a4edc336dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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