Compare commits
25 Commits
f32ebaf416
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
a2051dbd85
|
|||
|
767665180a
|
|||
|
8b0f24c6b4
|
|||
|
baf897e9e3
|
|||
|
e43fc4de86
|
|||
|
733d633753
|
|||
|
f5f073168d
|
|||
|
59f7d925a3
|
|||
|
d2b901a6ff
|
|||
|
f85e22311d
|
|||
|
37bbe35482
|
|||
|
8870703608
|
|||
|
25e6fcdeab
|
|||
|
62cbe2de3b
|
|||
|
52f0ab0661
|
|||
|
e5bb1f5a44
|
|||
|
62d5438b3b
|
|||
|
aa686cd81e
|
|||
|
2804576658
|
|||
|
49acfddde7
|
|||
|
498a5b5826
|
|||
| e8999ae4e4 | |||
|
bb5cd2878b
|
|||
|
8b9a7ec26e
|
|||
|
acc080b65e
|
@@ -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
|
||||||
40
README.md
Normal file
40
README.md
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
# otp-kt
|
||||||
|
|
||||||
|
`otp-kt` is a Kotlin Multiplatform library that provides common [One-Time Password](https://en.wikipedia.org/wiki/One-time_password) algorithms.
|
||||||
|
|
||||||
|
Supported algorithms:
|
||||||
|
- HOTP
|
||||||
|
- TOTP
|
||||||
|
|
||||||
|
## 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.3.1")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
fun main() {
|
||||||
|
// create TOTP instance
|
||||||
|
val totp = TOTP(
|
||||||
|
function = SHA1(),
|
||||||
|
length = 6,
|
||||||
|
period = 30.seconds,
|
||||||
|
)
|
||||||
|
|
||||||
|
// generate and verify OTP
|
||||||
|
val secret = "...".encodeToByteArray()
|
||||||
|
val time = System.now()
|
||||||
|
val otp = totp.generate(secret, time)
|
||||||
|
totp.verify(secret, time, otp)
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -1,62 +1,43 @@
|
|||||||
group = "com.infendro"
|
@file:OptIn(ExperimentalWasmDsl::class)
|
||||||
version = "1.1.1"
|
|
||||||
|
|
||||||
repositories {
|
import com.infendro.plugin.infendro
|
||||||
maven("https://git.infendro.com/api/packages/Infendro/maven")
|
import com.infendro.plugin.token
|
||||||
mavenCentral()
|
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
|
||||||
}
|
|
||||||
|
group = "com.infendro"
|
||||||
|
version = "1.3.1"
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
|
alias(libs.plugins.infendro)
|
||||||
alias(libs.plugins.multiplatform)
|
alias(libs.plugins.multiplatform)
|
||||||
id("maven-publish")
|
id("maven-publish")
|
||||||
}
|
}
|
||||||
|
|
||||||
kotlin {
|
kotlin {
|
||||||
jvm()
|
jvm()
|
||||||
js {
|
|
||||||
nodejs()
|
|
||||||
}
|
|
||||||
linuxX64()
|
linuxX64()
|
||||||
|
linuxArm64()
|
||||||
|
mingwX64()
|
||||||
|
macosX64()
|
||||||
|
macosArm64()
|
||||||
|
js { nodejs() }
|
||||||
|
wasmJs { nodejs() }
|
||||||
|
wasmWasi { nodejs() }
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
infendro()
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
sourceSets {
|
sourceSets {
|
||||||
commonMain.dependencies {
|
commonMain.dependencies {
|
||||||
implementation(libs.random)
|
implementation(libs.bytes)
|
||||||
implementation(libs.mac)
|
|
||||||
implementation(libs.hash)
|
implementation(libs.hash)
|
||||||
implementation(libs.bytearray)
|
implementation(libs.mac)
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
compilerOptions {
|
|
||||||
freeCompilerArgs.add("-opt-in=kotlin.time.ExperimentalTime")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
publishing {
|
|
||||||
repositories {
|
|
||||||
maven {
|
|
||||||
url = uri("https://git.infendro.com/api/packages/Infendro/maven")
|
|
||||||
authentication.create("header", HttpHeaderAuthentication::class)
|
|
||||||
credentials(HttpHeaderCredentials::class) {
|
|
||||||
val token = secret("git.token") ?: throw GradleException()
|
|
||||||
|
|
||||||
name = "Authorization"
|
|
||||||
value = "token $token"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun secret(
|
publishing.repositories {
|
||||||
key: String,
|
infendro(token)
|
||||||
): String? {
|
|
||||||
val property = key
|
|
||||||
val environment = key
|
|
||||||
.uppercase()
|
|
||||||
.replace(".", "__")
|
|
||||||
|
|
||||||
val prop = properties[property] as String?
|
|
||||||
val env = System.getenv(environment)
|
|
||||||
|
|
||||||
return prop ?: env
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
[versions]
|
[versions]
|
||||||
kotlin = "2.1.20"
|
infendro = "1.1.0"
|
||||||
random = "1.0.0"
|
kotlin = "2.3.0"
|
||||||
mac = "1.1.2"
|
bytes = "1.4.2"
|
||||||
hash = "1.2.2"
|
hash = "1.7.0"
|
||||||
bytearray = "1.1.1"
|
mac = "1.5.0"
|
||||||
|
|
||||||
[plugins]
|
[plugins]
|
||||||
|
infendro = { id = "com.infendro.plugin", version.ref = "infendro" }
|
||||||
multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
|
multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
|
||||||
|
|
||||||
[libraries]
|
[libraries]
|
||||||
random = { module = "com.infendro:random", version.ref = "random" }
|
bytes = { module = "com.infendro:bytes", version.ref = "bytes" }
|
||||||
mac = { module = "com.infendro:mac", version.ref = "mac" }
|
|
||||||
hash = { module = "com.infendro:hash", version.ref = "hash" }
|
hash = { module = "com.infendro:hash", version.ref = "hash" }
|
||||||
bytearray = { module = "com.infendro:bytearray", version.ref = "bytearray" }
|
mac = { module = "com.infendro:mac", version.ref = "mac" }
|
||||||
|
|||||||
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Binary file not shown.
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@@ -1,6 +1,6 @@
|
|||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-8.14-bin.zip
|
||||||
networkTimeout=10000
|
networkTimeout=10000
|
||||||
validateDistributionUrl=true
|
validateDistributionUrl=true
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
|
|||||||
12
gradlew
vendored
12
gradlew
vendored
@@ -15,6 +15,8 @@
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
#
|
#
|
||||||
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
#
|
||||||
|
|
||||||
##############################################################################
|
##############################################################################
|
||||||
#
|
#
|
||||||
@@ -55,7 +57,7 @@
|
|||||||
# Darwin, MinGW, and NonStop.
|
# Darwin, MinGW, and NonStop.
|
||||||
#
|
#
|
||||||
# (3) This script is generated from the Groovy template
|
# (3) This script is generated from the Groovy template
|
||||||
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
|
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
|
||||||
# within the Gradle project.
|
# within the Gradle project.
|
||||||
#
|
#
|
||||||
# You can find Gradle at https://github.com/gradle/gradle/.
|
# You can find Gradle at https://github.com/gradle/gradle/.
|
||||||
@@ -84,7 +86,7 @@ done
|
|||||||
# shellcheck disable=SC2034
|
# shellcheck disable=SC2034
|
||||||
APP_BASE_NAME=${0##*/}
|
APP_BASE_NAME=${0##*/}
|
||||||
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
|
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
|
||||||
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit
|
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit
|
||||||
|
|
||||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||||
MAX_FD=maximum
|
MAX_FD=maximum
|
||||||
@@ -112,7 +114,7 @@ case "$( uname )" in #(
|
|||||||
NONSTOP* ) nonstop=true ;;
|
NONSTOP* ) nonstop=true ;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
CLASSPATH="\\\"\\\""
|
||||||
|
|
||||||
|
|
||||||
# Determine the Java command to use to start the JVM.
|
# Determine the Java command to use to start the JVM.
|
||||||
@@ -203,7 +205,7 @@ fi
|
|||||||
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||||
|
|
||||||
# Collect all arguments for the java command:
|
# Collect all arguments for the java command:
|
||||||
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
|
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
|
||||||
# and any embedded shellness will be escaped.
|
# and any embedded shellness will be escaped.
|
||||||
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
|
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
|
||||||
# treated as '${Hostname}' itself on the command line.
|
# treated as '${Hostname}' itself on the command line.
|
||||||
@@ -211,7 +213,7 @@ DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
|||||||
set -- \
|
set -- \
|
||||||
"-Dorg.gradle.appname=$APP_BASE_NAME" \
|
"-Dorg.gradle.appname=$APP_BASE_NAME" \
|
||||||
-classpath "$CLASSPATH" \
|
-classpath "$CLASSPATH" \
|
||||||
org.gradle.wrapper.GradleWrapperMain \
|
-jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \
|
||||||
"$@"
|
"$@"
|
||||||
|
|
||||||
# Stop when "xargs" is not available.
|
# Stop when "xargs" is not available.
|
||||||
|
|||||||
26
gradlew.bat
vendored
26
gradlew.bat
vendored
@@ -13,6 +13,8 @@
|
|||||||
@rem See the License for the specific language governing permissions and
|
@rem See the License for the specific language governing permissions and
|
||||||
@rem limitations under the License.
|
@rem limitations under the License.
|
||||||
@rem
|
@rem
|
||||||
|
@rem SPDX-License-Identifier: Apache-2.0
|
||||||
|
@rem
|
||||||
|
|
||||||
@if "%DEBUG%"=="" @echo off
|
@if "%DEBUG%"=="" @echo off
|
||||||
@rem ##########################################################################
|
@rem ##########################################################################
|
||||||
@@ -43,11 +45,11 @@ set JAVA_EXE=java.exe
|
|||||||
%JAVA_EXE% -version >NUL 2>&1
|
%JAVA_EXE% -version >NUL 2>&1
|
||||||
if %ERRORLEVEL% equ 0 goto execute
|
if %ERRORLEVEL% equ 0 goto execute
|
||||||
|
|
||||||
echo.
|
echo. 1>&2
|
||||||
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
|
||||||
echo.
|
echo. 1>&2
|
||||||
echo Please set the JAVA_HOME variable in your environment to match the
|
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
|
||||||
echo location of your Java installation.
|
echo location of your Java installation. 1>&2
|
||||||
|
|
||||||
goto fail
|
goto fail
|
||||||
|
|
||||||
@@ -57,22 +59,22 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
|||||||
|
|
||||||
if exist "%JAVA_EXE%" goto execute
|
if exist "%JAVA_EXE%" goto execute
|
||||||
|
|
||||||
echo.
|
echo. 1>&2
|
||||||
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
|
||||||
echo.
|
echo. 1>&2
|
||||||
echo Please set the JAVA_HOME variable in your environment to match the
|
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
|
||||||
echo location of your Java installation.
|
echo location of your Java installation. 1>&2
|
||||||
|
|
||||||
goto fail
|
goto fail
|
||||||
|
|
||||||
:execute
|
:execute
|
||||||
@rem Setup the command line
|
@rem Setup the command line
|
||||||
|
|
||||||
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
set CLASSPATH=
|
||||||
|
|
||||||
|
|
||||||
@rem Execute Gradle
|
@rem Execute Gradle
|
||||||
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
|
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %*
|
||||||
|
|
||||||
:end
|
:end
|
||||||
@rem End local scope for the variables with windows NT shell
|
@rem End local scope for the variables with windows NT shell
|
||||||
|
|||||||
@@ -1 +1,6 @@
|
|||||||
rootProject.name = "otp"
|
rootProject.name = "otp"
|
||||||
|
|
||||||
|
pluginManagement.repositories {
|
||||||
|
maven("https://git.infendro.com/api/packages/Infendro/maven")
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,57 +1,32 @@
|
|||||||
package com.infendro.otp
|
package com.infendro.otp
|
||||||
|
|
||||||
import com.infendro.bytearray.toByteArray
|
import com.infendro.bytes.bytearray.toInt
|
||||||
import com.infendro.bytearray.toInt
|
import com.infendro.bytes.long.toByteArray
|
||||||
import com.infendro.hash.HashFunction
|
import com.infendro.hash.HashFunction
|
||||||
import com.infendro.hash.SHA1
|
import com.infendro.hash.sha1.SHA1
|
||||||
import com.infendro.mac.HMAC
|
import com.infendro.mac.HMAC
|
||||||
|
import com.infendro.otp.util.pow
|
||||||
import kotlin.experimental.and
|
import kotlin.experimental.and
|
||||||
import kotlin.math.pow
|
|
||||||
|
|
||||||
class HOTP(
|
class HOTP(
|
||||||
val function: HashFunction = SHA1,
|
val function: HashFunction = SHA1(),
|
||||||
val length: Int = 6,
|
val length: Int = 6,
|
||||||
) {
|
) {
|
||||||
fun verify(
|
private val hmac = HMAC(function)
|
||||||
secret: ByteArray,
|
|
||||||
counter: Long,
|
fun generate(secret: ByteArray, counter: Long): String {
|
||||||
otp: String,
|
require(counter >= 0)
|
||||||
): Boolean {
|
return otp(secret, counter)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun verify(secret: ByteArray, counter: Long, otp: String): Boolean {
|
||||||
return generate(secret, counter) == otp
|
return generate(secret, counter) == otp
|
||||||
}
|
}
|
||||||
|
|
||||||
fun generate(
|
private fun otp(secret: ByteArray, counter: Long): String {
|
||||||
secret: ByteArray,
|
val hash = hmac.hash(secret, counter.toByteArray())
|
||||||
counter: Long,
|
|
||||||
): String {
|
|
||||||
if (counter < 0) throw Exception()
|
|
||||||
|
|
||||||
val hash = generateHash(
|
|
||||||
secret,
|
|
||||||
counter.toByteArray()
|
|
||||||
)
|
|
||||||
return otp(hash)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun generateHash(
|
|
||||||
secret: ByteArray,
|
|
||||||
value: ByteArray,
|
|
||||||
): ByteArray {
|
|
||||||
return HMAC(function).hash(secret, value)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun otp(
|
|
||||||
hash: ByteArray,
|
|
||||||
): String {
|
|
||||||
val offset = (hash.last() and 0xF).toInt()
|
val offset = (hash.last() and 0xF).toInt()
|
||||||
|
val otp = (hash.toInt(offset) and 0x7FFFFFFF) % 10.pow(length)
|
||||||
var truncatedHash = hash
|
return "$otp".padStart(length, '0')
|
||||||
.drop(offset)
|
|
||||||
.take(4)
|
|
||||||
.toInt() and 0x7FFFFFFF
|
|
||||||
truncatedHash %= 10.0.pow(length).toInt()
|
|
||||||
|
|
||||||
return truncatedHash.toString()
|
|
||||||
.padStart(length, '0')
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,25 +0,0 @@
|
|||||||
package com.infendro.otp
|
|
||||||
|
|
||||||
import com.infendro.hash.*
|
|
||||||
import com.infendro.random.PRNG
|
|
||||||
|
|
||||||
object SecretGenerator {
|
|
||||||
private val random = PRNG.HMAC
|
|
||||||
|
|
||||||
fun generate(
|
|
||||||
algorithm: HashFunction = SHA1,
|
|
||||||
): ByteArray {
|
|
||||||
val bytes = when (algorithm) {
|
|
||||||
MD5 -> 16
|
|
||||||
SHA1 -> 20
|
|
||||||
SHA224 -> 28
|
|
||||||
SHA256 -> 32
|
|
||||||
SHA384 -> 48
|
|
||||||
SHA512 -> 64
|
|
||||||
}
|
|
||||||
|
|
||||||
return ByteArray(bytes).also {
|
|
||||||
random.nextBytes(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,39 +1,27 @@
|
|||||||
package com.infendro.otp
|
package com.infendro.otp
|
||||||
|
|
||||||
import com.infendro.hash.HashFunction
|
import com.infendro.hash.HashFunction
|
||||||
import com.infendro.hash.SHA1
|
import com.infendro.hash.sha1.SHA1
|
||||||
import kotlin.time.Duration
|
import kotlin.time.Duration
|
||||||
import kotlin.time.Duration.Companion.seconds
|
import kotlin.time.Duration.Companion.seconds
|
||||||
import kotlin.time.Instant
|
import kotlin.time.Instant
|
||||||
|
|
||||||
class TOTP(
|
class TOTP(
|
||||||
function: HashFunction = SHA1,
|
val function: HashFunction = SHA1(),
|
||||||
length: Int = 6,
|
val length: Int = 6,
|
||||||
val period: Duration = 30.seconds,
|
val period: Duration = 30.seconds,
|
||||||
) {
|
) {
|
||||||
val hotp = HOTP(function, length)
|
private val hotp = HOTP(function, length)
|
||||||
|
|
||||||
fun verify(
|
fun generate(secret: ByteArray, instant: Instant): String {
|
||||||
secret: ByteArray,
|
return hotp.generate(secret, counter(instant))
|
||||||
instant: Instant,
|
}
|
||||||
otp: String,
|
|
||||||
): Boolean {
|
fun verify(secret: ByteArray, instant: Instant, otp: String): Boolean {
|
||||||
return generate(secret, instant) == otp
|
return generate(secret, instant) == otp
|
||||||
}
|
}
|
||||||
|
|
||||||
fun generate(
|
private fun counter(instant: Instant): Long {
|
||||||
secret: ByteArray,
|
|
||||||
instant: Instant,
|
|
||||||
): String {
|
|
||||||
return hotp.generate(
|
|
||||||
secret,
|
|
||||||
counter(instant)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun counter(
|
|
||||||
instant: Instant,
|
|
||||||
): Long {
|
|
||||||
val ms = instant.toEpochMilliseconds()
|
val ms = instant.toEpochMilliseconds()
|
||||||
val d = period.inWholeMilliseconds
|
val d = period.inWholeMilliseconds
|
||||||
return ms / d
|
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