implement destination offset
All checks were successful
/ test (pull_request) Successful in 27s

This commit is contained in:
2026-01-22 12:18:22 +01:00
parent b00d231da5
commit 8535476674
16 changed files with 41 additions and 34 deletions

View File

@@ -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 =

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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++

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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)
}
}

View File

@@ -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 {

View File

@@ -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)
}
}

View File

@@ -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 {