56 lines
1.2 KiB
Markdown
56 lines
1.2 KiB
Markdown
# Kotlin OTP
|
|
|
|
This library provides various [One-Time Password](https://en.wikipedia.org/wiki/One-time_password)
|
|
implementations in Kotlin Multiplatform.
|
|
|
|
## Features
|
|
|
|
* [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)
|
|
|
|
## Installation
|
|
|
|
Add the following to your `build.gradle.kts`.
|
|
|
|
```kotlin
|
|
repositories {
|
|
maven("https://git.infendro.com/api/packages/Infendro/maven")
|
|
}
|
|
|
|
dependencies {
|
|
implementation("com.infendro:otp:1.1.1")
|
|
}
|
|
```
|
|
|
|
## Usage
|
|
|
|
```kotlin
|
|
import com.infendro.hash.SHA1
|
|
import com.infendro.otp.SecretGenerator
|
|
import com.infendro.otp.TOTP
|
|
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
|
|
val totp = TOTP(
|
|
function = SHA1,
|
|
length = 6,
|
|
period = 30.seconds,
|
|
)
|
|
|
|
// generate a secret using SHA1
|
|
val secret = SecretGenerator.generate(SHA1)
|
|
|
|
// generate an OTP
|
|
val otp = totp.generate(secret, now())
|
|
|
|
// verify an OTP
|
|
totp.verify(secret, now(), otp)
|
|
}
|
|
```
|