Compare commits

..

2 Commits

Author SHA1 Message Date
0229457a20 bump version to 1.2.0
All checks were successful
/ publish (push) Successful in 2m34s
2025-11-10 14:54:28 +01:00
9db4ee21a0 implement common class, refactor HMAC 2025-11-10 14:54:19 +01:00
4 changed files with 40 additions and 31 deletions

View File

@@ -1,5 +1,5 @@
group = "com.infendro"
version = "1.1.2"
version = "1.2.0"
repositories {
maven("https://git.infendro.com/api/packages/Infendro/maven")
@@ -21,8 +21,13 @@ kotlin {
sourceSets {
commonMain.dependencies {
implementation(libs.hash)
implementation(libs.bytearray)
}
}
compilerOptions {
freeCompilerArgs.add("-opt-in=kotlin.ExperimentalUnsignedTypes")
}
}
publishing {

View File

@@ -1,9 +1,11 @@
[versions]
kotlin = "2.1.20"
hash = "1.2.2"
hash = "1.3.1"
bytearray = "1.2.0"
[plugins]
multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
[libraries]
hash = { module = "com.infendro:hash", version.ref = "hash" }
bytearray = { module = "com.infendro:bytearray", version.ref = "bytearray" }

View File

@@ -1,45 +1,33 @@
package com.infendro.mac
import com.infendro.hash.*
import kotlin.experimental.xor
import com.infendro.bytearray.padEnd
import com.infendro.hash.HashFunction
class HMAC(
val function: HashFunction,
) {
val block: Int
get() = when (function) {
MD5, SHA1, SHA224, SHA256 -> 64
SHA384, SHA512 -> 128
}
) : MAC() {
override val bytes: Int
get() = function.bytes
fun hash(
key: ByteArray,
value: ByteArray,
): ByteArray {
override fun hash(
key: UByteArray,
value: UByteArray,
): UByteArray {
val paddedKey = pad(key)
val innerKey = paddedKey.map { it xor 0x36 }.toByteArray()
val outerKey = paddedKey.map { it xor 0x5C }.toByteArray()
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: ByteArray,
): ByteArray {
if (key.size > block) {
return function.hash(key)
}
if (key.size < block) {
return buildList {
addAll(key.toList())
repeat(block - size) {
add(0x00)
}
}.toByteArray()
}
return key
key: UByteArray,
): UByteArray {
return when {
key.size > function.block -> function.hash(key)
key.size < function.block -> key.padEnd(function.block, 0x00U)
else -> key
}
}
}

View File

@@ -0,0 +1,14 @@
package com.infendro.mac
abstract class MAC {
abstract val bytes: Int
@Suppress("UNUSED")
val bits: Int
get() = bytes * 8
abstract fun hash(
key: UByteArray,
value: UByteArray,
): UByteArray
}