1.5.0 Release #5
@@ -15,7 +15,7 @@ repositories {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation("com.infendro:mac:1.4.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.4.0"
|
version = "1.5.0"
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
alias(libs.plugins.infendro)
|
alias(libs.plugins.infendro)
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
[versions]
|
[versions]
|
||||||
infendro = "1.1.0"
|
infendro = "1.1.0"
|
||||||
kotlin = "2.3.0"
|
kotlin = "2.3.0"
|
||||||
bytes = "1.4.0"
|
bytes = "1.4.1"
|
||||||
hash = "1.6.0"
|
hash = "1.7.0"
|
||||||
|
|
||||||
[plugins]
|
[plugins]
|
||||||
infendro = { id = "com.infendro.plugin", version.ref = "infendro" }
|
infendro = { id = "com.infendro.plugin", version.ref = "infendro" }
|
||||||
|
|||||||
@@ -5,29 +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)
|
override fun init(key: ByteArray) {
|
||||||
function.hashInto(inner, it, function.block)
|
val padded = pad(key)
|
||||||
|
padded.xorInto(0x36, innerPadding)
|
||||||
|
padded.xorInto(0x5C, outerPadding)
|
||||||
|
|
||||||
|
function.reset()
|
||||||
|
function.update(innerPadding)
|
||||||
|
|
||||||
|
initialized = true
|
||||||
}
|
}
|
||||||
function.hashInto(outer, destination, destinationOffset)
|
|
||||||
|
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 {
|
private fun pad(key: ByteArray): ByteArray {
|
||||||
return ByteArray(function.block).also {
|
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,15 +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()
|
||||||
|
|
||||||
|
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 hash(key: ByteArray, value: ByteArray): ByteArray {
|
fun hash(key: ByteArray, value: ByteArray): ByteArray {
|
||||||
val result = ByteArray(bytes)
|
init(key)
|
||||||
hashInto(key, value, result)
|
update(value)
|
||||||
|
val result = digest()
|
||||||
|
reset()
|
||||||
return result
|
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() {
|
||||||
|
|||||||
Reference in New Issue
Block a user