1234567891011121314151617 |
- package com.chameth.yaotp.algos
-
- import java.nio.ByteBuffer
- import kotlin.experimental.and
- import kotlin.math.pow
-
- data class HotpParams(val key: ByteArray, val length: Int = 6, val hmacFunc: HmacFunc = ::hmacSha1)
-
- fun hotp(params: HotpParams, counter: ByteArray) = newOtp(hmacToHotp(params.hmacFunc(params.key, counter), params.length), params.length)
-
- internal fun offsetValue(bytes: ByteArray) = (bytes[bytes.size - 1] and 0x0F).toInt()
-
- internal fun offset(bytes: ByteArray) = offsetValue(bytes).let { bytes.sliceArray(IntRange(it, it + 3)) }
-
- internal fun mask(bytes: ByteArray) = ByteArray(bytes.size) { pos -> if (pos == 0) bytes[pos] and 0x7f else bytes[pos] }
-
- internal fun hmacToHotp(bytes: ByteArray, length: Int = 6) = (ByteBuffer.wrap(mask(offset(bytes))).int % 10.0.pow(length)).toInt()
|