Skip to content

Instantly share code, notes, and snippets.

@domdorn
Created November 21, 2020 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save domdorn/383de1e8b55d091f7096f6b01644f695 to your computer and use it in GitHub Desktop.
Save domdorn/383de1e8b55d091f7096f6b01644f695 to your computer and use it in GitHub Desktop.
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")
mac.init(secret)
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