Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Create a HMac Sha256 hash from a given string
object HMAC {
def generateHMAC(sharedSecret: String, input: String): String = {
val secret = new SecretKeySpec(sharedSecret.getBytes, "HmacSHA256") //Crypto Funs : 'SHA256' , 'HmacSHA1'
val mac = Mac.getInstance("HmacSHA256")
val hashString: Array[Byte] = mac.doFinal(input.getBytes)
// this makes sure that the string is 64chars long and gets padded with 0 if its shorter
String.format("%064x", new BigInteger(1, hashString))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment