Created
May 28, 2014 16:44
-
-
Save tsprates/edf76c8202aa8cb6c935 to your computer and use it in GitHub Desktop.
Hmac SHA512
This file contains hidden or 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
Good!