Merge pull request '1.2.1 Release' (#7) from develop into main
All checks were successful
/ publish (push) Successful in 3m1s

Reviewed-on: Infendro/kdf-kt#7
This commit was merged in pull request #7.
This commit is contained in:
2026-03-12 10:34:03 +00:00
committed by Gitea
5 changed files with 72 additions and 69 deletions

View File

@@ -15,7 +15,7 @@ repositories {
} }
dependencies { dependencies {
implementation("com.infendro:kdf:1.2.0") implementation("com.infendro:kdf:1.2.1")
} }
``` ```
@@ -25,7 +25,7 @@ dependencies {
fun main() { fun main() {
// create a PBKDF2 instance using HMAC SHA-256 // create a PBKDF2 instance using HMAC SHA-256
// this example uses 32 bytes of length and 100_000 iterations // this example uses 32 bytes of length and 100_000 iterations
val kdf = PBKDF2(32, 100_000, `SHA-256`) val kdf = PBKDF2(32, 100_000, `SHA-256`())
// securely hash value with some salt // securely hash value with some salt
val value = "password".encodeToByteArray() val value = "password".encodeToByteArray()

View File

@@ -5,7 +5,7 @@ import com.infendro.plugin.token
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
group = "com.infendro" group = "com.infendro"
version = "1.2.0" version = "1.2.1"
plugins { plugins {
alias(libs.plugins.infendro) alias(libs.plugins.infendro)

View File

@@ -1,9 +1,9 @@
[versions] [versions]
infendro = "1.1.0" infendro = "1.1.0"
kotlin = "2.3.0" kotlin = "2.3.0"
bytes = "1.4.0" bytes = "1.4.2"
hash = "1.6.0" hash = "1.7.0"
mac = "1.4.0" mac = "1.5.0"
[plugins] [plugins]
infendro = { id = "com.infendro.plugin", version.ref = "infendro" } infendro = { id = "com.infendro.plugin", version.ref = "infendro" }

View File

@@ -1,7 +1,7 @@
package com.infendro.kdf package com.infendro.kdf
import com.infendro.bytes.bytearray.xorInto import com.infendro.bytes.bytearray.xorWith
import com.infendro.bytes.int.copyInto import com.infendro.bytes.int.toByteArray
import com.infendro.hash.HashFunction import com.infendro.hash.HashFunction
import com.infendro.kdf.util.ceilDiv import com.infendro.kdf.util.ceilDiv
import com.infendro.mac.HMAC import com.infendro.mac.HMAC
@@ -17,21 +17,24 @@ class PBKDF2(
} }
private val hmac = HMAC(function) private val hmac = HMAC(function)
private val buffer = ByteArray(hmac.bytes)
override fun hashInto(value: ByteArray, salt: ByteArray, destination: ByteArray, destinationOffset: Int) { override fun hashInto(value: ByteArray, salt: ByteArray, destination: ByteArray, destinationOffset: Int) {
for (iteration in 1..bytes.ceilDiv(hmac.bytes)) { hmac.init(value)
val offset = (iteration - 1) * hmac.bytes
val block = minOf(bytes - offset, hmac.bytes)
val s = ByteArray(salt.size + 4) for (block in 1..bytes.ceilDiv(hmac.bytes)) {
salt.copyInto(s) val offset = (block - 1) * hmac.bytes
iteration.copyInto(s, salt.size) val bytes = minOf(bytes - offset, hmac.bytes)
val buffer = hmac.hash(value, s) hmac.update(salt)
buffer.copyInto(destination, destinationOffset + offset, endIndex = block) hmac.update(block.toByteArray())
hmac.digestInto(buffer)
buffer.copyInto(destination, destinationOffset + offset, endIndex = bytes)
repeat(iterations - 1) { repeat(iterations - 1) {
hmac.hashInto(value, buffer, buffer) hmac.update(buffer)
buffer.xorInto(destination, destinationOffset + offset, endIndex = block) hmac.digestInto(buffer)
val offset = destinationOffset + offset
destination.xorWith(buffer, offset, offset + bytes)
} }
} }
} }

View File

@@ -7,14 +7,14 @@ import kotlin.test.assertContentEquals
class `PBKDF2 Test` { class `PBKDF2 Test` {
@Test @Test
fun `32 bytes 1 iteration`() { fun `32 bytes 1 iteration`() {
val function = PBKDF2(32, 1, `SHA-256`) val function = PBKDF2(32, 1, `SHA-256`())
val expected = ubyteArrayOf( val expected = byteArrayOf(
0x12U, 0x0fU, 0xb6U, 0xcfU, 0xfcU, 0xf8U, 0xb3U, 0x2cU, +0x12, +0x0f, -0x4a, -0x31, -0x04, -0x08, -0x4d, +0x2c,
0x43U, 0xe7U, 0x22U, 0x52U, 0x56U, 0xc4U, 0xf8U, 0x37U, +0x43, -0x19, +0x22, +0x52, +0x56, -0x3c, -0x08, +0x37,
0xa8U, 0x65U, 0x48U, 0xc9U, 0x2cU, 0xccU, 0x35U, 0x48U, -0x58, +0x65, +0x48, -0x37, +0x2c, -0x34, +0x35, +0x48,
0x08U, 0x05U, 0x98U, 0x7cU, 0xb7U, 0x0bU, 0xe1U, 0x7bU, +0x08, +0x05, -0x68, +0x7c, -0x49, +0x0b, -0x1f, +0x7b,
).asByteArray() )
val actual = function.hash( val actual = function.hash(
"password".encodeToByteArray(), "password".encodeToByteArray(),
"salt".encodeToByteArray(), "salt".encodeToByteArray(),
@@ -24,14 +24,14 @@ class `PBKDF2 Test` {
@Test @Test
fun `32 bytes 2 iterations`() { fun `32 bytes 2 iterations`() {
val function = PBKDF2(32, 2, `SHA-256`) val function = PBKDF2(32, 2, `SHA-256`())
val expected = ubyteArrayOf( val expected = byteArrayOf(
0xaeU, 0x4dU, 0x0cU, 0x95U, 0xafU, 0x6bU, 0x46U, 0xd3U, -0x52, +0x4d, +0x0c, -0x6b, -0x51, +0x6b, +0x46, -0x2d,
0x2dU, 0x0aU, 0xdfU, 0xf9U, 0x28U, 0xf0U, 0x6dU, 0xd0U, +0x2d, +0x0a, -0x21, -0x07, +0x28, -0x10, +0x6d, -0x30,
0x2aU, 0x30U, 0x3fU, 0x8eU, 0xf3U, 0xc2U, 0x51U, 0xdfU, +0x2a, +0x30, +0x3f, -0x72, -0x0d, -0x3e, +0x51, -0x21,
0xd6U, 0xe2U, 0xd8U, 0x5aU, 0x95U, 0x47U, 0x4cU, 0x43U, -0x2a, -0x1e, -0x28, +0x5a, -0x6b, +0x47, +0x4c, +0x43,
).asByteArray() )
val actual = function.hash( val actual = function.hash(
"password".encodeToByteArray(), "password".encodeToByteArray(),
"salt".encodeToByteArray(), "salt".encodeToByteArray(),
@@ -41,14 +41,14 @@ class `PBKDF2 Test` {
@Test @Test
fun `32 bytes 4096 iterations`() { fun `32 bytes 4096 iterations`() {
val function = PBKDF2(32, 4096, `SHA-256`) val function = PBKDF2(32, 4096, `SHA-256`())
val expected = ubyteArrayOf( val expected = byteArrayOf(
0xc5U, 0xe4U, 0x78U, 0xd5U, 0x92U, 0x88U, 0xc8U, 0x41U, -0x3b, -0x1c, +0x78, -0x2b, -0x6e, -0x78, -0x38, +0x41,
0xaaU, 0x53U, 0x0dU, 0xb6U, 0x84U, 0x5cU, 0x4cU, 0x8dU, -0x56, +0x53, +0x0d, -0x4a, -0x7c, +0x5c, +0x4c, -0x73,
0x96U, 0x28U, 0x93U, 0xa0U, 0x01U, 0xceU, 0x4eU, 0x11U, -0x6a, +0x28, -0x6d, -0x60, +0x01, -0x32, +0x4e, +0x11,
0xa4U, 0x96U, 0x38U, 0x73U, 0xaaU, 0x98U, 0x13U, 0x4aU, -0x5c, -0x6a, +0x38, +0x73, -0x56, -0x68, +0x13, +0x4a,
).asByteArray() )
val actual = function.hash( val actual = function.hash(
"password".encodeToByteArray(), "password".encodeToByteArray(),
"salt".encodeToByteArray(), "salt".encodeToByteArray(),
@@ -58,15 +58,15 @@ class `PBKDF2 Test` {
@Test @Test
fun `40 bytes 4096 iterations`() { fun `40 bytes 4096 iterations`() {
val function = PBKDF2(40, 4096, `SHA-256`) val function = PBKDF2(40, 4096, `SHA-256`())
val expected = ubyteArrayOf( val expected = byteArrayOf(
0xc5U, 0xe4U, 0x78U, 0xd5U, 0x92U, 0x88U, 0xc8U, 0x41U, -0x3b, -0x1c, +0x78, -0x2b, -0x6e, -0x78, -0x38, +0x41,
0xaaU, 0x53U, 0x0dU, 0xb6U, 0x84U, 0x5cU, 0x4cU, 0x8dU, -0x56, +0x53, +0x0d, -0x4a, -0x7c, +0x5c, +0x4c, -0x73,
0x96U, 0x28U, 0x93U, 0xa0U, 0x01U, 0xceU, 0x4eU, 0x11U, -0x6a, +0x28, -0x6d, -0x60, +0x01, -0x32, +0x4e, +0x11,
0xa4U, 0x96U, 0x38U, 0x73U, 0xaaU, 0x98U, 0x13U, 0x4aU, -0x5c, -0x6a, +0x38, +0x73, -0x56, -0x68, +0x13, +0x4a,
0xf7U, 0xadU, 0x98U, 0xc1U, 0xb4U, 0x58U, 0xceU, 0x3fU, -0x09, -0x53, -0x68, -0x3f, -0x4c, +0x58, -0x32, +0x3f,
).asByteArray() )
val actual = function.hash( val actual = function.hash(
"password".encodeToByteArray(), "password".encodeToByteArray(),
"salt".encodeToByteArray(), "salt".encodeToByteArray(),
@@ -76,18 +76,18 @@ class `PBKDF2 Test` {
@Test @Test
fun `64 bytes 1 iteration`() { fun `64 bytes 1 iteration`() {
val function = PBKDF2(64, 1, `SHA-256`) val function = PBKDF2(64, 1, `SHA-256`())
val expected = ubyteArrayOf( val expected = byteArrayOf(
0x55U, 0xacU, 0x04U, 0x6eU, 0x56U, 0xe3U, 0x08U, 0x9fU, +0x55, -0x54, +0x04, +0x6e, +0x56, -0x1d, +0x08, -0x61,
0xecU, 0x16U, 0x91U, 0xc2U, 0x25U, 0x44U, 0xb6U, 0x05U, -0x14, +0x16, -0x6f, -0x3e, +0x25, +0x44, -0x4a, +0x05,
0xf9U, 0x41U, 0x85U, 0x21U, 0x6dU, 0xdeU, 0x04U, 0x65U, -0x07, +0x41, -0x7b, +0x21, +0x6d, -0x22, +0x04, +0x65,
0xe6U, 0x8bU, 0x9dU, 0x57U, 0xc2U, 0x0dU, 0xacU, 0xbcU, -0x1a, -0x75, -0x63, +0x57, -0x3e, +0x0d, -0x54, -0x44,
0x49U, 0xcaU, 0x9cU, 0xccU, 0xf1U, 0x79U, 0xb6U, 0x45U, +0x49, -0x36, -0x64, -0x34, -0x0f, +0x79, -0x4a, +0x45,
0x99U, 0x16U, 0x64U, 0xb3U, 0x9dU, 0x77U, 0xefU, 0x31U, -0x67, +0x16, +0x64, -0x4d, -0x63, +0x77, -0x11, +0x31,
0x7cU, 0x71U, 0xb8U, 0x45U, 0xb1U, 0xe3U, 0x0bU, 0xd5U, +0x7c, +0x71, -0x48, +0x45, -0x4f, -0x1d, +0x0b, -0x2b,
0x09U, 0x11U, 0x20U, 0x41U, 0xd3U, 0xa1U, 0x97U, 0x83U, +0x09, +0x11, +0x20, +0x41, -0x2d, -0x5f, -0x69, -0x7d,
).asByteArray() )
val actual = function.hash( val actual = function.hash(
"passwd".encodeToByteArray(), "passwd".encodeToByteArray(),
"salt".encodeToByteArray(), "salt".encodeToByteArray(),
@@ -97,18 +97,18 @@ class `PBKDF2 Test` {
@Test @Test
fun `64 bytes 80000 iterations`() { fun `64 bytes 80000 iterations`() {
val function = PBKDF2(64, 80_000, `SHA-256`) val function = PBKDF2(64, 80_000, `SHA-256`())
val expected = ubyteArrayOf( val expected = byteArrayOf(
0x4dU, 0xdcU, 0xd8U, 0xf6U, 0x0bU, 0x98U, 0xbeU, 0x21U, +0x4d, -0x24, -0x28, -0x0a, +0x0b, -0x68, -0x42, +0x21,
0x83U, 0x0cU, 0xeeU, 0x5eU, 0xf2U, 0x27U, 0x01U, 0xf9U, -0x7d, +0x0c, -0x12, +0x5e, -0x0e, +0x27, +0x01, -0x07,
0x64U, 0x1aU, 0x44U, 0x18U, 0xd0U, 0x4cU, 0x04U, 0x14U, +0x64, +0x1a, +0x44, +0x18, -0x30, +0x4c, +0x04, +0x14,
0xaeU, 0xffU, 0x08U, 0x87U, 0x6bU, 0x34U, 0xabU, 0x56U, -0x52, -0x01, +0x08, -0x79, +0x6b, +0x34, -0x55, +0x56,
0xa1U, 0xd4U, 0x25U, 0xa1U, 0x22U, 0x58U, 0x33U, 0x54U, -0x5f, -0x2c, +0x25, -0x5f, +0x22, +0x58, +0x33, +0x54,
0x9aU, 0xdbU, 0x84U, 0x1bU, 0x51U, 0xc9U, 0xb3U, 0x17U, -0x66, -0x25, -0x7c, +0x1b, +0x51, -0x37, -0x4d, +0x17,
0x6aU, 0x27U, 0x2bU, 0xdeU, 0xbbU, 0xa1U, 0xd0U, 0x78U, +0x6a, +0x27, +0x2b, -0x22, -0x45, -0x5f, -0x30, +0x78,
0x47U, 0x8fU, 0x62U, 0xb3U, 0x97U, 0xf3U, 0x3cU, 0x8dU, +0x47, -0x71, +0x62, -0x4d, -0x69, -0x0d, +0x3c, -0x73,
).asByteArray() )
val actual = function.hash( val actual = function.hash(
"Password".encodeToByteArray(), "Password".encodeToByteArray(),
"NaCl".encodeToByteArray(), "NaCl".encodeToByteArray(),