123456789101112131415 |
- package com.chameth.yaotp.algos
-
- import java.nio.ByteBuffer
- import kotlin.experimental.and
- import kotlin.math.pow
-
- fun hotp(key: ByteArray, counter: ByteArray, length: Int = 6) = hmacToHotp(hmacSha1(key, counter), length)
-
- internal fun offsetValue(bytes: ByteArray) = (bytes[19] 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()
|