Compare commits
2 Commits
e5bb1f5a44
...
62cbe2de3b
| Author | SHA1 | Date | |
|---|---|---|---|
|
62cbe2de3b
|
|||
|
52f0ab0661
|
33
README.md
33
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())
|
||||
|
||||
@@ -2,7 +2,7 @@ import com.infendro.plugin.infendro
|
||||
import com.infendro.plugin.token
|
||||
|
||||
group = "com.infendro"
|
||||
version = "1.2.1"
|
||||
version = "1.2.2"
|
||||
|
||||
plugins {
|
||||
alias(libs.plugins.infendro)
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user