Skip to content

Instantly share code, notes, and snippets.

@pomadchin
Last active August 29, 2015 14:00
Show Gist options
  • Save pomadchin/11302083 to your computer and use it in GitHub Desktop.
Save pomadchin/11302083 to your computer and use it in GitHub Desktop.
hmacSha1 function
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