From d4c316ae00cdcda07dd28ef9c3af26ccc1968b00 Mon Sep 17 00:00:00 2001 From: Infendro Date: Wed, 11 Mar 2026 23:53:42 +0100 Subject: [PATCH 1/2] Upgrade dependencies --- gradle/libs.versions.toml | 6 +- .../kotlin/com/infendro/kdf/PBKDF2.kt | 27 ++--- .../kotlin/com/infendro/kdf/PBKDF2.kt | 102 +++++++++--------- 3 files changed, 69 insertions(+), 66 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 3dd3ae9..b8b2fa2 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,9 +1,9 @@ [versions] infendro = "1.1.0" kotlin = "2.3.0" -bytes = "1.4.0" -hash = "1.6.0" -mac = "1.4.0" +bytes = "1.4.2" +hash = "1.7.0" +mac = "1.5.0" [plugins] infendro = { id = "com.infendro.plugin", version.ref = "infendro" } diff --git a/src/commonMain/kotlin/com/infendro/kdf/PBKDF2.kt b/src/commonMain/kotlin/com/infendro/kdf/PBKDF2.kt index a44b6a6..ebc1320 100644 --- a/src/commonMain/kotlin/com/infendro/kdf/PBKDF2.kt +++ b/src/commonMain/kotlin/com/infendro/kdf/PBKDF2.kt @@ -1,7 +1,7 @@ package com.infendro.kdf -import com.infendro.bytes.bytearray.xorInto -import com.infendro.bytes.int.copyInto +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 @@ -17,21 +17,24 @@ class PBKDF2( } private val hmac = HMAC(function) + private val buffer = ByteArray(hmac.bytes) override fun hashInto(value: ByteArray, salt: ByteArray, destination: ByteArray, destinationOffset: Int) { - for (iteration in 1..bytes.ceilDiv(hmac.bytes)) { - val offset = (iteration - 1) * hmac.bytes - val block = minOf(bytes - offset, hmac.bytes) + hmac.init(value) - val s = ByteArray(salt.size + 4) - salt.copyInto(s) - iteration.copyInto(s, salt.size) + for (block in 1..bytes.ceilDiv(hmac.bytes)) { + val offset = (block - 1) * hmac.bytes + val bytes = minOf(bytes - offset, hmac.bytes) - val buffer = hmac.hash(value, s) - buffer.copyInto(destination, destinationOffset + offset, endIndex = block) + hmac.update(salt) + hmac.update(block.toByteArray()) + hmac.digestInto(buffer) + buffer.copyInto(destination, destinationOffset + offset, endIndex = bytes) repeat(iterations - 1) { - hmac.hashInto(value, buffer, buffer) - buffer.xorInto(destination, destinationOffset + offset, endIndex = block) + hmac.update(buffer) + hmac.digestInto(buffer) + val offset = destinationOffset + offset + destination.xorWith(buffer, offset, offset + bytes) } } } diff --git a/src/commonTest/kotlin/com/infendro/kdf/PBKDF2.kt b/src/commonTest/kotlin/com/infendro/kdf/PBKDF2.kt index ea32483..0b6fd4f 100644 --- a/src/commonTest/kotlin/com/infendro/kdf/PBKDF2.kt +++ b/src/commonTest/kotlin/com/infendro/kdf/PBKDF2.kt @@ -7,14 +7,14 @@ import kotlin.test.assertContentEquals class `PBKDF2 Test` { @Test fun `32 bytes 1 iteration`() { - val function = PBKDF2(32, 1, `SHA-256`) + 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, - ).asByteArray() + 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".encodeToByteArray(), "salt".encodeToByteArray(), @@ -24,14 +24,14 @@ class `PBKDF2 Test` { @Test fun `32 bytes 2 iterations`() { - val function = PBKDF2(32, 2, `SHA-256`) + 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, - ).asByteArray() + 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".encodeToByteArray(), "salt".encodeToByteArray(), @@ -41,14 +41,14 @@ class `PBKDF2 Test` { @Test fun `32 bytes 4096 iterations`() { - val function = PBKDF2(32, 4096, `SHA-256`) + 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, - ).asByteArray() + 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".encodeToByteArray(), "salt".encodeToByteArray(), @@ -58,15 +58,15 @@ class `PBKDF2 Test` { @Test fun `40 bytes 4096 iterations`() { - val function = PBKDF2(40, 4096, `SHA-256`) + 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, - ).asByteArray() + 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".encodeToByteArray(), "salt".encodeToByteArray(), @@ -76,18 +76,18 @@ class `PBKDF2 Test` { @Test fun `64 bytes 1 iteration`() { - val function = PBKDF2(64, 1, `SHA-256`) + 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, - ).asByteArray() + 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".encodeToByteArray(), "salt".encodeToByteArray(), @@ -97,18 +97,18 @@ class `PBKDF2 Test` { @Test fun `64 bytes 80000 iterations`() { - val function = PBKDF2(64, 80_000, `SHA-256`) + 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, - ).asByteArray() + 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(), "NaCl".encodeToByteArray(), -- 2.49.1 From 8f80ceaf7f5857ed0f8e7b178dffaa61d8fa21ba Mon Sep 17 00:00:00 2001 From: Infendro Date: Wed, 11 Mar 2026 23:54:11 +0100 Subject: [PATCH 2/2] Bump version to 1.2.1 --- README.md | 4 ++-- build.gradle.kts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d46eb45..9c2d1ec 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ repositories { } dependencies { - implementation("com.infendro:kdf:1.2.0") + implementation("com.infendro:kdf:1.2.1") } ``` @@ -25,7 +25,7 @@ dependencies { fun main() { // 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`) + val kdf = PBKDF2(32, 100_000, `SHA-256`()) // securely hash value with some salt val value = "password".encodeToByteArray() diff --git a/build.gradle.kts b/build.gradle.kts index 2c3eb7b..dc945fa 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -5,7 +5,7 @@ import com.infendro.plugin.token import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl group = "com.infendro" -version = "1.2.0" +version = "1.2.1" plugins { alias(libs.plugins.infendro) -- 2.49.1