From 8535476674b9cb72dcc33e46960f6900597e9fd2 Mon Sep 17 00:00:00 2001 From: Infendro Date: Thu, 22 Jan 2026 12:18:22 +0100 Subject: [PATCH] implement destination offset --- README.md | 2 +- build.gradle.kts | 2 +- .../kotlin/com/infendro/hash/HashFunction.kt | 8 ++++---- .../kotlin/com/infendro/hash/blake/Blake-224.kt | 4 ++-- .../kotlin/com/infendro/hash/blake/Blake-256.kt | 4 ++-- .../kotlin/com/infendro/hash/blake/Blake-384.kt | 4 ++-- .../kotlin/com/infendro/hash/blake/Blake-512.kt | 4 ++-- .../kotlin/com/infendro/hash/blake2/Blake2b.kt | 4 ++-- .../kotlin/com/infendro/hash/blake2/Blake2s.kt | 4 ++-- .../kotlin/com/infendro/hash/keccak/Keccak.kt | 15 +++++++++++---- src/commonMain/kotlin/com/infendro/hash/md/MD5.kt | 4 ++-- .../kotlin/com/infendro/hash/sha1/SHA1.kt | 4 ++-- .../kotlin/com/infendro/hash/sha2/SHA-224.kt | 4 ++-- .../kotlin/com/infendro/hash/sha2/SHA-256.kt | 4 ++-- .../kotlin/com/infendro/hash/sha2/SHA-384.kt | 4 ++-- .../kotlin/com/infendro/hash/sha2/SHA-512.kt | 4 ++-- 16 files changed, 41 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 0f8f260..f5cce9f 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ repositories { } dependencies { - implementation("com.infendro:hash:1.5.0") + implementation("com.infendro:hash:1.5.1") } ``` diff --git a/build.gradle.kts b/build.gradle.kts index f427d4c..3f374f9 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.5.0" +version = "1.5.1" plugins { alias(libs.plugins.infendro) diff --git a/src/commonMain/kotlin/com/infendro/hash/HashFunction.kt b/src/commonMain/kotlin/com/infendro/hash/HashFunction.kt index 154aebd..294d0ad 100644 --- a/src/commonMain/kotlin/com/infendro/hash/HashFunction.kt +++ b/src/commonMain/kotlin/com/infendro/hash/HashFunction.kt @@ -7,15 +7,15 @@ interface HashFunction { val block: Int - fun hashInto(value: ByteArray, destination: ByteArray) + fun hashInto(value: ByteArray, destination: ByteArray, destinationOffset: Int = 0) - fun hashInto(value: ByteArray, destination: UByteArray) = + fun hashInto(value: ByteArray, destination: UByteArray, destinationOffset: Int = 0) = hashInto(value, destination.asByteArray()) - fun hashInto(value: UByteArray, destination: UByteArray) = + fun hashInto(value: UByteArray, destination: UByteArray, destinationOffset: Int = 0) = hashInto(value.asByteArray(), destination.asByteArray()) - fun hashInto(value: UByteArray, destination: ByteArray) = + fun hashInto(value: UByteArray, destination: ByteArray, destinationOffset: Int = 0) = hashInto(value.asByteArray(), destination) fun hash(value: ByteArray): ByteArray = diff --git a/src/commonMain/kotlin/com/infendro/hash/blake/Blake-224.kt b/src/commonMain/kotlin/com/infendro/hash/blake/Blake-224.kt index eec2225..b10edbd 100644 --- a/src/commonMain/kotlin/com/infendro/hash/blake/Blake-224.kt +++ b/src/commonMain/kotlin/com/infendro/hash/blake/Blake-224.kt @@ -19,7 +19,7 @@ object `Blake-224` : HashFunction { 0x64f98fa7U, 0xbefa4fa4U, ).asIntArray() - override fun hashInto(value: ByteArray, destination: ByteArray) { + override fun hashInto(value: ByteArray, destination: ByteArray, destinationOffset: Int) { val length = value.size.toULong() * 8UL val input = pad(value) val h = initial.copyOf() @@ -31,7 +31,7 @@ object `Blake-224` : HashFunction { t += minOf(length - t, 512UL).toUInt() `Blake-256`.compress(h, input, i, m, v, t) } - h.copyInto(destination, endIndex = 7) + h.copyInto(destination, destinationOffset, endIndex = 7) } private fun pad(value: ByteArray): ByteArray { diff --git a/src/commonMain/kotlin/com/infendro/hash/blake/Blake-256.kt b/src/commonMain/kotlin/com/infendro/hash/blake/Blake-256.kt index 8ef894e..fd61ca9 100644 --- a/src/commonMain/kotlin/com/infendro/hash/blake/Blake-256.kt +++ b/src/commonMain/kotlin/com/infendro/hash/blake/Blake-256.kt @@ -44,7 +44,7 @@ object `Blake-256` : HashFunction { intArrayOf(10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0), ) - override fun hashInto(value: ByteArray, destination: ByteArray) { + override fun hashInto(value: ByteArray, destination: ByteArray, destinationOffset: Int) { val length = value.size.toULong() * 8UL val input = pad(value) val h = initial.copyOf() @@ -56,7 +56,7 @@ object `Blake-256` : HashFunction { t += minOf(length - t, 512UL).toUInt() compress(h, input, i, m, v, t) } - h.copyInto(destination) + h.copyInto(destination, destinationOffset) } private fun pad(value: ByteArray): ByteArray { diff --git a/src/commonMain/kotlin/com/infendro/hash/blake/Blake-384.kt b/src/commonMain/kotlin/com/infendro/hash/blake/Blake-384.kt index 588710b..d76db8e 100644 --- a/src/commonMain/kotlin/com/infendro/hash/blake/Blake-384.kt +++ b/src/commonMain/kotlin/com/infendro/hash/blake/Blake-384.kt @@ -19,7 +19,7 @@ object `Blake-384` : HashFunction { 0xdb0c2e0d64f98fa7U, 0x47b5481dbefa4fa4U, ).asLongArray() - override fun hashInto(value: ByteArray, destination: ByteArray) { + override fun hashInto(value: ByteArray, destination: ByteArray, destinationOffset: Int) { val length = value.size.toULong() * 8UL val input = pad(value) val h = initial.copyOf() @@ -31,7 +31,7 @@ object `Blake-384` : HashFunction { t += minOf(length - t, 1024UL) `Blake-512`.compress(h, input, i, m, v, t) } - h.copyInto(destination, endIndex = 6) + h.copyInto(destination, destinationOffset, endIndex = 6) } private fun pad(value: ByteArray): ByteArray { diff --git a/src/commonMain/kotlin/com/infendro/hash/blake/Blake-512.kt b/src/commonMain/kotlin/com/infendro/hash/blake/Blake-512.kt index 7974370..1704470 100644 --- a/src/commonMain/kotlin/com/infendro/hash/blake/Blake-512.kt +++ b/src/commonMain/kotlin/com/infendro/hash/blake/Blake-512.kt @@ -44,7 +44,7 @@ object `Blake-512` : HashFunction { intArrayOf(10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0), ) - override fun hashInto(value: ByteArray, destination: ByteArray) { + override fun hashInto(value: ByteArray, destination: ByteArray, destinationOffset: Int) { val length = value.size.toULong() * 8UL val input = pad(value) val h = initial.copyOf() @@ -56,7 +56,7 @@ object `Blake-512` : HashFunction { t += minOf(length - t, 1024UL) compress(h, input, i, m, v, t) } - h.copyInto(destination) + h.copyInto(destination, destinationOffset) } private fun pad(value: ByteArray): ByteArray { diff --git a/src/commonMain/kotlin/com/infendro/hash/blake2/Blake2b.kt b/src/commonMain/kotlin/com/infendro/hash/blake2/Blake2b.kt index 7ab685c..839cd44 100644 --- a/src/commonMain/kotlin/com/infendro/hash/blake2/Blake2b.kt +++ b/src/commonMain/kotlin/com/infendro/hash/blake2/Blake2b.kt @@ -36,7 +36,7 @@ class Blake2b( intArrayOf(10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0), ) - override fun hashInto(value: ByteArray, destination: ByteArray) { + override fun hashInto(value: ByteArray, destination: ByteArray, destinationOffset: Int) { val length = value.size.toULong() val input = pad(value) val h = initial.copyOf() @@ -49,7 +49,7 @@ class Blake2b( t += minOf(length - t, 128UL) compress(h, input, i, m, v, t, t < 1024UL) } - h.toByteArray(LITTLE_ENDIAN).copyInto(destination, endIndex = bytes) + h.toByteArray(LITTLE_ENDIAN).copyInto(destination, destinationOffset, endIndex = bytes) } private fun pad(value: ByteArray): ByteArray { diff --git a/src/commonMain/kotlin/com/infendro/hash/blake2/Blake2s.kt b/src/commonMain/kotlin/com/infendro/hash/blake2/Blake2s.kt index 590e38e..5c976d8 100644 --- a/src/commonMain/kotlin/com/infendro/hash/blake2/Blake2s.kt +++ b/src/commonMain/kotlin/com/infendro/hash/blake2/Blake2s.kt @@ -36,7 +36,7 @@ class Blake2s( intArrayOf(10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0), ) - override fun hashInto(value: ByteArray, destination: ByteArray) { + override fun hashInto(value: ByteArray, destination: ByteArray, destinationOffset: Int) { val length = value.size.toULong() val input = pad(value) val h = initial.copyOf() @@ -49,7 +49,7 @@ class Blake2s( t += minOf(length - t, 64UL).toUInt() compress(h, input, i, m, v, t, t < 1024UL) } - h.toByteArray(LITTLE_ENDIAN).copyInto(destination, endIndex = bytes) + h.toByteArray(LITTLE_ENDIAN).copyInto(destination, destinationOffset, endIndex = bytes) } private fun pad(value: ByteArray): ByteArray { diff --git a/src/commonMain/kotlin/com/infendro/hash/keccak/Keccak.kt b/src/commonMain/kotlin/com/infendro/hash/keccak/Keccak.kt index 91520c0..63ee801 100644 --- a/src/commonMain/kotlin/com/infendro/hash/keccak/Keccak.kt +++ b/src/commonMain/kotlin/com/infendro/hash/keccak/Keccak.kt @@ -36,7 +36,7 @@ open class Keccak( intArrayOf(27, 20, 39, 8, 14), ) - override fun hashInto(value: ByteArray, destination: ByteArray) { + override fun hashInto(value: ByteArray, destination: ByteArray, destinationOffset: Int) { val input = pad(value) val state = LongArray(25) val w = LongArray(block / 8) @@ -48,7 +48,7 @@ open class Keccak( absorb(state, input, i, w) permute(state, b, c, d) } - squeeze(state, b, c, d, destination) + squeeze(state, b, c, d, destination, destinationOffset) } private fun pad(value: ByteArray): ByteArray { @@ -67,7 +67,14 @@ open class Keccak( } } - private fun squeeze(state: LongArray, b: LongArray, c: LongArray, d: LongArray, destination: ByteArray) { + private fun squeeze( + state: LongArray, + b: LongArray, + c: LongArray, + d: LongArray, + destination: ByteArray, + destinationOffset: Int, + ) { val buffer = ByteArray(8) val threshold = rate / 8 @@ -81,7 +88,7 @@ open class Keccak( val length = minOf(bytes - consumed, 8) state[i].copyInto(buffer, order = LITTLE_ENDIAN) - buffer.copyInto(destination, consumed, endIndex = length) + buffer.copyInto(destination, destinationOffset + consumed, endIndex = length) consumed += length i++ diff --git a/src/commonMain/kotlin/com/infendro/hash/md/MD5.kt b/src/commonMain/kotlin/com/infendro/hash/md/MD5.kt index 846ed1c..ff3f8f4 100644 --- a/src/commonMain/kotlin/com/infendro/hash/md/MD5.kt +++ b/src/commonMain/kotlin/com/infendro/hash/md/MD5.kt @@ -49,7 +49,7 @@ object MD5 : HashFunction { 6, 10, 15, 21, 6, 10, 15, 21, ) - override fun hashInto(value: ByteArray, destination: ByteArray) { + override fun hashInto(value: ByteArray, destination: ByteArray, destinationOffset: Int) { val input = pad(value) val h = initial.copyOf() val w = IntArray(16) @@ -58,7 +58,7 @@ object MD5 : HashFunction { for (i in input.indices step block) { process(h, input, i, w, v) } - h.copyInto(destination, order = LITTLE_ENDIAN) + h.copyInto(destination, destinationOffset, order = LITTLE_ENDIAN) } private fun pad(value: ByteArray): ByteArray { diff --git a/src/commonMain/kotlin/com/infendro/hash/sha1/SHA1.kt b/src/commonMain/kotlin/com/infendro/hash/sha1/SHA1.kt index 7d1b356..5b54668 100644 --- a/src/commonMain/kotlin/com/infendro/hash/sha1/SHA1.kt +++ b/src/commonMain/kotlin/com/infendro/hash/sha1/SHA1.kt @@ -24,7 +24,7 @@ object SHA1 : HashFunction { 0x8f1bbcdcU, 0xca62c1d6U, ).asIntArray() - override fun hashInto(value: ByteArray, destination: ByteArray) { + override fun hashInto(value: ByteArray, destination: ByteArray, destinationOffset: Int) { val input = pad(value) val h = initial.copyOf() val w = IntArray(80) @@ -33,7 +33,7 @@ object SHA1 : HashFunction { for (i in input.indices step block) { process(h, input, i, w, v) } - h.copyInto(destination) + h.copyInto(destination, destinationOffset) } private fun pad(value: ByteArray): ByteArray { diff --git a/src/commonMain/kotlin/com/infendro/hash/sha2/SHA-224.kt b/src/commonMain/kotlin/com/infendro/hash/sha2/SHA-224.kt index d2807b6..01afd7d 100644 --- a/src/commonMain/kotlin/com/infendro/hash/sha2/SHA-224.kt +++ b/src/commonMain/kotlin/com/infendro/hash/sha2/SHA-224.kt @@ -17,7 +17,7 @@ object `SHA-224` : HashFunction { 0x64f98fa7U, 0xbefa4fa4U, ).asIntArray() - override fun hashInto(value: ByteArray, destination: ByteArray) { + override fun hashInto(value: ByteArray, destination: ByteArray, destinationOffset: Int) { val input = `SHA-256`.pad(value) val h = initial.copyOf() val w = IntArray(64) @@ -26,6 +26,6 @@ object `SHA-224` : HashFunction { for (i in input.indices step block) { `SHA-256`.process(h, input, i, w, v) } - h.copyInto(destination, endIndex = 7) + h.copyInto(destination, destinationOffset, endIndex = 7) } } diff --git a/src/commonMain/kotlin/com/infendro/hash/sha2/SHA-256.kt b/src/commonMain/kotlin/com/infendro/hash/sha2/SHA-256.kt index db53bef..326f216 100644 --- a/src/commonMain/kotlin/com/infendro/hash/sha2/SHA-256.kt +++ b/src/commonMain/kotlin/com/infendro/hash/sha2/SHA-256.kt @@ -31,7 +31,7 @@ object `SHA-256` : HashFunction { 0x748f82eeU, 0x78a5636fU, 0x84c87814U, 0x8cc70208U, 0x90befffaU, 0xa4506cebU, 0xbef9a3f7U, 0xc67178f2U, ).asIntArray() - override fun hashInto(value: ByteArray, destination: ByteArray) { + override fun hashInto(value: ByteArray, destination: ByteArray, destinationOffset: Int) { val input = pad(value) val h = initial.copyOf() val w = IntArray(64) @@ -40,7 +40,7 @@ object `SHA-256` : HashFunction { for (i in input.indices step block) { process(h, input, i, w, v) } - h.copyInto(destination) + h.copyInto(destination, destinationOffset) } internal fun pad(value: ByteArray): ByteArray { diff --git a/src/commonMain/kotlin/com/infendro/hash/sha2/SHA-384.kt b/src/commonMain/kotlin/com/infendro/hash/sha2/SHA-384.kt index 3d069e7..5f7f5bc 100644 --- a/src/commonMain/kotlin/com/infendro/hash/sha2/SHA-384.kt +++ b/src/commonMain/kotlin/com/infendro/hash/sha2/SHA-384.kt @@ -17,7 +17,7 @@ object `SHA-384` : HashFunction { 0xdb0c2e0d64f98fa7UL, 0x47b5481dbefa4fa4UL, ).asLongArray() - override fun hashInto(value: ByteArray, destination: ByteArray) { + override fun hashInto(value: ByteArray, destination: ByteArray, destinationOffset: Int) { val input = `SHA-512`.pad(value) val h = initial.copyOf() val w = LongArray(80) @@ -26,6 +26,6 @@ object `SHA-384` : HashFunction { for (i in input.indices step block) { `SHA-512`.process(h, input, i, w, v) } - h.copyInto(destination, endIndex = 6) + h.copyInto(destination, destinationOffset, endIndex = 6) } } diff --git a/src/commonMain/kotlin/com/infendro/hash/sha2/SHA-512.kt b/src/commonMain/kotlin/com/infendro/hash/sha2/SHA-512.kt index 1dd84e0..c0be8cc 100644 --- a/src/commonMain/kotlin/com/infendro/hash/sha2/SHA-512.kt +++ b/src/commonMain/kotlin/com/infendro/hash/sha2/SHA-512.kt @@ -43,7 +43,7 @@ object `SHA-512` : HashFunction { 0x4cc5d4becb3e42b6UL, 0x597f299cfc657e2aUL, 0x5fcb6fab3ad6faecUL, 0x6c44198c4a475817UL, ).asLongArray() - override fun hashInto(value: ByteArray, destination: ByteArray) { + override fun hashInto(value: ByteArray, destination: ByteArray, destinationOffset: Int) { val input = pad(value) val h = initial.copyOf() val w = LongArray(80) @@ -52,7 +52,7 @@ object `SHA-512` : HashFunction { for (i in input.indices step block) { process(h, input, i, w, v) } - h.copyInto(destination) + h.copyInto(destination, destinationOffset) } internal fun pad(value: ByteArray): ByteArray {