Compare commits

..

2 Commits

Author SHA1 Message Date
2e52f7aa39 Merge pull request '1.4.0 release' (#4) from develop into main
All checks were successful
/ publish (push) Successful in 2m51s
Reviewed-on: Infendro/mac-kt#4
2026-02-15 13:13:18 +00:00
11f84752fb Merge pull request '1.3.0 release' (#3) from develop into main
All checks were successful
/ publish (push) Successful in 4m38s
Reviewed-on: Infendro/mac-kt#3
2026-01-29 16:41:55 +00:00
6 changed files with 32 additions and 85 deletions

View File

@@ -1,6 +1,6 @@
# mac.kt
# mac-kt
`mac.kt` is a Kotlin Multiplatform library that provides common [Message Authentication Codes](https://en.wikipedia.org/wiki/Message_authentication_code).
`mac-kt` is a Kotlin Multiplatform library that provides common [Message Authentication Codes](https://en.wikipedia.org/wiki/Message_authentication_code).
Supported algorithms:
- HMAC
@@ -15,7 +15,7 @@ repositories {
}
dependencies {
implementation("com.infendro:mac:1.5.0")
implementation("com.infendro:mac:1.4.0")
}
```
@@ -24,7 +24,7 @@ dependencies {
```kotlin
fun main() {
// create an HMAC instance using SHA1
val hmac = HMAC(SHA1())
val hmac = HMAC(SHA1)
// generate MAC
val key = "secret".encodeToByteArray()

View File

@@ -5,7 +5,7 @@ import com.infendro.plugin.token
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
group = "com.infendro"
version = "1.5.0"
version = "1.4.0"
plugins {
alias(libs.plugins.infendro)

View File

@@ -1,8 +1,8 @@
[versions]
infendro = "1.1.0"
kotlin = "2.3.0"
bytes = "1.4.1"
hash = "1.7.0"
bytes = "1.4.0"
hash = "1.6.0"
[plugins]
infendro = { id = "com.infendro.plugin", version.ref = "infendro" }

View File

@@ -5,59 +5,29 @@ import com.infendro.hash.HashFunction
class HMAC(
val function: HashFunction,
) : MAC(function.bytes) {
private var initialized = false
private val innerPadding: ByteArray = ByteArray(function.block)
private val outerPadding: ByteArray = ByteArray(function.block)
private val inner: ByteArray = ByteArray(function.bytes)
) : MAC {
override val bytes: Int
get() = function.bytes
override fun reset() {
function.reset()
initialized = false
innerPadding.fill(0x00)
outerPadding.fill(0x00)
override fun hashInto(key: ByteArray, value: ByteArray, destination: ByteArray, destinationOffset: Int) {
val paddedKey = pad(key)
val inner = ByteArray(function.block + value.size).also {
paddedKey.xorInto(0x36, it)
value.copyInto(it, function.block)
}
override fun init(key: ByteArray) {
val padded = pad(key)
padded.xorInto(0x36, innerPadding)
padded.xorInto(0x5C, outerPadding)
function.reset()
function.update(innerPadding)
initialized = true
val outer = ByteArray(function.block + function.bytes).also {
paddedKey.xorInto(0x5C, it)
function.hashInto(inner, it, function.block)
}
override fun update(byte: Byte) {
require(initialized)
function.update(byte)
}
override fun update(bytes: ByteArray) {
require(initialized)
function.update(bytes)
}
override fun digestInto(destination: ByteArray, destinationOffset: Int) {
require(initialized)
function.digestInto(inner)
function.update(outerPadding)
function.update(inner)
function.digestInto(destination, destinationOffset)
function.update(innerPadding)
function.hashInto(outer, destination, destinationOffset)
}
private fun pad(key: ByteArray): ByteArray {
val padded = ByteArray(function.block)
return ByteArray(function.block).also {
when {
key.size > function.block -> function.hashInto(key, padded)
else -> key.copyInto(padded)
key.size > function.block -> function.hashInto(key, it)
else -> key.copyInto(it)
}
}
return padded
}
}

View File

@@ -1,38 +1,15 @@
package com.infendro.mac
abstract class MAC(
val bytes: Int,
) {
interface MAC {
val bytes: Int
val bits: Int
get() = bytes * 8
abstract fun reset()
abstract fun init(key: ByteArray)
abstract fun update(byte: Byte)
abstract fun update(bytes: ByteArray)
abstract fun digestInto(destination: ByteArray, destinationOffset: Int = 0)
fun digest(): ByteArray {
val result = ByteArray(bytes)
digestInto(result)
return result
}
fun hashInto(key: ByteArray, value: ByteArray, destination: ByteArray, destinationOffset: Int = 0) {
init(key)
update(value)
digestInto(destination, destinationOffset)
reset()
}
fun hashInto(key: ByteArray, value: ByteArray, destination: ByteArray, destinationOffset: Int = 0)
fun hash(key: ByteArray, value: ByteArray): ByteArray {
init(key)
update(value)
val result = digest()
reset()
val result = ByteArray(bytes)
hashInto(key, value, result)
return result
}
}

View File

@@ -6,7 +6,7 @@ import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
class `HMAC Test` {
val hmac = HMAC(`SHA-256`())
val hmac = HMAC(`SHA-256`)
@Test
fun bytes() {