Last active
August 29, 2015 14:00
-
-
Save pomadchin/11302083 to your computer and use it in GitHub Desktop.
hmacSha1 function
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
def hmacSha1(value: String, key: String) = { | |
// Get an hmac_sha1 key from the raw key bytes | |
val keyBytes = key.getBytes | |
val signingKey = new SecretKeySpec(keyBytes, "HmacSHA1") | |
// Get an hmac_sha1 Mac instance and initialize with the signing key | |
val mac = Mac.getInstance("HmacSHA1") | |
mac.init(signingKey) | |
// Compute the hmac on input data bytes | |
val rawHmac = mac.doFinal(value.getBytes()) | |
// Convert raw bytes to Hex | |
val hexBytes = new Hex().encode(rawHmac) | |
// Covert array of Hex bytes to a String | |
//new String(hexBytes, "UTF-8") -- if u want a String as a result | |
hexBytes // returns byte arr | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment