Merge pull request '1.3.0 release' (#3) from develop into main
All checks were successful
/ publish (push) Successful in 4m38s
All checks were successful
/ publish (push) Successful in 4m38s
Reviewed-on: Infendro/mac-kt#3
This commit was merged in pull request #3.
This commit is contained in:
@@ -1,36 +0,0 @@
|
|||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- main
|
|
||||||
|
|
||||||
env:
|
|
||||||
INFENDRO__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/
|
|
||||||
47
.gitea/workflows/publish.yaml
Normal file
47
.gitea/workflows/publish.yaml
Normal 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 }}
|
||||||
28
.gitea/workflows/test.yaml
Normal file
28
.gitea/workflows/test.yaml
Normal 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
|
||||||
24
README.md
24
README.md
@@ -1,19 +1,10 @@
|
|||||||
# mac-kt
|
# mac-kt
|
||||||
|
|
||||||
`mac-kt` is a Kotlin Multiplatform library that provides common MAC implementations.
|
`mac-kt` is a Kotlin Multiplatform library that provides common [Message Authentication Codes](https://en.wikipedia.org/wiki/Message_authentication_code).
|
||||||
|
|
||||||
Supported MACs:
|
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.3.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)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -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.3.0"
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
alias(libs.plugins.infendro)
|
alias(libs.plugins.infendro)
|
||||||
@@ -12,10 +15,14 @@ plugins {
|
|||||||
|
|
||||||
kotlin {
|
kotlin {
|
||||||
jvm()
|
jvm()
|
||||||
js {
|
|
||||||
nodejs()
|
|
||||||
}
|
|
||||||
linuxX64()
|
linuxX64()
|
||||||
|
linuxArm64()
|
||||||
|
mingwX64()
|
||||||
|
macosX64()
|
||||||
|
macosArm64()
|
||||||
|
js { nodejs() }
|
||||||
|
wasmJs { nodejs() }
|
||||||
|
wasmWasi { nodejs() }
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
infendro()
|
infendro()
|
||||||
@@ -25,7 +32,7 @@ kotlin {
|
|||||||
sourceSets {
|
sourceSets {
|
||||||
commonMain.dependencies {
|
commonMain.dependencies {
|
||||||
implementation(libs.hash)
|
implementation(libs.hash)
|
||||||
implementation(libs.bytearray)
|
implementation(libs.bytes)
|
||||||
}
|
}
|
||||||
commonTest.dependencies {
|
commonTest.dependencies {
|
||||||
implementation(libs.test)
|
implementation(libs.test)
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
[versions]
|
[versions]
|
||||||
infendro = "1.0.0"
|
infendro = "1.1.0"
|
||||||
kotlin = "2.2.21"
|
kotlin = "2.3.0"
|
||||||
hash = "1.3.2"
|
hash = "1.5.1"
|
||||||
bytearray = "1.2.4"
|
bytes = "1.3.3"
|
||||||
|
|
||||||
[plugins]
|
[plugins]
|
||||||
infendro = { id = "com.infendro.plugin", version.ref = "infendro" }
|
infendro = { id = "com.infendro.plugin", version.ref = "infendro" }
|
||||||
@@ -10,5 +10,5 @@ multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotl
|
|||||||
|
|
||||||
[libraries]
|
[libraries]
|
||||||
hash = { module = "com.infendro:hash", version.ref = "hash" }
|
hash = { module = "com.infendro:hash", version.ref = "hash" }
|
||||||
bytearray = { module = "com.infendro:bytearray", version.ref = "bytearray" }
|
bytes = { module = "com.infendro:bytes", version.ref = "bytes" }
|
||||||
test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
|
test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
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(
|
||||||
@@ -9,25 +9,24 @@ class HMAC(
|
|||||||
override val bytes: Int
|
override val bytes: Int
|
||||||
get() = function.bytes
|
get() = function.bytes
|
||||||
|
|
||||||
override fun hash(
|
override fun hashInto(key: ByteArray, value: ByteArray, destination: ByteArray, destinationOffset: Int) {
|
||||||
key: UByteArray,
|
|
||||||
value: UByteArray,
|
|
||||||
): UByteArray {
|
|
||||||
val paddedKey = pad(key)
|
val paddedKey = pad(key)
|
||||||
|
val inner = ByteArray(function.block + value.size).also {
|
||||||
val innerKey = paddedKey.map { it xor 0x36U }.toUByteArray()
|
paddedKey.xorInto(0x36, it)
|
||||||
val outerKey = paddedKey.map { it xor 0x5CU }.toUByteArray()
|
value.copyInto(it, function.block)
|
||||||
|
}
|
||||||
return function.hash(outerKey + function.hash(innerKey + value))
|
val outer = ByteArray(function.block + function.bytes).also {
|
||||||
|
paddedKey.xorInto(0x5C, it)
|
||||||
|
function.hashInto(inner, it, function.block)
|
||||||
|
}
|
||||||
|
function.hashInto(outer, destination, destinationOffset)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun pad(
|
private fun pad(key: ByteArray): ByteArray =
|
||||||
key: UByteArray,
|
ByteArray(function.block).also {
|
||||||
): UByteArray {
|
when {
|
||||||
return when {
|
key.size > function.block -> function.hashInto(key, it)
|
||||||
key.size > function.block -> function.hash(key)
|
else -> key.copyInto(it)
|
||||||
key.size < function.block -> key.padEnd(function.block, 0x00U)
|
|
||||||
else -> key
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,8 +5,20 @@ interface MAC {
|
|||||||
val bits: Int
|
val bits: Int
|
||||||
get() = bytes * 8
|
get() = bytes * 8
|
||||||
|
|
||||||
fun hash(
|
fun hashInto(key: ByteArray, value: ByteArray, destination: ByteArray, destinationOffset: Int = 0)
|
||||||
key: UByteArray,
|
|
||||||
value: UByteArray,
|
fun hashInto(key: ByteArray, value: ByteArray, destination: UByteArray, destinationOffset: Int = 0) =
|
||||||
): UByteArray
|
hashInto(key, value, destination.asByteArray(), destinationOffset)
|
||||||
|
|
||||||
|
fun hashInto(key: UByteArray, value: UByteArray, destination: ByteArray, destinationOffset: Int = 0) =
|
||||||
|
hashInto(key.asByteArray(), value.asByteArray(), destination, destinationOffset)
|
||||||
|
|
||||||
|
fun hashInto(key: UByteArray, value: UByteArray, destination: UByteArray, destinationOffset: Int = 0) =
|
||||||
|
hashInto(key.asByteArray(), value.asByteArray(), destination.asByteArray(), destinationOffset)
|
||||||
|
|
||||||
|
fun hash(key: ByteArray, value: ByteArray): ByteArray =
|
||||||
|
ByteArray(bytes).also { hashInto(key, value, it) }
|
||||||
|
|
||||||
|
fun hash(key: UByteArray, value: UByteArray): UByteArray =
|
||||||
|
UByteArray(bytes).also { hashInto(key, value, it) }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
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
|
||||||
@@ -16,9 +15,8 @@ class `HMAC Test` {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `hash() - example`() {
|
fun example() {
|
||||||
assertContentEquals(
|
val expected = ubyteArrayOf(
|
||||||
ubyteArrayOf(
|
|
||||||
0xf7U, 0xbcU, 0x83U, 0xf4U,
|
0xf7U, 0xbcU, 0x83U, 0xf4U,
|
||||||
0x30U, 0x53U, 0x84U, 0x24U,
|
0x30U, 0x53U, 0x84U, 0x24U,
|
||||||
0xb1U, 0x32U, 0x98U, 0xe6U,
|
0xb1U, 0x32U, 0x98U, 0xe6U,
|
||||||
@@ -27,11 +25,11 @@ class `HMAC Test` {
|
|||||||
0x49U, 0x46U, 0x17U, 0x59U,
|
0x49U, 0x46U, 0x17U, 0x59U,
|
||||||
0x97U, 0x47U, 0x9dU, 0xbcU,
|
0x97U, 0x47U, 0x9dU, 0xbcU,
|
||||||
0x2dU, 0x1aU, 0x3cU, 0xd8U,
|
0x2dU, 0x1aU, 0x3cU, 0xd8U,
|
||||||
),
|
).asByteArray()
|
||||||
hmac.hash(
|
val actual = hmac.hash(
|
||||||
"key".encodeToUByteArray(),
|
"key".encodeToByteArray(),
|
||||||
"The quick brown fox jumps over the lazy dog".encodeToUByteArray()
|
"The quick brown fox jumps over the lazy dog".encodeToByteArray(),
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user