Compare commits

..

9 Commits

Author SHA1 Message Date
1c178c3262 Merge pull request '1.5.0 Release' (#5) from develop into main
All checks were successful
/ publish (push) Successful in 2m38s
Reviewed-on: Infendro/mac-kt#5
2026-03-11 22:07:23 +00:00
1f31ea9509 Bump version to 1.5.0
All checks were successful
/ test (pull_request) Successful in 42s
2026-03-11 22:58:59 +01:00
667fba9bdf Implement streaming API 2026-03-11 22:57:43 +01:00
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
4f04f4ae02 Remove unsigned support
All checks were successful
/ test (pull_request) Successful in 50s
2026-02-15 00:11:45 +01: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
41d37cb42e split ci
All checks were successful
/ test (pull_request) Successful in 41s
2026-01-22 12:57:34 +01:00
c11e80d221 bump version to 1.3.0, add targets
Some checks failed
/ publish (push) Has been cancelled
/ test (push) Has been cancelled
2026-01-22 12:51:45 +01:00
d07f2658ff implement optimizations 2026-01-22 12:50:36 +01:00
9 changed files with 192 additions and 170 deletions

View File

@@ -1,92 +0,0 @@
on:
push:
branches: [ develop, main ]
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: 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: Test
run: ./gradlew allTests
publish:
if: ${{ github.event_name == 'push' && github.ref_name == 'main' }}
needs: 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: 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

@@ -0,0 +1,47 @@
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

@@ -0,0 +1,28 @@
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,19 +1,10 @@
# mac-kt # mac-kt
`mac-kt` is a Kotlin Multiplatform library that provides common [Message Authentication Code](https://en.wikipedia.org/wiki/Message_authentication_code) algorithms. `mac-kt` is a Kotlin Multiplatform library that provides common [Message Authentication Codes](https://en.wikipedia.org/wiki/Message_authentication_code).
Supported algorithms: Supported algorithms:
- HMAC - HMAC
## Targets
| Target | Status |
|------------------|---------------|
| JVM | Supported |
| JavaScript | Supported |
| Native (Linux) | Supported |
| Native (Windows) | Not Supported |
## Installation ## Installation
Add the following to your `build.gradle.kts`. Add the following to your `build.gradle.kts`.
@@ -24,25 +15,20 @@ repositories {
} }
dependencies { dependencies {
implementation("com.infendro:mac:1.2.0") implementation("com.infendro:mac:1.5.0")
} }
``` ```
## Usage ## Usage
```kotlin ```kotlin
import com.infendro.hash.sha1.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" val key = "secret".encodeToByteArray()
.encodeToByteArray().asUByteArray() val value = "Hello World!".encodeToByteArray()
val value = "Hello World!"
.encodeToByteArray().asUByteArray()
val mac = hmac.hash(key, value) val mac = hmac.hash(key, value)
} }
``` ```

View File

