upgrade
Some checks failed
/ test (push) Has been cancelled
/ publish (push) Has been cancelled

too tired to write a detailed commit message or split into self-explanatory commits
This commit is contained in:
2026-01-29 18:11:17 +01:00
parent f85e22311d
commit d2b901a6ff
7 changed files with 50 additions and 108 deletions

View File

@@ -7,30 +7,21 @@ import kotlin.time.Duration.Companion.seconds
import kotlin.time.Instant
class TOTP(
function: HashFunction = SHA1,
length: Int = 6,
private val period: Duration = 30.seconds,
val function: HashFunction = SHA1,
val length: Int = 6,
val period: Duration = 30.seconds,
) {
private val hotp = HOTP(function, length)
fun verify(
secret: UByteArray,
instant: Instant,
otp: String,
): Boolean {
fun verify(secret: ByteArray, instant: Instant, otp: String): Boolean {
return generate(secret, instant) == otp
}
fun generate(
secret: UByteArray,
instant: Instant,
): String {
fun generate(secret: ByteArray, instant: Instant): String {
return hotp.generate(secret, counter(instant))
}
private fun counter(
instant: Instant,
): Long {
private fun counter(instant: Instant): Long {
val ms = instant.toEpochMilliseconds()
val d = period.inWholeMilliseconds
return ms / d