Compare commits

..
2 Commits
Author SHA1 Message Date
Infendro 633263c541 add opt-in 2025-07-24 21:07:38 +02:00
Infendro ba1c983094 update dependencies 2025-07-24 21:07:32 +02:00
6 changed files with 19 additions and 32 deletions

No files matched your search

+7 -1
View File
@@ -20,10 +20,16 @@ kotlin {
sourceSets { sourceSets {
commonMain.dependencies { commonMain.dependencies {
implementation(libs.hash) implementation(libs.random)
implementation(libs.mac) implementation(libs.mac)
implementation(libs.hash)
implementation(libs.bytearray)
} }
} }
compilerOptions {
freeCompilerArgs.add("-opt-in=kotlin.time.ExperimentalTime")
}
} }
publishing { publishing {
+7 -3
View File
@@ -1,11 +1,15 @@
[versions] [versions]
kotlin = "2.1.20" kotlin = "2.1.20"
hash = "1.1.0" random = "1.0.0"
mac = "1.1.0" mac = "1.1.2"
hash = "1.2.2"
bytearray = "1.1.1"
[plugins] [plugins]
multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" } multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
[libraries] [libraries]
hash = { module = "com.infendro:hash", version.ref = "hash" } random = { module = "com.infendro:random", version.ref = "random" }
mac = { module = "com.infendro:mac", version.ref = "mac" } mac = { module = "com.infendro:mac", version.ref = "mac" }
hash = { module = "com.infendro:hash", version.ref = "hash" }
bytearray = { module = "com.infendro:bytearray", version.ref = "bytearray" }
@@ -1,10 +1,10 @@
package com.infendro.otp package com.infendro.otp
import com.infendro.bytearray.toByteArray
import com.infendro.bytearray.toInt
import com.infendro.hash.HashFunction import com.infendro.hash.HashFunction
import com.infendro.hash.SHA1 import com.infendro.hash.SHA1
import com.infendro.mac.HMAC import com.infendro.mac.HMAC
import com.infendro.otp.util.toByteArray
import com.infendro.otp.util.toInt
import kotlin.experimental.and import kotlin.experimental.and
import kotlin.math.pow import kotlin.math.pow
@@ -24,8 +24,7 @@ class HOTP(
secret: ByteArray, secret: ByteArray,
counter: Long, counter: Long,
): String { ): String {
if (counter < 0) if (counter < 0) throw Exception()
throw Exception()
val hash = generateHash( val hash = generateHash(
secret, secret,
@@ -1,10 +1,10 @@
package com.infendro.otp package com.infendro.otp
import com.infendro.hash.* import com.infendro.hash.*
import kotlin.random.Random import com.infendro.random.PRNG
object SecretGenerator { object SecretGenerator {
private val random = Random.Default private val random = PRNG.HMAC
fun generate( fun generate(
algorithm: HashFunction = SHA1, algorithm: HashFunction = SHA1,
@@ -4,10 +4,8 @@ import com.infendro.hash.HashFunction
import com.infendro.hash.SHA1 import com.infendro.hash.SHA1
import kotlin.time.Duration import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds import kotlin.time.Duration.Companion.seconds
import kotlin.time.ExperimentalTime
import kotlin.time.Instant import kotlin.time.Instant
@ExperimentalTime
class TOTP( class TOTP(
function: HashFunction = SHA1, function: HashFunction = SHA1,
length: Int = 6, length: Int = 6,
@@ -1,20 +0,0 @@
package com.infendro.otp.util
internal fun Long.toByteArray(): ByteArray {
return ByteArray(8) { i ->
val offset = (7 - i) * 8
(this shr offset).toByte()
}
}
internal fun ByteArray.toInt(): Int {
if (size != 4) throw Exception()
return fold(0) { acc, byte ->
(acc shl 8) or (byte.toInt() and 0xFF)
}
}
internal fun Collection<Byte>.toInt(): Int {
return toByteArray().toInt()
}