123456789101112131415161718 |
- package com.chameth.yaotp.algos
-
- import java.nio.ByteBuffer
-
- data class TotpParams(val hotpParams: HotpParams, val startTime: Long = 0, val step: Int = 30)
-
- fun totp(totpParams: TotpParams, currentTime: Long = currentTime()): Otp {
- val hotp = hotp(totpParams.hotpParams, count(totpParams.startTime, totpParams.step, currentTime).toByteArray())
- return Otp(hotp.otp, expiry(totpParams.startTime, totpParams.step, currentTime))
- }
-
- internal fun count(startTime: Long, step: Int, currentTime: Long) = (currentTime - startTime) / step
-
- internal fun currentTime() = System.currentTimeMillis() / 1000
-
- internal fun Long.toByteArray() = ByteBuffer.allocate(java.lang.Long.BYTES).putLong(this).array()
-
- internal fun expiry(startTime: Long, step: Int, currentTime: Long) = currentTime + step - (currentTime - startTime) % step
|