upgrade
too tired to write a detailed commit message or split into self-explanatory commits
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user