update dependencies, use common plugin

This commit is contained in:
2025-12-13 01:44:39 +01:00
parent e8999ae4e4
commit 498a5b5826
6 changed files with 58 additions and 83 deletions

View File

@@ -1,19 +1,20 @@
package com.infendro.otp
import com.infendro.bytearray.toByteArray
import com.infendro.bytearray.toInt
import com.infendro.bytearray.toUByteArray
import com.infendro.hash.HashFunction
import com.infendro.hash.SHA1
import com.infendro.hash.sha1.SHA1
import com.infendro.mac.HMAC
import kotlin.experimental.and
import kotlin.math.pow
class HOTP(
val function: HashFunction = SHA1,
val length: Int = 6,
) {
private val hmac = HMAC(function)
fun verify(
secret: ByteArray,
secret: UByteArray,
counter: Long,
otp: String,
): Boolean {
@@ -21,33 +22,31 @@ class HOTP(
}
fun generate(
secret: ByteArray,
secret: UByteArray,
counter: Long,
): String {
if (counter < 0) throw Exception()
if (counter < 0)
throw IllegalArgumentException()
val hash = generateHash(
secret,
counter.toByteArray()
)
val hash = generateHash(secret, counter.toUByteArray())
return otp(hash)
}
private fun generateHash(
secret: ByteArray,
value: ByteArray,
): ByteArray {
return HMAC(function).hash(secret, value)
secret: UByteArray,
value: UByteArray,
): UByteArray {
return hmac.hash(secret, value)
}
private fun otp(
hash: ByteArray,
hash: UByteArray,
): String {
val offset = (hash.last() and 0xF).toInt()
val offset = (hash.last() and 0xFU).toInt()
var truncatedHash = hash
.drop(offset)
.take(4)
.drop(offset).take(4)
.toUByteArray()
.toInt() and 0x7FFFFFFF
truncatedHash %= 10.0.pow(length).toInt()