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)
|
`otp-kt` is a Kotlin Multiplatform library that provides common one-time password implementations.
|
||||||
implementations in Kotlin Multiplatform.
|
|
||||||
|
|
||||||
## Features
|
Supported implementations:
|
||||||
|
- HOTP
|
||||||
|
- TOTP
|
||||||
|
|
||||||
* [HOTP](https://en.wikipedia.org/wiki/HMAC-based_one-time_password)
|
## Targets
|
||||||
* [TOTP](https://en.wikipedia.org/wiki/Time-based_one-time_password)
|
|
||||||
* Multiplatform support
|
| Target | Status |
|
||||||
* JVM
|
|------------------|---------------|
|
||||||
* JavaScript
|
| JVM | Supported |
|
||||||
* Native (Linux)
|
| JavaScript | Supported |
|
||||||
|
| Native (Linux) | Supported |
|
||||||
|
| Native (Windows) | Not Supported |
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
@@ -22,21 +25,23 @@ repositories {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation("com.infendro:otp:1.1.1")
|
implementation("com.infendro:otp:1.2.1")
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```kotlin
|
```kotlin
|
||||||
import com.infendro.hash.SHA1
|
import com.infendro.hash.sha1.SHA1
|
||||||
import com.infendro.otp.SecretGenerator
|
import com.infendro.otp.SecretGenerator
|
||||||
import com.infendro.otp.TOTP
|
import com.infendro.otp.TOTP
|
||||||
|
import com.infendro.random.csprng.CSPRNG
|
||||||
import kotlin.time.Clock.System.now
|
import kotlin.time.Clock.System.now
|
||||||
import kotlin.time.Duration.Companion.seconds
|
import kotlin.time.Duration.Companion.seconds
|
||||||
|
|
||||||
fun main() {
|
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(
|
val totp = TOTP(
|
||||||
function = SHA1,
|
function = SHA1,
|
||||||
length = 6,
|
length = 6,
|
||||||
@@ -44,7 +49,7 @@ fun main() {
|
|||||||
)
|
)
|
||||||
|
|
||||||
// generate a secret using SHA1
|
// generate a secret using SHA1
|
||||||
val secret = SecretGenerator.generate(SHA1)
|
val secret = generator.generate()
|
||||||
|
|
||||||
// generate an OTP
|
// generate an OTP
|
||||||
val otp = totp.generate(secret, now())
|
val otp = totp.generate(secret, now())
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import com.infendro.plugin.infendro
|
|||||||
import com.infendro.plugin.token
|
import com.infendro.plugin.token
|
||||||
|
|
||||||
group = "com.infendro"
|
group = "com.infendro"
|
||||||
version = "1.2.1"
|
version = "1.2.2"
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
alias(libs.plugins.infendro)
|
alias(libs.plugins.infendro)
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ import com.infendro.mac.HMAC
|
|||||||
import kotlin.math.pow
|
import kotlin.math.pow
|
||||||
|
|
||||||
class HOTP(
|
class HOTP(
|
||||||
val function: HashFunction = SHA1,
|
function: HashFunction = SHA1,
|
||||||
val length: Int = 6,
|
private val length: Int = 6,
|
||||||
) {
|
) {
|
||||||
private val hmac = HMAC(function)
|
private val hmac = HMAC(function)
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,14 @@
|
|||||||
package com.infendro.otp
|
package com.infendro.otp
|
||||||
|
|
||||||
import com.infendro.hash.HashFunction
|
import kotlin.random.Random
|
||||||
import com.infendro.random.csprng.CSPRNG
|
|
||||||
import kotlin.random.nextUBytes
|
import kotlin.random.nextUBytes
|
||||||
|
|
||||||
class SecretGenerator(
|
class SecretGenerator(
|
||||||
val function: HashFunction,
|
private val random: Random,
|
||||||
) {
|
) {
|
||||||
private val random = CSPRNG.HMAC(function)
|
fun generate(
|
||||||
|
bytes: Int = 20,
|
||||||
fun generate(): UByteArray {
|
): UByteArray {
|
||||||
return random.nextUBytes(function.bytes)
|
return random.nextUBytes(bytes)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,9 +7,9 @@ import kotlin.time.Duration.Companion.seconds
|
|||||||
import kotlin.time.Instant
|
import kotlin.time.Instant
|
||||||
|
|
||||||
class TOTP(
|
class TOTP(
|
||||||
val function: HashFunction = SHA1,
|
function: HashFunction = SHA1,
|
||||||
val length: Int = 6,
|
length: Int = 6,
|
||||||
val period: Duration = 30.seconds,
|
private val period: Duration = 30.seconds,
|
||||||
) {
|
) {
|
||||||
private val hotp = HOTP(function, length)
|
private val hotp = HOTP(function, length)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user