implement optimizations

This commit is contained in:
2026-01-22 12:50:36 +01:00
parent 3603583d94
commit d07f2658ff
6 changed files with 58 additions and 63 deletions

View File

@@ -1,6 +1,6 @@
package com.infendro.mac
import com.infendro.bytearray.padEnd
import com.infendro.bytes.bytearray.xorInto
import com.infendro.hash.HashFunction
class HMAC(
@@ -9,25 +9,24 @@ class HMAC(
override val bytes: Int
get() = function.bytes
override fun hash(
key: UByteArray,
value: UByteArray,
): UByteArray {
override fun hashInto(key: ByteArray, value: ByteArray, destination: ByteArray, destinationOffset: Int) {
val paddedKey = pad(key)
val innerKey = paddedKey.map { it xor 0x36U }.toUByteArray()
val outerKey = paddedKey.map { it xor 0x5CU }.toUByteArray()
return function.hash(outerKey + function.hash(innerKey + value))
}
private fun pad(
key: UByteArray,
): UByteArray {
return when {
key.size > function.block -> function.hash(key)
key.size < function.block -> key.padEnd(function.block, 0x00U)
else -> key
val inner = ByteArray(function.block + value.size).also {
paddedKey.xorInto(0x36, it)
value.copyInto(it, function.block)
}
val outer = ByteArray(function.block + function.bytes).also {
paddedKey.xorInto(0x5C, it)
function.hashInto(inner, it, function.block)
}
function.hashInto(outer, destination, destinationOffset)
}
private fun pad(key: ByteArray): ByteArray =
ByteArray(function.block).also {
when {
key.size > function.block -> function.hashInto(key, it)
else -> key.copyInto(it)
}
}
}

View File

@@ -5,8 +5,20 @@ interface MAC {
val bits: Int
get() = bytes * 8
fun hash(
key: UByteArray,
value: UByteArray,
): UByteArray
fun hashInto(key: ByteArray, value: ByteArray, destination: ByteArray, destinationOffset: Int = 0)
fun hashInto(key: ByteArray, value: ByteArray, destination: UByteArray, destinationOffset: Int = 0) =
hashInto(key, value, destination.asByteArray(), destinationOffset)
fun hashInto(key: UByteArray, value: UByteArray, destination: ByteArray, destinationOffset: Int = 0) =
hashInto(key.asByteArray(), value.asByteArray(), destination, destinationOffset)
fun hashInto(key: UByteArray, value: UByteArray, destination: UByteArray, destinationOffset: Int = 0) =
hashInto(key.asByteArray(), value.asByteArray(), destination.asByteArray(), destinationOffset)
fun hash(key: ByteArray, value: ByteArray): ByteArray =
ByteArray(bytes).also { hashInto(key, value, it) }
fun hash(key: UByteArray, value: UByteArray): UByteArray =
UByteArray(bytes).also { hashInto(key, value, it) }
}

View File

@@ -1,6 +1,5 @@
package com.infendro.mac
import com.infendro.bytearray.encodeToUByteArray
import com.infendro.hash.sha2.`SHA-256`
import kotlin.test.Test
import kotlin.test.assertContentEquals
@@ -16,22 +15,21 @@ class `HMAC Test` {
}
@Test
fun `example`() {
assertContentEquals(
ubyteArrayOf(
0xf7U, 0xbcU, 0x83U, 0xf4U,
0x30U, 0x53U, 0x84U, 0x24U,
0xb1U, 0x32U, 0x98U, 0xe6U,
0xaaU, 0x6fU, 0xb1U, 0x43U,
0xefU, 0x4dU, 0x59U, 0xa1U,
0x49U, 0x46U, 0x17U, 0x59U,
0x97U, 0x47U, 0x9dU, 0xbcU,
0x2dU, 0x1aU, 0x3cU, 0xd8U,
),
hmac.hash(
"key".encodeToUByteArray(),
"The quick brown fox jumps over the lazy dog".encodeToUByteArray()
)
fun example() {
val expected = ubyteArrayOf(
0xf7U, 0xbcU, 0x83U, 0xf4U,
0x30U, 0x53U, 0x84U, 0x24U,
0xb1U, 0x32U, 0x98U, 0xe6U,
0xaaU, 0x6fU, 0xb1U, 0x43U,
0xefU, 0x4dU, 0x59U, 0xa1U,
0x49U, 0x46U, 0x17U, 0x59U,
0x97U, 0x47U, 0x9dU, 0xbcU,
0x2dU, 0x1aU, 0x3cU, 0xd8U,
).asByteArray()
val actual = hmac.hash(
"key".encodeToByteArray(),
"The quick brown fox jumps over the lazy dog".encodeToByteArray(),
)
assertContentEquals(expected, actual)
}
}