Compare commits
13 Commits
26cff5d331
...
develop
| Author | SHA1 | Date | |
|---|---|---|---|
|
576b0190ad
|
|||
|
8f80ceaf7f
|
|||
|
d4c316ae00
|
|||
|
124f6a21a3
|
|||
|
ab33fe3670
|
|||
|
f78b10f565
|
|||
|
8686be3346
|
|||
|
8644c79167
|
|||
|
1537dd1be3
|
|||
|
8552d3dd2e
|
|||
|
9a9543a1ff
|
|||
|
d6449e647a
|
|||
|
a43cf1467d
|
@@ -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
|
||||
29
README.md
29
README.md
@@ -1,15 +1,9 @@
|
||||
# Kotlin KDF
|
||||
# kdf.kt
|
||||
|
||||
This library provides various [Key Derivation Function](https://en.wikipedia.org/wiki/Key_derivation_function)
|
||||
implementations in Kotlin Multiplatform.
|
||||
`kdf.kt` is a Kotlin Multiplatform library that provides common [Key Derivation Functions](https://en.wikipedia.org/wiki/Key_derivation_function).
|
||||
|
||||
## Features
|
||||
|
||||
* [PBKDF2](https://en.wikipedia.org/wiki/PBKDF2)
|
||||
* Multiplatform support
|
||||
* JVM
|
||||
* JavaScript
|
||||
* Native (Linux)
|
||||
Supported algorithms:
|
||||
- PBKDF2
|
||||
|
||||
## Installation
|
||||
|
||||
@@ -21,24 +15,21 @@ repositories {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation("com.infendro:kdf:1.0.3")
|
||||
implementation("com.infendro:kdf:1.2.1")
|
||||
}
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```kotlin
|
||||
import com.infendro.hash.SHA256
|
||||
import com.infendro.kdf.PBKDF2
|
||||
|
||||
fun main() {
|
||||
// create a PBKDF2 instance using HMAC SHA256
|
||||
val kdf = PBKDF2(SHA256)
|
||||
// create a PBKDF2 instance using HMAC SHA-256
|
||||
// this example uses 32 bytes of length and 100_000 iterations
|
||||
val kdf = PBKDF2(32, 100_000, `SHA-256`())
|
||||
|
||||
// securely hash value with some salt
|
||||
// this example uses 100_000 iterations and 32 bytes of length
|
||||
val value = "password".encodeToByteArray()
|
||||
val salt = "...".encodeToByteArray()
|
||||
val hash = kdf.hash(value, salt, 100_000, 32)
|
||||
val salt = "salt".encodeToByteArray()
|
||||
val hash = kdf.hash(value, salt)
|
||||
}
|
||||
```
|
||||
|
||||
@@ -1,64 +1,46 @@
|
||||
group = "com.infendro"
|
||||
version = "1.0.3"
|
||||
@file:OptIn(ExperimentalWasmDsl::class)
|
||||
|
||||
repositories {
|
||||
maven("https://git.infendro.com/api/packages/Infendro/maven")
|
||||
mavenCentral()
|
||||
}
|
||||
import com.infendro.plugin.infendro
|
||||
import com.infendro.plugin.token
|
||||
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
|
||||
|
||||
group = "com.infendro"
|
||||
version = "1.2.1"
|
||||
|
||||
plugins {
|
||||
alias(libs.plugins.infendro)
|
||||
alias(libs.plugins.multiplatform)
|
||||
id("maven-publish")
|
||||
}
|
||||
|
||||
kotlin {
|
||||
jvm()
|
||||
js {
|
||||
nodejs()
|
||||
}
|
||||
linuxX64()
|
||||
linuxArm64()
|
||||
mingwX64()
|
||||
macosX64()
|
||||
macosArm64()
|
||||
js { nodejs() }
|
||||
wasmJs { nodejs() }
|
||||
wasmWasi { nodejs() }
|
||||
|
||||
repositories {
|
||||
infendro()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
sourceSets {
|
||||
commonMain.dependencies {
|
||||
implementation(libs.mac)
|
||||
implementation(libs.bytes)
|
||||
implementation(libs.hash)
|
||||
implementation(libs.bytearray)
|
||||
implementation(libs.mac)
|
||||
}
|
||||
commonTest.dependencies {
|
||||
implementation(libs.test)
|
||||
}
|
||||
}
|
||||
|
||||
compilerOptions {
|
||||
freeCompilerArgs.add("-opt-in=kotlin.ExperimentalUnsignedTypes")
|
||||
}
|
||||
}
|
||||
|
||||
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(
|
||||
key: String,
|
||||
): String? {
|
||||
val property = key
|
||||
val environment = key
|
||||
.uppercase()
|
||||
.replace(".", "__")
|
||||
|
||||
val prop = properties[property] as String?
|
||||
val env = System.getenv(environment)
|
||||
|
||||
return prop ?: env
|
||||
publishing.repositories {
|
||||
infendro(token)
|
||||
}
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
[versions]
|
||||
kotlin = "2.2.21"
|
||||
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]
|
||||
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" }
|
||||
test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
|
||||
|
||||
@@ -1 +1,6 @@
|
||||
rootProject.name = "kdf"
|
||||
|
||||
pluginManagement.repositories {
|
||||
maven("https://git.infendro.com/api/packages/Infendro/maven")
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
@@ -5,8 +5,11 @@ interface KDF {
|
||||
val bits: Int
|
||||
get() = bytes * 8
|
||||
|
||||
fun hash(
|
||||
value: UByteArray,
|
||||
salt: UByteArray,
|
||||
): UByteArray
|
||||
fun hashInto(value: ByteArray, salt: ByteArray, destination: ByteArray, destinationOffset: Int = 0)
|
||||
|
||||
fun hash(value: ByteArray, salt: ByteArray): ByteArray {
|
||||
val result = ByteArray(bytes)
|
||||
hashInto(value, salt, result)
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package com.infendro.kdf
|
||||
|
||||
import com.infendro.bytearray.toUByteArray
|
||||
import com.infendro.bytearray.xor
|
||||
import com.infendro.bytes.bytearray.xorWith
|
||||
import com.infendro.bytes.int.toByteArray
|
||||
import com.infendro.hash.HashFunction
|
||||
import com.infendro.kdf.util.ceilDiv
|
||||
import com.infendro.mac.HMAC
|
||||
import kotlin.math.min
|
||||
|
||||
class PBKDF2(
|
||||
override val bytes: Int,
|
||||
@@ -12,38 +12,30 @@ class PBKDF2(
|
||||
function: HashFunction,
|
||||
) : KDF {
|
||||
init {
|
||||
if (bytes < 1) throw IllegalArgumentException()
|
||||
if (iterations < 1) throw IllegalArgumentException()
|
||||
require(bytes > 0)
|
||||
require(iterations > 0)
|
||||
}
|
||||
|
||||
private val mac = HMAC(function)
|
||||
private val hmac = HMAC(function)
|
||||
private val buffer = ByteArray(hmac.bytes)
|
||||
|
||||
override fun hash(
|
||||
value: UByteArray,
|
||||
salt: UByteArray,
|
||||
): UByteArray {
|
||||
return buildList {
|
||||
var i = 1
|
||||
while (size < bytes) {
|
||||
addAll(
|
||||
f(value, salt, i)
|
||||
.sliceArray(0..<min(bytes - size, mac.bytes))
|
||||
)
|
||||
i++
|
||||
}
|
||||
}.toUByteArray()
|
||||
}
|
||||
override fun hashInto(value: ByteArray, salt: ByteArray, destination: ByteArray, destinationOffset: Int) {
|
||||
hmac.init(value)
|
||||
|
||||
private fun f(
|
||||
value: UByteArray,
|
||||
salt: UByteArray,
|
||||
iteration: Int,
|
||||
): UByteArray {
|
||||
return buildList {
|
||||
add(mac.hash(value, salt + iteration.toUByteArray()))
|
||||
for (block in 1..bytes.ceilDiv(hmac.bytes)) {
|
||||
val offset = (block - 1) * hmac.bytes
|
||||
val bytes = minOf(bytes - offset, hmac.bytes)
|
||||
|
||||
hmac.update(salt)
|
||||
hmac.update(block.toByteArray())
|
||||
hmac.digestInto(buffer)
|
||||
buffer.copyInto(destination, destinationOffset + offset, endIndex = bytes)
|
||||
repeat(iterations - 1) {
|
||||
add(mac.hash(value, last()))
|
||||
hmac.update(buffer)
|
||||
hmac.digestInto(buffer)
|
||||
val offset = destinationOffset + offset
|
||||
destination.xorWith(buffer, offset, offset + bytes)
|
||||
}
|
||||
}
|
||||
}.reduce { acc, bytes -> acc xor bytes }
|
||||
}
|
||||
}
|
||||
|
||||
4
src/commonMain/kotlin/com/infendro/kdf/util/Math.kt
Normal file
4
src/commonMain/kotlin/com/infendro/kdf/util/Math.kt
Normal file
@@ -0,0 +1,4 @@
|
||||
package com.infendro.kdf.util
|
||||
|
||||
internal fun Int.ceilDiv(n: Int): Int =
|
||||
(this + n - 1) / n
|
||||
@@ -1,118 +1,117 @@
|
||||
package com.infendro.kdf
|
||||
|
||||
import com.infendro.bytearray.encodeToUByteArray
|
||||
import com.infendro.hash.sha2.`SHA-256`
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
|
||||
class `PBKDF2 Test` {
|
||||
@Test
|
||||
fun `32 bytes, 1 iteration`() {
|
||||
val function = PBKDF2(32, 1, `SHA-256`)
|
||||
fun `32 bytes 1 iteration`() {
|
||||
val function = PBKDF2(32, 1, `SHA-256`())
|
||||
|
||||
val expected = ubyteArrayOf(
|
||||
0x12U, 0x0fU, 0xb6U, 0xcfU, 0xfcU, 0xf8U, 0xb3U, 0x2cU,
|
||||
0x43U, 0xe7U, 0x22U, 0x52U, 0x56U, 0xc4U, 0xf8U, 0x37U,
|
||||
0xa8U, 0x65U, 0x48U, 0xc9U, 0x2cU, 0xccU, 0x35U, 0x48U,
|
||||
0x08U, 0x05U, 0x98U, 0x7cU, 0xb7U, 0x0bU, 0xe1U, 0x7bU,
|
||||
val expected = byteArrayOf(
|
||||
+0x12, +0x0f, -0x4a, -0x31, -0x04, -0x08, -0x4d, +0x2c,
|
||||
+0x43, -0x19, +0x22, +0x52, +0x56, -0x3c, -0x08, +0x37,
|
||||
-0x58, +0x65, +0x48, -0x37, +0x2c, -0x34, +0x35, +0x48,
|
||||
+0x08, +0x05, -0x68, +0x7c, -0x49, +0x0b, -0x1f, +0x7b,
|
||||
)
|
||||
val actual = function.hash(
|
||||
"password".encodeToUByteArray(),
|
||||
"salt".encodeToUByteArray(),
|
||||
"password".encodeToByteArray(),
|
||||
"salt".encodeToByteArray(),
|
||||
)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `32 bytes, 2 iterations`() {
|
||||
val function = PBKDF2(32, 2, `SHA-256`)
|
||||
fun `32 bytes 2 iterations`() {
|
||||
val function = PBKDF2(32, 2, `SHA-256`())
|
||||
|
||||
val expected = ubyteArrayOf(
|
||||
0xaeU, 0x4dU, 0x0cU, 0x95U, 0xafU, 0x6bU, 0x46U, 0xd3U,
|
||||
0x2dU, 0x0aU, 0xdfU, 0xf9U, 0x28U, 0xf0U, 0x6dU, 0xd0U,
|
||||
0x2aU, 0x30U, 0x3fU, 0x8eU, 0xf3U, 0xc2U, 0x51U, 0xdfU,
|
||||
0xd6U, 0xe2U, 0xd8U, 0x5aU, 0x95U, 0x47U, 0x4cU, 0x43U,
|
||||
val expected = byteArrayOf(
|
||||
-0x52, +0x4d, +0x0c, -0x6b, -0x51, +0x6b, +0x46, -0x2d,
|
||||
+0x2d, +0x0a, -0x21, -0x07, +0x28, -0x10, +0x6d, -0x30,
|
||||
+0x2a, +0x30, +0x3f, -0x72, -0x0d, -0x3e, +0x51, -0x21,
|
||||
-0x2a, -0x1e, -0x28, +0x5a, -0x6b, +0x47, +0x4c, +0x43,
|
||||
)
|
||||
val actual = function.hash(
|
||||
"password".encodeToUByteArray(),
|
||||
"salt".encodeToUByteArray(),
|
||||
"password".encodeToByteArray(),
|
||||
"salt".encodeToByteArray(),
|
||||
)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `32 bytes, 4096 iterations`() {
|
||||
val function = PBKDF2(32, 4096, `SHA-256`)
|
||||
fun `32 bytes 4096 iterations`() {
|
||||
val function = PBKDF2(32, 4096, `SHA-256`())
|
||||
|
||||
val expected = ubyteArrayOf(
|
||||
0xc5U, 0xe4U, 0x78U, 0xd5U, 0x92U, 0x88U, 0xc8U, 0x41U,
|
||||
0xaaU, 0x53U, 0x0dU, 0xb6U, 0x84U, 0x5cU, 0x4cU, 0x8dU,
|
||||
0x96U, 0x28U, 0x93U, 0xa0U, 0x01U, 0xceU, 0x4eU, 0x11U,
|
||||
0xa4U, 0x96U, 0x38U, 0x73U, 0xaaU, 0x98U, 0x13U, 0x4aU,
|
||||
val expected = byteArrayOf(
|
||||
-0x3b, -0x1c, +0x78, -0x2b, -0x6e, -0x78, -0x38, +0x41,
|
||||
-0x56, +0x53, +0x0d, -0x4a, -0x7c, +0x5c, +0x4c, -0x73,
|
||||
-0x6a, +0x28, -0x6d, -0x60, +0x01, -0x32, +0x4e, +0x11,
|
||||
-0x5c, -0x6a, +0x38, +0x73, -0x56, -0x68, +0x13, +0x4a,
|
||||
)
|
||||
val actual = function.hash(
|
||||
"password".encodeToUByteArray(),
|
||||
"salt".encodeToUByteArray(),
|
||||
"password".encodeToByteArray(),
|
||||
"salt".encodeToByteArray(),
|
||||
)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `40 bytes, 4096 iterations`() {
|
||||
val function = PBKDF2(40, 4096, `SHA-256`)
|
||||
fun `40 bytes 4096 iterations`() {
|
||||
val function = PBKDF2(40, 4096, `SHA-256`())
|
||||
|
||||
val expected = ubyteArrayOf(
|
||||
0xc5U, 0xe4U, 0x78U, 0xd5U, 0x92U, 0x88U, 0xc8U, 0x41U,
|
||||
0xaaU, 0x53U, 0x0dU, 0xb6U, 0x84U, 0x5cU, 0x4cU, 0x8dU,
|
||||
0x96U, 0x28U, 0x93U, 0xa0U, 0x01U, 0xceU, 0x4eU, 0x11U,
|
||||
0xa4U, 0x96U, 0x38U, 0x73U, 0xaaU, 0x98U, 0x13U, 0x4aU,
|
||||
0xf7U, 0xadU, 0x98U, 0xc1U, 0xb4U, 0x58U, 0xceU, 0x3fU,
|
||||
val expected = byteArrayOf(
|
||||
-0x3b, -0x1c, +0x78, -0x2b, -0x6e, -0x78, -0x38, +0x41,
|
||||
-0x56, +0x53, +0x0d, -0x4a, -0x7c, +0x5c, +0x4c, -0x73,
|
||||
-0x6a, +0x28, -0x6d, -0x60, +0x01, -0x32, +0x4e, +0x11,
|
||||
-0x5c, -0x6a, +0x38, +0x73, -0x56, -0x68, +0x13, +0x4a,
|
||||
-0x09, -0x53, -0x68, -0x3f, -0x4c, +0x58, -0x32, +0x3f,
|
||||
)
|
||||
val actual = function.hash(
|
||||
"password".encodeToUByteArray(),
|
||||
"salt".encodeToUByteArray(),
|
||||
"password".encodeToByteArray(),
|
||||
"salt".encodeToByteArray(),
|
||||
)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `64 bytes, 1 iteration`() {
|
||||
val function = PBKDF2(64, 1, `SHA-256`)
|
||||
fun `64 bytes 1 iteration`() {
|
||||
val function = PBKDF2(64, 1, `SHA-256`())
|
||||
|
||||
val expected = ubyteArrayOf(
|
||||
0x55U, 0xacU, 0x04U, 0x6eU, 0x56U, 0xe3U, 0x08U, 0x9fU,
|
||||
0xecU, 0x16U, 0x91U, 0xc2U, 0x25U, 0x44U, 0xb6U, 0x05U,
|
||||
0xf9U, 0x41U, 0x85U, 0x21U, 0x6dU, 0xdeU, 0x04U, 0x65U,
|
||||
0xe6U, 0x8bU, 0x9dU, 0x57U, 0xc2U, 0x0dU, 0xacU, 0xbcU,
|
||||
0x49U, 0xcaU, 0x9cU, 0xccU, 0xf1U, 0x79U, 0xb6U, 0x45U,
|
||||
0x99U, 0x16U, 0x64U, 0xb3U, 0x9dU, 0x77U, 0xefU, 0x31U,
|
||||
0x7cU, 0x71U, 0xb8U, 0x45U, 0xb1U, 0xe3U, 0x0bU, 0xd5U,
|
||||
0x09U, 0x11U, 0x20U, 0x41U, 0xd3U, 0xa1U, 0x97U, 0x83U,
|
||||
val expected = byteArrayOf(
|
||||
+0x55, -0x54, +0x04, +0x6e, +0x56, -0x1d, +0x08, -0x61,
|
||||
-0x14, +0x16, -0x6f, -0x3e, +0x25, +0x44, -0x4a, +0x05,
|
||||
-0x07, +0x41, -0x7b, +0x21, +0x6d, -0x22, +0x04, +0x65,
|
||||
-0x1a, -0x75, -0x63, +0x57, -0x3e, +0x0d, -0x54, -0x44,
|
||||
+0x49, -0x36, -0x64, -0x34, -0x0f, +0x79, -0x4a, +0x45,
|
||||
-0x67, +0x16, +0x64, -0x4d, -0x63, +0x77, -0x11, +0x31,
|
||||
+0x7c, +0x71, -0x48, +0x45, -0x4f, -0x1d, +0x0b, -0x2b,
|
||||
+0x09, +0x11, +0x20, +0x41, -0x2d, -0x5f, -0x69, -0x7d,
|
||||
)
|
||||
val actual = function.hash(
|
||||
"passwd".encodeToUByteArray(),
|
||||
"salt".encodeToUByteArray(),
|
||||
"passwd".encodeToByteArray(),
|
||||
"salt".encodeToByteArray(),
|
||||
)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `64 bytes, 80,000 iterations`() {
|
||||
val function = PBKDF2(64, 80_000, `SHA-256`)
|
||||
fun `64 bytes 80000 iterations`() {
|
||||
val function = PBKDF2(64, 80_000, `SHA-256`())
|
||||
|
||||
val expected = ubyteArrayOf(
|
||||
0x4dU, 0xdcU, 0xd8U, 0xf6U, 0x0bU, 0x98U, 0xbeU, 0x21U,
|
||||
0x83U, 0x0cU, 0xeeU, 0x5eU, 0xf2U, 0x27U, 0x01U, 0xf9U,
|
||||
0x64U, 0x1aU, 0x44U, 0x18U, 0xd0U, 0x4cU, 0x04U, 0x14U,
|
||||
0xaeU, 0xffU, 0x08U, 0x87U, 0x6bU, 0x34U, 0xabU, 0x56U,
|
||||
0xa1U, 0xd4U, 0x25U, 0xa1U, 0x22U, 0x58U, 0x33U, 0x54U,
|
||||
0x9aU, 0xdbU, 0x84U, 0x1bU, 0x51U, 0xc9U, 0xb3U, 0x17U,
|
||||
0x6aU, 0x27U, 0x2bU, 0xdeU, 0xbbU, 0xa1U, 0xd0U, 0x78U,
|
||||
0x47U, 0x8fU, 0x62U, 0xb3U, 0x97U, 0xf3U, 0x3cU, 0x8dU,
|
||||
val expected = byteArrayOf(
|
||||
+0x4d, -0x24, -0x28, -0x0a, +0x0b, -0x68, -0x42, +0x21,
|
||||
-0x7d, +0x0c, -0x12, +0x5e, -0x0e, +0x27, +0x01, -0x07,
|
||||
+0x64, +0x1a, +0x44, +0x18, -0x30, +0x4c, +0x04, +0x14,
|
||||
-0x52, -0x01, +0x08, -0x79, +0x6b, +0x34, -0x55, +0x56,
|
||||
-0x5f, -0x2c, +0x25, -0x5f, +0x22, +0x58, +0x33, +0x54,
|
||||
-0x66, -0x25, -0x7c, +0x1b, +0x51, -0x37, -0x4d, +0x17,
|
||||
+0x6a, +0x27, +0x2b, -0x22, -0x45, -0x5f, -0x30, +0x78,
|
||||
+0x47, -0x71, +0x62, -0x4d, -0x69, -0x0d, +0x3c, -0x73,
|
||||
)
|
||||
val actual = function.hash(
|
||||
"Password".encodeToByteArray().toUByteArray(),
|
||||
"NaCl".encodeToByteArray().toUByteArray()
|
||||
"Password".encodeToByteArray(),
|
||||
"NaCl".encodeToByteArray(),
|
||||
)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user