Compare commits
5 Commits
11f84752fb
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
1c178c3262
|
|||
|
1f31ea9509
|
|||
|
667fba9bdf
|
|||
|
2e52f7aa39
|
|||
|
4f04f4ae02
|
@@ -15,7 +15,7 @@ repositories {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation("com.infendro:mac:1.3.0")
|
implementation("com.infendro:mac:1.5.0")
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -24,7 +24,7 @@ dependencies {
|
|||||||
```kotlin
|
```kotlin
|
||||||
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()
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import com.infendro.plugin.token
|
|||||||
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
|
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
|
||||||
|
|
||||||
group = "com.infendro"
|
group = "com.infendro"
|
||||||
version = "1.3.0"
|
version = "1.5.0"
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
alias(libs.plugins.infendro)
|
alias(libs.plugins.infendro)
|
||||||
@@ -31,17 +31,13 @@ kotlin {
|
|||||||
|
|
||||||
sourceSets {
|
sourceSets {
|
||||||
commonMain.dependencies {
|
commonMain.dependencies {
|
||||||
implementation(libs.hash)
|
|
||||||
implementation(libs.bytes)
|
implementation(libs.bytes)
|
||||||
|
implementation(libs.hash)
|
||||||
}
|
}
|
||||||
commonTest.dependencies {
|
commonTest.dependencies {
|
||||||
implementation(libs.test)
|
implementation(libs.test)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
compilerOptions {
|
|
||||||
freeCompilerArgs.add("-opt-in=kotlin.ExperimentalUnsignedTypes")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
publishing.repositories {
|
publishing.repositories {
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
[versions]
|
[versions]
|
||||||
infendro = "1.1.0"
|
infendro = "1.1.0"
|
||||||
kotlin = "2.3.0"
|
kotlin = "2.3.0"
|
||||||
hash = "1.5.1"
|
bytes = "1.4.1"
|
||||||
bytes = "1.3.3"
|
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]
|
||||||
hash = { module = "com.infendro:hash", version.ref = "hash" }
|
|
||||||
bytes = { module = "com.infendro:bytes", version.ref = "bytes" }
|
bytes = { module = "com.infendro:bytes", version.ref = "bytes" }
|
||||||
|
hash = { module = "com.infendro:hash", version.ref = "hash" }
|
||||||
test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
|
test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
|
||||||
|
|||||||
@@ -5,28 +5,59 @@ 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 hashInto(key: ByteArray, value: ByteArray, destination: ByteArray, destinationOffset: Int) {
|
override fun reset() {
|
||||||
val paddedKey = pad(key)
|
function.reset()
|
||||||
val inner = ByteArray(function.block + value.size).also {
|
initialized = false
|
||||||
paddedKey.xorInto(0x36, it)
|
innerPadding.fill(0x00)
|
||||||
value.copyInto(it, function.block)
|
outerPadding.fill(0x00)
|
||||||
}
|
|
||||||
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(key: ByteArray): ByteArray =
|
override fun init(key: ByteArray) {
|
||||||
ByteArray(function.block).also {
|
val padded = pad(key)
|
||||||
|
padded.xorInto(0x36, innerPadding)
|
||||||
|
padded.xorInto(0x5C, outerPadding)
|
||||||
|
|
||||||
|
function.reset()
|
||||||
|
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 {
|
when {
|
||||||
key.size > function.block -> function.hashInto(key, it)
|
key.size > function.block -> function.hashInto(key, padded)
|
||||||
else -> key.copyInto(it)
|
else -> key.copyInto(padded)
|
||||||
}
|
}
|
||||||
|
return padded
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,24 +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 hashInto(key: ByteArray, value: ByteArray, destination: ByteArray, destinationOffset: Int = 0)
|
abstract fun reset()
|
||||||
|
|
||||||
fun hashInto(key: ByteArray, value: ByteArray, destination: UByteArray, destinationOffset: Int = 0) =
|
abstract fun init(key: ByteArray)
|
||||||
hashInto(key, value, destination.asByteArray(), destinationOffset)
|
|
||||||
|
|
||||||
fun hashInto(key: UByteArray, value: UByteArray, destination: ByteArray, destinationOffset: Int = 0) =
|
abstract fun update(byte: Byte)
|
||||||
hashInto(key.asByteArray(), value.asByteArray(), destination, destinationOffset)
|
abstract fun update(bytes: ByteArray)
|
||||||
|
|
||||||
fun hashInto(key: UByteArray, value: UByteArray, destination: UByteArray, destinationOffset: Int = 0) =
|
abstract fun digestInto(destination: ByteArray, destinationOffset: Int = 0)
|
||||||
hashInto(key.asByteArray(), value.asByteArray(), destination.asByteArray(), destinationOffset)
|
|
||||||
|
|
||||||
fun hash(key: ByteArray, value: ByteArray): ByteArray =
|
fun digest(): ByteArray {
|
||||||
ByteArray(bytes).also { hashInto(key, value, it) }
|
val result = ByteArray(bytes)
|
||||||
|
digestInto(result)
|
||||||
fun hash(key: UByteArray, value: UByteArray): UByteArray =
|
return result
|
||||||
UByteArray(bytes).also { hashInto(key, value, it) }
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ 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,16 +16,12 @@ class `HMAC Test` {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun example() {
|
fun example() {
|
||||||
val expected = ubyteArrayOf(
|
val expected = byteArrayOf(
|
||||||
0xf7U, 0xbcU, 0x83U, 0xf4U,
|
-0x09, -0x44, -0x7d, -0x0c, +0x30, +0x53, -0x7c, +0x24,
|
||||||
0x30U, 0x53U, 0x84U, 0x24U,
|
-0x4f, +0x32, -0x68, -0x1a, -0x56, +0x6f, -0x4f, +0x43,
|
||||||
0xb1U, 0x32U, 0x98U, 0xe6U,
|
-0x11, +0x4d, +0x59, -0x5f, +0x49, +0x46, +0x17, +0x59,
|
||||||
0xaaU, 0x6fU, 0xb1U, 0x43U,
|
-0x69, +0x47, -0x63, -0x44, +0x2d, +0x1a, +0x3c, -0x28,
|
||||||
0xefU, 0x4dU, 0x59U, 0xa1U,
|
)
|
||||||
0x49U, 0x46U, 0x17U, 0x59U,
|
|
||||||
0x97U, 0x47U, 0x9dU, 0xbcU,
|
|
||||||
0x2dU, 0x1aU, 0x3cU, 0xd8U,
|
|
||||||
).asByteArray()
|
|
||||||
val actual = hmac.hash(
|
val actual = hmac.hash(
|
||||||
"key".encodeToByteArray(),
|
"key".encodeToByteArray(),
|
||||||
"The quick brown fox jumps over the lazy dog".encodeToByteArray(),
|
"The quick brown fox jumps over the lazy dog".encodeToByteArray(),
|
||||||
|
|||||||
Reference in New Issue
Block a user