implement common class, refactor HMAC
This commit is contained in:
@@ -21,8 +21,13 @@ kotlin {
|
||||
sourceSets {
|
||||
commonMain.dependencies {
|
||||
implementation(libs.hash)
|
||||
implementation(libs.bytearray)
|
||||
}
|
||||
}
|
||||
|
||||
compilerOptions {
|
||||
freeCompilerArgs.add("-opt-in=kotlin.ExperimentalUnsignedTypes")
|
||||
}
|
||||
}
|
||||
|
||||
publishing {
|
||||
|
||||
@@ -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" }
|
||||
|
||||
@@ -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)
|
||||
key: UByteArray,
|
||||
): UByteArray {
|
||||
return when {
|
||||
key.size > function.block -> function.hash(key)
|
||||
key.size < function.block -> key.padEnd(function.block, 0x00U)
|
||||
else -> key
|
||||
}
|
||||
|
||||
if (key.size < block) {
|
||||
return buildList {
|
||||
addAll(key.toList())
|
||||
repeat(block - size) {
|
||||
add(0x00)
|
||||
}
|
||||
}.toByteArray()
|
||||
}
|
||||
|
||||
return key
|
||||
}
|
||||
}
|
||||
|
||||
14
src/commonMain/kotlin/com/infendro/mac/MAC.kt
Normal file
14
src/commonMain/kotlin/com/infendro/mac/MAC.kt
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user