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 906B

1234567891011121314151617181920212223242526
  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 getHmacFunc(name: String?): HmacFunc {
  6. return when (name?.toLowerCase()) {
  7. "sha1" -> ::hmacSha1
  8. "sha256" -> ::hmacSha256
  9. "sha512" -> ::hmacSha512
  10. else -> ::hmacSha1
  11. }
  12. }
  13. fun hmacSha1(keyMaterial: ByteArray, input: ByteArray) = hmac(keyMaterial, input, "HmacSHA1")
  14. fun hmacSha256(keyMaterial: ByteArray, input: ByteArray) = hmac(keyMaterial, input, "HmacSHA256")
  15. fun hmacSha512(keyMaterial: ByteArray, input: ByteArray) = hmac(keyMaterial, input, "HmacSHA512")
  16. private fun hmac(keyMaterial: ByteArray, input: ByteArray, algorithm: String): ByteArray {
  17. return with(Mac.getInstance(algorithm)) {
  18. init(SecretKeySpec(keyMaterial, algorithm))
  19. doFinal(input)
  20. }
  21. }