@@ -1,8 +1,11 @@
@file:OptIn(ExperimentalWasmDsl::class)
import com.infendro.plugin.infendro import com.infendro.plugin.infendro
import com.infendro.plugin.token import com.infendro.plugin.token
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
group = "com.infendro" group = "com.infendro"
version = "1.2.0" version = "1.5.0"
plugins { plugins {
alias(libs.plugins.infendro) alias(libs.plugins.infendro)
@@ -13,9 +16,13 @@ plugins {
kotlin { kotlin {
jvm() jvm()
linuxX64() linuxX64()
js { linuxArm64()
nodejs() mingwX64()
} macosX64()
macosArm64()
js { nodejs() }
wasmJs { nodejs() }
wasmWasi { nodejs() }
repositories { repositories {
infendro() infendro()
@@ -24,17 +31,13 @@ kotlin {
sourceSets { sourceSets {
commonMain.dependencies { commonMain.dependencies {
implementation(libs.bytes)
implementation(libs.hash) implementation(libs.hash)
implementation(libs.bytearray)
} }
commonTest.dependencies { commonTest.dependencies {
implementation(libs.test) implementation(libs.test)
} }
} }
compilerOptions {
freeCompilerArgs.add("-opt-in=kotlin.ExperimentalUnsignedTypes")
}
} }
publishing.repositories { publishing.repositories {

View File

@@ -1,14 +1,14 @@
[versions] [versions]
infendro = "1.0.0" infendro = "1.1.0"
kotlin = "2.2.21" kotlin = "2.3.0"
hash = "1.3.2" bytes = "1.4.1"
bytearray = "1.2.4" hash = "1.7.0"
[plugins] [plugins]
infendro = { id = "com.infendro.plugin", version.ref = "infendro" } 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" }
bytearray = { module = "com.infendro:bytearray", version.ref = "bytearray" }
test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" } test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }

View File

@@ -1,33 +1,63 @@
package com.infendro.mac package com.infendro.mac
import com.infendro.bytearray.padEnd import com.infendro.bytes.bytearray.xorInto
import com.infendro.hash.HashFunction import com.infendro.hash.HashFunction
class HMAC( class HMAC(
val function: HashFunction, val function: HashFunction,
) : MAC { ) : MAC(function.bytes) {
override val bytes: Int private var initialized = false
get() = function.bytes private val innerPadding: ByteArray = ByteArray(function.block)
private val outerPadding: ByteArray = ByteArray(function.block)
private val inner: ByteArray = ByteArray(function.bytes)
override fun hash( override fun reset() {
key: UByteArray, function.reset()
value: UByteArray, initialized = false
): UByteArray { innerPadding.fill(0x00)
val paddedKey = pad(key) outerPadding.fill(0x00)
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( override fun init(key: ByteArray) {
key: UByteArray, val padded = pad(key)
): UByteArray { padded.xorInto(0x36, innerPadding)
return when { padded.xorInto(0x5C, outerPadding)
key.size > function.block -> function.hash(key)
key.size < function.block -> key.padEnd(function.block, 0x00U) function.reset()
else -> key function.update(innerPadding)
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,12 +1,38 @@
package com.infendro.mac package com.infendro.mac
interface MAC { abstract class MAC(
val bytes: Int val bytes: Int,
) {
val bits: Int val bits: Int
get() = bytes * 8 get() = bytes * 8
fun hash( abstract fun reset()
key: UByteArray,
value: UByteArray, abstract fun init(key: ByteArray)
): 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,13 +1,12 @@
package com.infendro.mac package com.infendro.mac
import com.infendro.bytearray.encodeToUByteArray
import com.infendro.hash.sha2.`SHA-256` import com.infendro.hash.sha2.`SHA-256`
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertContentEquals import kotlin.test.assertContentEquals
import kotlin.test.assertEquals import kotlin.test.assertEquals
class `HMAC Test` { class `HMAC Test` {
val hmac = HMAC(`SHA-256`) val hmac = HMAC(`SHA-256`())
@Test @Test
fun bytes() { fun bytes() {
@@ -16,22 +15,17 @@ class `HMAC Test` {
} }
@Test @Test
fun `example`() { fun example() {
assertContentEquals( val expected = byteArrayOf(
ubyteArrayOf( -0x09, -0x44, -0x7d, -0x0c, +0x30, +0x53, -0x7c, +0x24,
0xf7U, 0xbcU, 0x83U, 0xf4U, -0x4f, +0x32, -0x68, -0x1a, -0x56, +0x6f, -0x4f, +0x43,
0x30U, 0x53U, 0x84U, 0x24U, -0x11, +0x4d, +0x59, -0x5f, +0x49, +0x46, +0x17, +0x59,
0xb1U, 0x32U, 0x98U, 0xe6U, -0x69, +0x47, -0x63, -0x44, +0x2d, +0x1a, +0x3c, -0x28,
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()
)
) )
val actual = hmac.hash(
"key".encodeToByteArray(),
"The quick brown fox jumps over the lazy dog".encodeToByteArray(),
)
assertContentEquals(expected, actual)
} }
} }