redegsigned OTP page and show next TOTP code

This commit is contained in:
2026-09-25 06:18:09 +05:00
parent fbedc631c5
commit 369c6e4a37
7 changed files with 258 additions and 65 deletions
@@ -9,10 +9,11 @@ object Totp {
/**
* Generate a 6-digit TOTP code from a Base32-encoded secret (RFC 6238 / RFC 4226).
* Uses HmacSHA1, 30-second window, 6 digits — matching standard authenticator apps.
* [periodOffset] shifts the time window, e.g. 1 yields the next code.
*/
fun generate(base32Secret: String, digits: Int = 6, periodSeconds: Long = 30): String {
fun generate(base32Secret: String, digits: Int = 6, periodSeconds: Long = 30, periodOffset: Long = 0): String {
val key = base32Decode(base32Secret.uppercase().replace(" ", "").replace("-", ""))
val counter = System.currentTimeMillis() / 1000L / periodSeconds
val counter = System.currentTimeMillis() / 1000L / periodSeconds + periodOffset
val otp = hotp(key, counter, digits)
return otp.toString().padStart(digits, '0')
}