1234567891011121314151617 |
- package com.chameth.yaotp.algos
-
- import javax.crypto.Mac
- import javax.crypto.spec.SecretKeySpec
-
- typealias HmacFunc = (ByteArray, ByteArray) -> ByteArray
-
- fun hmacSha1(keyMaterial: ByteArray, input: ByteArray) = hmac(keyMaterial, input, "HmacSHA1")
- fun hmacSha256(keyMaterial: ByteArray, input: ByteArray) = hmac(keyMaterial, input, "HmacSHA256")
- fun hmacSha512(keyMaterial: ByteArray, input: ByteArray) = hmac(keyMaterial, input, "HmacSHA512")
-
- private fun hmac(keyMaterial: ByteArray, input: ByteArray, algorithm: String): ByteArray {
- return with(Mac.getInstance(algorithm)) {
- init(SecretKeySpec(keyMaterial, algorithm))
- doFinal(input)
- }
- }
|