Yet Another OTP generator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Hmac.kt 683B

1234567891011121314151617
  1. package com.chameth.yaotp.algos
  2. import javax.crypto.Mac
  3. import javax.crypto.spec.SecretKeySpec
  4. typealias HmacFunc = (ByteArray, ByteArray) -> ByteArray
  5. fun hmacSha1(keyMaterial: ByteArray, input: ByteArray) = hmac(keyMaterial, input, "HmacSHA1")
  6. fun hmacSha256(keyMaterial: ByteArray, input: ByteArray) = hmac(keyMaterial, input, "HmacSHA256")
  7. fun hmacSha512(keyMaterial: ByteArray, input: ByteArray) = hmac(keyMaterial, input, "HmacSHA512")
  8. private fun hmac(keyMaterial: ByteArray, input: ByteArray, algorithm: String): ByteArray {
  9. return with(Mac.getInstance(algorithm)) {
  10. init(SecretKeySpec(keyMaterial, algorithm))
  11. doFinal(input)
  12. }
  13. }