initial commit
All checks were successful
/ publish (push) Successful in 3m9s

This commit is contained in:
2025-07-12 20:04:39 +02:00
commit 1300e1d728
14 changed files with 776 additions and 0 deletions

View File

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