Compare commits
19 Commits
49acfddde7
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
a2051dbd85
|
|||
|
767665180a
|
|||
|
8b0f24c6b4
|
|||
|
baf897e9e3
|
|||
|
e43fc4de86
|
|||
|
733d633753
|
|||
|
f5f073168d
|
|||
|
59f7d925a3
|
|||
|
d2b901a6ff
|
|||
|
f85e22311d
|
|||
|
37bbe35482
|
|||
|
8870703608
|
|||
|
25e6fcdeab
|
|||
|
62cbe2de3b
|
|||
|
52f0ab0661
|
|||
|
e5bb1f5a44
|
|||
|
62d5438b3b
|
|||
|
aa686cd81e
|
|||
|
2804576658
|
@@ -1,36 +0,0 @@
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
|
||||
env:
|
||||
GIT__TOKEN: ${{ secrets.TOKEN }}
|
||||
|
||||
jobs:
|
||||
publish:
|
||||
runs-on: linux
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- uses: actions/cache/restore@v4
|
||||
with:
|
||||
key: gradle
|
||||
path: |
|
||||
~/.gradle/caches/
|
||||
~/.gradle/wrapper/
|
||||
~/.konan/
|
||||
|
||||
- uses: actions/setup-java@v4
|
||||
with:
|
||||
java-version: '24'
|
||||
distribution: 'temurin'
|
||||
|
||||
- run: ./gradlew publish
|
||||
|
||||
- uses: actions/cache/save@v4
|
||||
with:
|
||||
key: gradle
|
||||
path: |
|
||||
~/.gradle/caches/
|
||||
~/.gradle/wrapper/
|
||||
~/.konan/
|
||||
47
.gitea/workflows/publish.yaml
Normal file
47
.gitea/workflows/publish.yaml
Normal file
@@ -0,0 +1,47 @@
|
||||
on:
|
||||
push:
|
||||
branches: [ main ]
|
||||
|
||||
jobs:
|
||||
publish:
|
||||
runs-on: linux
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup Java
|
||||
uses: actions/setup-java@v4
|
||||
with:
|
||||
java-version: '24'
|
||||
distribution: 'temurin'
|
||||
|
||||
- name: Cache Gradle
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
key: gradle-${{ hashFiles('**/libs.versions.toml') }}
|
||||
restore-keys: gradle
|
||||
path: |
|
||||
~/.gradle/caches/
|
||||
~/.gradle/wrapper/
|
||||
|
||||
- name: Cache Konan
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
key: konan-${{ hashFiles('**/libs.versions.toml') }}
|
||||
restore-keys: konan
|
||||
path: |
|
||||
~/.konan/
|
||||
|
||||
- name: Cache Node
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
key: node-${{ hashFiles('**/libs.versions.toml') }}
|
||||
restore-keys: node
|
||||
path: |
|
||||
~/.gradle/nodejs
|
||||
~/.gradle/yarn
|
||||
|
||||
- name: Publish
|
||||
run: ./gradlew publish
|
||||
env:
|
||||
INFENDRO__TOKEN: ${{ secrets.TOKEN }}
|
||||
28
.gitea/workflows/test.yaml
Normal file
28
.gitea/workflows/test.yaml
Normal file
@@ -0,0 +1,28 @@
|
||||
on:
|
||||
pull_request:
|
||||
branches: [ main ]
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: linux
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup Java
|
||||
uses: actions/setup-java@v4
|
||||
with:
|
||||
java-version: '24'
|
||||
distribution: 'temurin'
|
||||
|
||||
- name: Cache Gradle
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
key: gradle-${{ hashFiles('**/libs.versions.toml') }}
|
||||
restore-keys: gradle
|
||||
path: |
|
||||
~/.gradle/caches/
|
||||
~/.gradle/wrapper/
|
||||
|
||||
- name: Test
|
||||
run: ./gradlew jvmTest
|
||||
41
README.md
41
README.md
@@ -1,16 +1,10 @@
|
||||
# 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](https://en.wikipedia.org/wiki/One-time_password) algorithms.
|
||||
|
||||
## 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)
|
||||
Supported algorithms:
|
||||
- HOTP
|
||||
- TOTP
|
||||
|
||||
## Installation
|
||||
|
||||
@@ -22,34 +16,25 @@ repositories {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation("com.infendro:otp:1.1.1")
|
||||
implementation("com.infendro:otp:1.3.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
|
||||
// create TOTP instance
|
||||
val totp = TOTP(
|
||||
function = SHA1,
|
||||
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)
|
||||
// generate and verify OTP
|
||||
val secret = "...".encodeToByteArray()
|
||||
val time = System.now()
|
||||
val otp = totp.generate(secret, time)
|
||||
totp.verify(secret, time, otp)
|
||||
}
|
||||
```
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
@file:OptIn(ExperimentalWasmDsl::class)
|
||||
|
||||
import com.infendro.plugin.infendro
|
||||
import com.infendro.plugin.token
|
||||
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
|
||||
|
||||
group = "com.infendro"
|
||||
version = "1.2.0"
|
||||
version = "1.3.1"
|
||||
|
||||
plugins {
|
||||
alias(libs.plugins.infendro)
|
||||
@@ -12,10 +15,14 @@ plugins {
|
||||
|
||||
kotlin {
|
||||
jvm()
|
||||
js {
|
||||
nodejs()
|
||||
}
|
||||
linuxX64()
|
||||
linuxArm64()
|
||||
mingwX64()
|
||||
macosX64()
|
||||
macosArm64()
|
||||
js { nodejs() }
|
||||
wasmJs { nodejs() }
|
||||
wasmWasi { nodejs() }
|
||||
|
||||
repositories {
|
||||
infendro()
|
||||
@@ -24,17 +31,11 @@ kotlin {
|
||||
|
||||
sourceSets {
|
||||
commonMain.dependencies {
|
||||
implementation(libs.prng)
|
||||
implementation(libs.mac)
|
||||
implementation(libs.bytes)
|
||||
implementation(libs.hash)
|
||||
implementation(libs.bytearray)
|
||||
implementation(libs.mac)
|
||||
}
|
||||
}
|
||||
|
||||
compilerOptions {
|
||||
freeCompilerArgs.add("-opt-in=kotlin.ExperimentalUnsignedTypes")
|
||||
freeCompilerArgs.add("-opt-in=kotlin.time.ExperimentalTime")
|
||||
}
|
||||
}
|
||||
|
||||
publishing.repositories {
|
||||
|
||||
@@ -1,17 +1,15 @@
|
||||
[versions]
|
||||
infendro = "1.0.0"
|
||||
kotlin = "2.2.21"
|
||||
prng = "1.2.0"
|
||||
mac = "1.2.0"
|
||||
hash = "1.3.2"
|
||||
bytearray = "1.2.4"
|
||||
infendro = "1.1.0"
|
||||
kotlin = "2.3.0"
|
||||
bytes = "1.4.2"
|
||||
hash = "1.7.0"
|
||||
mac = "1.5.0"
|
||||
|
||||
[plugins]
|
||||
infendro = { id = "com.infendro.plugin", version.ref = "infendro" }
|
||||
multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
|
||||
|
||||
[libraries]
|
||||
prng = { module = "com.infendro:prng", version.ref = "prng" }
|
||||
mac = { module = "com.infendro:mac", version.ref = "mac" }
|
||||
bytes = { module = "com.infendro:bytes", version.ref = "bytes" }
|
||||
hash = { module = "com.infendro:hash", version.ref = "hash" }
|
||||
bytearray = { module = "com.infendro:bytearray", version.ref = "bytearray" }
|
||||
mac = { module = "com.infendro:mac", version.ref = "mac" }
|
||||
|
||||
@@ -1,56 +1,32 @@
|
||||
package com.infendro.otp
|
||||
|
||||
import com.infendro.bytearray.toInt
|
||||
import com.infendro.bytearray.toUByteArray
|
||||
import com.infendro.bytes.bytearray.toInt
|
||||
import com.infendro.bytes.long.toByteArray
|
||||
import com.infendro.hash.HashFunction
|
||||
import com.infendro.hash.sha1.SHA1
|
||||
import com.infendro.mac.HMAC
|
||||
import kotlin.math.pow
|
||||
import com.infendro.otp.util.pow
|
||||
import kotlin.experimental.and
|
||||
|
||||
class HOTP(
|
||||
val function: HashFunction = SHA1,
|
||||
val function: HashFunction = SHA1(),
|
||||
val length: Int = 6,
|
||||
) {
|
||||
private val hmac = HMAC(function)
|
||||
|
||||
fun verify(
|
||||
secret: UByteArray,
|
||||
counter: Long,
|
||||
otp: String,
|
||||
): Boolean {
|
||||
fun generate(secret: ByteArray, counter: Long): String {
|
||||
require(counter >= 0)
|
||||
return otp(secret, counter)
|
||||
}
|
||||
|
||||
fun verify(secret: ByteArray, counter: Long, otp: String): Boolean {
|
||||
return generate(secret, counter) == otp
|
||||
}
|
||||
|
||||
fun generate(
|
||||
secret: UByteArray,
|
||||
counter: Long,
|
||||
): String {
|
||||
if (counter < 0)
|
||||
throw IllegalArgumentException()
|
||||
|
||||
val hash = generateHash(secret, counter.toUByteArray())
|
||||
return otp(hash)
|
||||
}
|
||||
|
||||
private fun generateHash(
|
||||
secret: UByteArray,
|
||||
value: UByteArray,
|
||||
): UByteArray {
|
||||
return hmac.hash(secret, value)
|
||||
}
|
||||
|
||||
private fun otp(
|
||||
hash: UByteArray,
|
||||
): String {
|
||||
val offset = (hash.last() and 0xFU).toInt()
|
||||
|
||||
var truncatedHash = hash
|
||||
.drop(offset).take(4)
|
||||
.toUByteArray()
|
||||
.toInt() and 0x7FFFFFFF
|
||||
truncatedHash %= 10.0.pow(length).toInt()
|
||||
|
||||
return truncatedHash.toString()
|
||||
.padStart(length, '0')
|
||||
private fun otp(secret: ByteArray, counter: Long): String {
|
||||
val hash = hmac.hash(secret, counter.toByteArray())
|
||||
val offset = (hash.last() and 0xF).toInt()
|
||||
val otp = (hash.toInt(offset) and 0x7FFFFFFF) % 10.pow(length)
|
||||
return "$otp".padStart(length, '0')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
package com.infendro.otp
|
||||
|
||||
import com.infendro.hash.HashFunction
|
||||
import com.infendro.random.csprng.CSPRNG
|
||||
|
||||
class SecretGenerator(
|
||||
val function: HashFunction,
|
||||
) {
|
||||
private val random = CSPRNG.HMAC(function)
|
||||
|
||||
fun generate(): ByteArray {
|
||||
return random.nextBytes(function.bytes)
|
||||
}
|
||||
}
|
||||
@@ -7,30 +7,21 @@ import kotlin.time.Duration.Companion.seconds
|
||||
import kotlin.time.Instant
|
||||
|
||||
class TOTP(
|
||||
val function: HashFunction = SHA1,
|
||||
val function: HashFunction = SHA1(),
|
||||
val length: Int = 6,
|
||||
val period: Duration = 30.seconds,
|
||||
) {
|
||||
private val hotp = HOTP(function, length)
|
||||
|
||||
fun verify(
|
||||
secret: UByteArray,
|
||||
instant: Instant,
|
||||
otp: String,
|
||||
): Boolean {
|
||||
return generate(secret, instant) == otp
|
||||
}
|
||||
|
||||
fun generate(
|
||||
secret: UByteArray,
|
||||
instant: Instant,
|
||||
): String {
|
||||
fun generate(secret: ByteArray, instant: Instant): String {
|
||||
return hotp.generate(secret, counter(instant))
|
||||
}
|
||||
|
||||
private fun counter(
|
||||
instant: Instant,
|
||||
): Long {
|
||||
fun verify(secret: ByteArray, instant: Instant, otp: String): Boolean {
|
||||
return generate(secret, instant) == otp
|
||||
}
|
||||
|
||||
private fun counter(instant: Instant): Long {
|
||||
val ms = instant.toEpochMilliseconds()
|
||||
val d = period.inWholeMilliseconds
|
||||
return ms / d
|
||||
|
||||
6
src/commonMain/kotlin/com/infendro/otp/util/Math.kt
Normal file
6
src/commonMain/kotlin/com/infendro/otp/util/Math.kt
Normal file
@@ -0,0 +1,6 @@
|
||||
package com.infendro.otp.util
|
||||
|
||||
import kotlin.math.pow
|
||||
|
||||
internal fun Int.pow(x: Int): Int =
|
||||
this.toDouble().pow(x).toInt()
|
||||
Reference in New Issue
Block a user