From 52f0ab066140627f3dc92030960e42abb8e19efa Mon Sep 17 00:00:00 2001 From: Infendro Date: Sat, 13 Dec 2025 23:27:49 +0100 Subject: [PATCH] update README --- README.md | 33 +++++++++++-------- .../kotlin/com/infendro/otp/HOTP.kt | 4 +-- .../com/infendro/otp/SecretGenerator.kt | 13 ++++---- .../kotlin/com/infendro/otp/TOTP.kt | 6 ++-- 4 files changed, 30 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 2a901ac..198cc14 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,19 @@ -# Kotlin OTP +# otp-kt -This library provides various [One-Time Password](https://en.wikipedia.org/wiki/One-time_password) -implementations in Kotlin Multiplatform. +`otp-kt` is a Kotlin Multiplatform library that provides common one-time password implementations. -## Features +Supported implementations: +- HOTP +- TOTP -* [HOTP](https://en.wikipedia.org/wiki/HMAC-based_one-time_password) -* [TOTP](https://en.wikipedia.org/wiki/Time-based_one-time_password) -* Multiplatform support - * JVM - * JavaScript - * Native (Linux) +## Targets + +| Target | Status | +|------------------|---------------| +| JVM | Supported | +| JavaScript | Supported | +| Native (Linux) | Supported | +| Native (Windows) | Not Supported | ## Installation @@ -22,21 +25,23 @@ repositories { } dependencies { - implementation("com.infendro:otp:1.1.1") + implementation("com.infendro:otp:1.2.1") } ``` ## Usage ```kotlin -import com.infendro.hash.SHA1 +import com.infendro.hash.sha1.SHA1 import com.infendro.otp.SecretGenerator import com.infendro.otp.TOTP +import com.infendro.random.csprng.CSPRNG import kotlin.time.Clock.System.now import kotlin.time.Duration.Companion.seconds fun main() { - // create a TOTP instance using SHA1, a length of 6, and a period of 30 seconds + // create a SecretGenerator and TOTP instance + val generator = SecretGenerator(CSPRNG.HMAC(SHA1)) val totp = TOTP( function = SHA1, length = 6, @@ -44,7 +49,7 @@ fun main() { ) // generate a secret using SHA1 - val secret = SecretGenerator.generate(SHA1) + val secret = generator.generate() // generate an OTP val otp = totp.generate(secret, now()) diff --git a/src/commonMain/kotlin/com/infendro/otp/HOTP.kt b/src/commonMain/kotlin/com/infendro/otp/HOTP.kt index d0113d6..cc41940 100644 --- a/src/commonMain/kotlin/com/infendro/otp/HOTP.kt +++ b/src/commonMain/kotlin/com/infendro/otp/HOTP.kt @@ -8,8 +8,8 @@ import com.infendro.mac.HMAC import kotlin.math.pow class HOTP( - val function: HashFunction = SHA1, - val length: Int = 6, + function: HashFunction = SHA1, + private val length: Int = 6, ) { private val hmac = HMAC(function) diff --git a/src/commonMain/kotlin/com/infendro/otp/SecretGenerator.kt b/src/commonMain/kotlin/com/infendro/otp/SecretGenerator.kt index 462a93f..8aaed62 100644 --- a/src/commonMain/kotlin/com/infendro/otp/SecretGenerator.kt +++ b/src/commonMain/kotlin/com/infendro/otp/SecretGenerator.kt @@ -1,15 +1,14 @@ package com.infendro.otp -import com.infendro.hash.HashFunction -import com.infendro.random.csprng.CSPRNG +import kotlin.random.Random import kotlin.random.nextUBytes class SecretGenerator( - val function: HashFunction, + private val random: Random, ) { - private val random = CSPRNG.HMAC(function) - - fun generate(): UByteArray { - return random.nextUBytes(function.bytes) + fun generate( + bytes: Int = 20, + ): UByteArray { + return random.nextUBytes(bytes) } } diff --git a/src/commonMain/kotlin/com/infendro/otp/TOTP.kt b/src/commonMain/kotlin/com/infendro/otp/TOTP.kt index 5e1a6cc..0a22a1c 100644 --- a/src/commonMain/kotlin/com/infendro/otp/TOTP.kt +++ b/src/commonMain/kotlin/com/infendro/otp/TOTP.kt @@ -7,9 +7,9 @@ import kotlin.time.Duration.Companion.seconds import kotlin.time.Instant class TOTP( - val function: HashFunction = SHA1, - val length: Int = 6, - val period: Duration = 30.seconds, + function: HashFunction = SHA1, + length: Int = 6, + private val period: Duration = 30.seconds, ) { private val hotp = HOTP(function, length)