Compare commits

..

2 Commits

Author SHA1 Message Date
cff4a4e7a4 bump version to 1.2.1
All checks were successful
/ publish (push) Successful in 2m32s
2025-11-10 16:10:59 +01:00
0229457a20 bump version to 1.2.0
All checks were successful
/ publish (push) Successful in 2m34s
2025-11-10 14:54:28 +01:00
10 changed files with 124 additions and 232 deletions

View File

@@ -0,0 +1,36 @@
on:
push:
branches:
- main
env:
GIT__TOKEN: ${{ secrets.TOKEN }}
jobs:
publish:
runs-on: linux
steps:
- uses: actions/checkout@v4
- uses: actions/cache/restore@v4
with:
key: gradle
path: |
~/.gradle/caches/
~/.gradle/wrapper/
~/.konan/
- uses: actions/setup-java@v4
with:
java-version: '24'
distribution: 'temurin'
- run: ./gradlew publish
- uses: actions/cache/save@v4
with:
key: gradle
path: |
~/.gradle/caches/
~/.gradle/wrapper/
~/.konan/

View File

@@ -1,47 +0,0 @@
on:
push:
branches: [ main ]
jobs:
publish:
runs-on: linux
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Java
uses: actions/setup-java@v4
with:
java-version: '24'
distribution: 'temurin'
- name: Cache Gradle
uses: actions/cache@v4
with:
key: gradle-${{ hashFiles('**/libs.versions.toml') }}
restore-keys: gradle
path: |
~/.gradle/caches/
~/.gradle/wrapper/
- name: Cache Konan
uses: actions/cache@v4
with:
key: konan-${{ hashFiles('**/libs.versions.toml') }}
restore-keys: konan
path: |
~/.konan/
- name: Cache Node
uses: actions/cache@v4
with:
key: node-${{ hashFiles('**/libs.versions.toml') }}
restore-keys: node
path: |
~/.gradle/nodejs
~/.gradle/yarn
- name: Publish
run: ./gradlew publish
env:
INFENDRO__TOKEN: ${{ secrets.TOKEN }}

View File

@@ -1,28 +0,0 @@
on:
pull_request:
branches: [ main ]
jobs:
test:
runs-on: linux
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Java
uses: actions/setup-java@v4
with:
java-version: '24'
distribution: 'temurin'
- name: Cache Gradle
uses: actions/cache@v4
with:
key: gradle-${{ hashFiles('**/libs.versions.toml') }}
restore-keys: gradle
path: |
~/.gradle/caches/
~/.gradle/wrapper/
- name: Test
run: ./gradlew jvmTest

View File

@@ -1,9 +1,15 @@
# mac-kt # Kotlin MAC
`mac-kt` is a Kotlin Multiplatform library that provides common [Message Authentication Codes](https://en.wikipedia.org/wiki/Message_authentication_code). This library provides various [Message Authentication Code](https://en.wikipedia.org/wiki/Message_authentication_code) implementations in Kotlin Multiplatform.
Supported algorithms: ## Features
- HMAC
* Generate message authentication codes.
* [HMAC](https://en.wikipedia.org/wiki/HMAC)
* Multiplatform support
* JVM
* JavaScript
* Native (Linux)
## Installation ## Installation
@@ -15,16 +21,19 @@ repositories {
} }
dependencies { dependencies {
implementation("com.infendro:mac:1.5.0") implementation("com.infendro:mac:1.2.2")
} }
``` ```
## Usage ## Usage
```kotlin ```kotlin
import com.infendro.hash.SHA1
import com.infendro.mac.HMAC
fun main() { fun main() {
// create an HMAC instance using SHA1 // create an HMAC instance using SHA1
val hmac = HMAC(SHA1()) val hmac = HMAC(SHA1)
// generate MAC // generate MAC
val key = "secret".encodeToByteArray() val key = "secret".encodeToByteArray()

View File

@@ -1,45 +1,60 @@
@file:OptIn(ExperimentalWasmDsl::class)
import com.infendro.plugin.infendro
import com.infendro.plugin.token
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
group = "com.infendro" group = "com.infendro"
version = "1.5.0" version = "1.2.1"
repositories {
maven("https://git.infendro.com/api/packages/Infendro/maven")
mavenCentral()
}
plugins { plugins {
alias(libs.plugins.infendro)
alias(libs.plugins.multiplatform) alias(libs.plugins.multiplatform)
id("maven-publish") id("maven-publish")
} }
kotlin { kotlin {
jvm() jvm()
linuxX64() js {
linuxArm64() nodejs()
mingwX64()
macosX64()
macosArm64()
js { nodejs() }
wasmJs { nodejs() }
wasmWasi { nodejs() }
repositories {
infendro()
mavenCentral()
} }
linuxX64()
sourceSets { sourceSets {
commonMain.dependencies { commonMain.dependencies {
implementation(libs.bytes)
implementation(libs.hash) implementation(libs.hash)
implementation(libs.bytearray)
}
}
compilerOptions {
freeCompilerArgs.add("-opt-in=kotlin.ExperimentalUnsignedTypes")
}
}
publishing {
repositories {
maven {
url = uri("https://git.infendro.com/api/packages/Infendro/maven")
authentication.create("header", HttpHeaderAuthentication::class)
credentials(HttpHeaderCredentials::class) {
val token = secret("git.token") ?: throw GradleException()
name = "Authorization"
value = "token $token"
} }
commonTest.dependencies {
implementation(libs.test)
} }
} }
} }
publishing.repositories { fun secret(
infendro(token) key: String,
): String? {
val property = key
val environment = key
.uppercase()
.replace(".", "__")
val prop = properties[property] as String?
val env = System.getenv(environment)
return prop ?: env
} }

View File

@@ -1,14 +1,11 @@
[versions] [versions]
infendro = "1.1.0" kotlin = "2.1.20"
kotlin = "2.3.0" hash = "1.3.1"
bytes = "1.4.1" bytearray = "1.2.1"
hash = "1.7.0"
[plugins] [plugins]
infendro = { id = "com.infendro.plugin", version.ref = "infendro" }
multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" } multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
[libraries] [libraries]
bytes = { module = "com.infendro:bytes", version.ref = "bytes" }
hash = { module = "com.infendro:hash", version.ref = "hash" } hash = { module = "com.infendro:hash", version.ref = "hash" }
test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" } bytearray = { module = "com.infendro:bytearray", version.ref = "bytearray" }

View File

@@ -1,6 +1 @@
rootProject.name = "mac" rootProject.name = "mac"
pluginManagement.repositories {
maven("https://git.infendro.com/api/packages/Infendro/maven")
mavenCentral()
}

View File

@@ -1,63 +1,33 @@
package com.infendro.mac package com.infendro.mac
import com.infendro.bytes.bytearray.xorInto import com.infendro.bytearray.padEnd
import com.infendro.hash.HashFunction import com.infendro.hash.HashFunction
class HMAC( class HMAC(
val function: HashFunction, val function: HashFunction,
) : MAC(function.bytes) { ) : MAC() {
private var initialized = false override val bytes: Int
private val innerPadding: ByteArray = ByteArray(function.block) get() = function.bytes
private val outerPadding: ByteArray = ByteArray(function.block)
private val inner: ByteArray = ByteArray(function.bytes)
override fun reset() { override fun hash(
function.reset() key: UByteArray,
initialized = false value: UByteArray,
innerPadding.fill(0x00) ): UByteArray {
outerPadding.fill(0x00) 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))
} }
override fun init(key: ByteArray) { private fun pad(
val padded = pad(key) key: UByteArray,
padded.xorInto(0x36, innerPadding) ): UByteArray {
padded.xorInto(0x5C, outerPadding) return when {
key.size > function.block -> function.hash(key).padEnd(function.block, 0x00U)
function.reset() key.size < function.block -> key.padEnd(function.block, 0x00U)
function.update(innerPadding) else -> key
initialized = true
} }
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)
}
private fun pad(key: ByteArray): ByteArray {
val padded = ByteArray(function.block)
when {
key.size > function.block -> function.hashInto(key, padded)
else -> key.copyInto(padded)
}
return padded
} }
} }

View File

@@ -1,38 +1,14 @@
package com.infendro.mac package com.infendro.mac
abstract class MAC( abstract class MAC {
val bytes: Int, abstract val bytes: Int
) {
@Suppress("UNUSED")
val bits: Int val bits: Int
get() = bytes * 8 get() = bytes * 8
abstract fun reset() abstract fun hash(
key: UByteArray,
abstract fun init(key: ByteArray) value: UByteArray,
): UByteArray
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 hash(key: ByteArray, value: ByteArray): ByteArray {
init(key)
update(value)
val result = digest()
reset()
return result
}
} }

View File

@@ -1,31 +0,0 @@
package com.infendro.mac
import com.infendro.hash.sha2.`SHA-256`
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
class `HMAC Test` {
val hmac = HMAC(`SHA-256`())
@Test
fun bytes() {
assertEquals(32, hmac.bytes)
assertEquals(256, hmac.bits)
}
@Test
fun example() {
val expected = byteArrayOf(
-0x09, -0x44, -0x7d, -0x0c, +0x30, +0x53, -0x7c, +0x24,
-0x4f, +0x32, -0x68, -0x1a, -0x56, +0x6f, -0x4f, +0x43,
-0x11, +0x4d, +0x59, -0x5f, +0x49, +0x46, +0x17, +0x59,
-0x69, +0x47, -0x63, -0x44, +0x2d, +0x1a, +0x3c, -0x28,
)
val actual = hmac.hash(
"key".encodeToByteArray(),
"The quick brown fox jumps over the lazy dog".encodeToByteArray(),
)
assertContentEquals(expected, actual)
}
}