implement tests
Some checks failed
/ publish (push) Has been cancelled

also fix MD5
This commit is contained in:
2025-11-10 17:42:15 +01:00
parent 9b93fbd093
commit d033d895da
28 changed files with 469 additions and 30 deletions

View File

@@ -22,6 +22,9 @@ kotlin {
commonMain.dependencies { commonMain.dependencies {
implementation(libs.bytearray) implementation(libs.bytearray)
} }
commonTest.dependencies {
implementation(libs.test)
}
} }
compilerOptions { compilerOptions {

View File

@@ -7,3 +7,4 @@ multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotl
[libraries] [libraries]
bytearray = { module = "com.infendro:bytearray", version.ref = "bytearray" } bytearray = { module = "com.infendro:bytearray", version.ref = "bytearray" }
test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }

View File

@@ -2,8 +2,6 @@ package com.infendro.hash
abstract class HashFunction { abstract class HashFunction {
abstract val bytes: Int abstract val bytes: Int
@Suppress("UNUSED")
val bits: Int val bits: Int
get() = bytes * 8 get() = bytes * 8

View File

@@ -6,12 +6,13 @@ import com.infendro.bytearray.toULongArray
import com.infendro.hash.HashFunction import com.infendro.hash.HashFunction
import kotlin.math.min import kotlin.math.min
@Suppress("UNUSED") abstract class Keccak internal constructor(
open class Keccak internal constructor( private val rate: Int,
override val bytes: Int,
override val block: Int,
private val domain: UByte, private val domain: UByte,
) : HashFunction() { ) : HashFunction() {
override val block: Int
get() = rate
private val rc = ulongArrayOf( private val rc = ulongArrayOf(
0x0000000000000001UL, 0x0000000000008082UL, 0x800000000000808aUL, 0x8000000080008000UL, 0x0000000000000001UL, 0x0000000000008082UL, 0x800000000000808aUL, 0x8000000080008000UL,
0x000000000000808bUL, 0x0000000080000001UL, 0x8000000080008081UL, 0x8000000000008009UL, 0x000000000000808bUL, 0x0000000080000001UL, 0x8000000080008081UL, 0x8000000000008009UL,
@@ -35,7 +36,7 @@ open class Keccak internal constructor(
val paddedValue = buildList { val paddedValue = buildList {
addAll(value) addAll(value)
add(domain) add(domain)
while ((size % block) != (block - 1)) { while ((size % rate) != (rate - 1)) {
add(0x00U) add(0x00U)
} }
add(0x80U) add(0x80U)
@@ -45,7 +46,7 @@ open class Keccak internal constructor(
// absorb // absorb
val chunks = paddedValue val chunks = paddedValue
.chunked(block) { it.toUByteArray() } .chunked(rate) { it.toUByteArray() }
for (chunk in chunks) { for (chunk in chunks) {
val numbers = chunk.toULongArray(LITTLE_ENDIAN) val numbers = chunk.toULongArray(LITTLE_ENDIAN)
for ((i, number) in numbers.withIndex()) { for ((i, number) in numbers.withIndex()) {
@@ -58,7 +59,7 @@ open class Keccak internal constructor(
return buildList { return buildList {
var i = 0 var i = 0
while (size < bytes) { while (size < bytes) {
if (i == block / 8) { if (i == rate / 8) {
permute(state) permute(state)
i = 0 i = 0
} }

View File

@@ -2,5 +2,7 @@ package com.infendro.hash.keccak.sha3
import com.infendro.hash.keccak.Keccak import com.infendro.hash.keccak.Keccak
@Suppress("UNUSED") object `SHA3-224` : Keccak(144, 0x06U) {
object `SHA3-224` : Keccak(28, 144, 0x06U) override val bytes: Int
get() = 28
}

View File

@@ -2,5 +2,7 @@ package com.infendro.hash.keccak.sha3
import com.infendro.hash.keccak.Keccak import com.infendro.hash.keccak.Keccak
@Suppress("UNUSED") object `SHA3-256` : Keccak(136, 0x06U) {
object `SHA3-256` : Keccak(32, 136, 0x06U) override val bytes: Int
get() = 32
}

View File

@@ -2,5 +2,7 @@ package com.infendro.hash.keccak.sha3
import com.infendro.hash.keccak.Keccak import com.infendro.hash.keccak.Keccak
@Suppress("UNUSED") object `SHA3-384` : Keccak(104, 0x06U) {
object `SHA3-384` : Keccak(48, 104, 0x06U) override val bytes: Int
get() = 48
}

View File

@@ -2,5 +2,7 @@ package com.infendro.hash.keccak.sha3
import com.infendro.hash.keccak.Keccak import com.infendro.hash.keccak.Keccak
@Suppress("UNUSED") object `SHA3-512` : Keccak(72, 0x06U) {
object `SHA3-512` : Keccak(64, 72, 0x06U) override val bytes: Int
get() = 64
}

View File

@@ -2,5 +2,6 @@ package com.infendro.hash.keccak.shake
import com.infendro.hash.keccak.Keccak import com.infendro.hash.keccak.Keccak
@Suppress("UNUSED") class SHAKE128(
class SHAKE128(bytes: Int) : Keccak(bytes, 168, 0x1FU) override val bytes: Int,
) : Keccak(168, 0x1FU)

View File

@@ -2,5 +2,6 @@ package com.infendro.hash.keccak.shake
import com.infendro.hash.keccak.Keccak import com.infendro.hash.keccak.Keccak
@Suppress("UNUSED") class SHAKE256(
class SHAKE256(bytes: Int) : Keccak(bytes, 136, 0x1FU) override val bytes: Int,
) : Keccak(136, 0x1FU)

View File

@@ -5,7 +5,6 @@ import com.infendro.bytearray.toUByteArray
import com.infendro.bytearray.toUIntArray import com.infendro.bytearray.toUIntArray
import com.infendro.hash.HashFunction import com.infendro.hash.HashFunction
@Suppress("UNUSED")
object MD5 : HashFunction() { object MD5 : HashFunction() {
override val bytes: Int override val bytes: Int
get() = 16 get() = 16
@@ -94,10 +93,11 @@ object MD5 : HashFunction() {
in 32..47 -> (3 * i + 5) % 16 in 32..47 -> (3 * i + 5) % 16
else -> (7 * i) % 16 else -> (7 * i) % 16
} }
val temp = a
a = d a = d
d = c d = c
c = b c = b
b += (f + a + k[i] + w[g]).rotateLeft(s[i]) b += (temp + f + k[i] + w[g]).rotateLeft(s[i])
} }
a0 += a a0 += a

View File

@@ -4,7 +4,6 @@ import com.infendro.bytearray.toUByteArray
import com.infendro.bytearray.toUIntArray import com.infendro.bytearray.toUIntArray
import com.infendro.hash.HashFunction import com.infendro.hash.HashFunction
@Suppress("UNUSED")
object SHA1 : HashFunction() { object SHA1 : HashFunction() {
override val bytes: Int override val bytes: Int
get() = 20 get() = 20

View File

@@ -4,7 +4,6 @@ import com.infendro.bytearray.toUByteArray
import com.infendro.bytearray.toUIntArray import com.infendro.bytearray.toUIntArray
import com.infendro.hash.HashFunction import com.infendro.hash.HashFunction
@Suppress("UNUSED")
object `SHA-224` : HashFunction() { object `SHA-224` : HashFunction() {
override val bytes: Int override val bytes: Int
get() = 28 get() = 28

View File

@@ -4,7 +4,6 @@ import com.infendro.bytearray.toUByteArray
import com.infendro.bytearray.toUIntArray import com.infendro.bytearray.toUIntArray
import com.infendro.hash.HashFunction import com.infendro.hash.HashFunction
@Suppress("UNUSED")
object `SHA-256` : HashFunction() { object `SHA-256` : HashFunction() {
override val bytes: Int override val bytes: Int
get() = 32 get() = 32

View File

@@ -4,7 +4,6 @@ import com.infendro.bytearray.toUByteArray
import com.infendro.bytearray.toULongArray import com.infendro.bytearray.toULongArray
import com.infendro.hash.HashFunction import com.infendro.hash.HashFunction
@Suppress("UNUSED")
object `SHA-384` : HashFunction() { object `SHA-384` : HashFunction() {
override val bytes: Int override val bytes: Int
get() = 48 get() = 48

View File

@@ -4,7 +4,6 @@ import com.infendro.bytearray.toUByteArray
import com.infendro.bytearray.toULongArray import com.infendro.bytearray.toULongArray
import com.infendro.hash.HashFunction import com.infendro.hash.HashFunction
@Suppress("UNUSED")
object `SHA-512` : HashFunction() { object `SHA-512` : HashFunction() {
override val bytes: Int override val bytes: Int
get() = 64 get() = 64

View File

@@ -0,0 +1,33 @@
package com.infendro.hash.keccak.sha3
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
class `SHA3-224 Test` {
val function = `SHA3-224`
@Test
fun `bytes and block`() {
assertEquals(28, function.bytes)
assertEquals(224, function.bits)
assertEquals(144, function.block)
}
@Test
fun `empty bytearray`() {
val expected = ubyteArrayOf(
0x6bU, 0x4eU, 0x03U, 0x42U,
0x36U, 0x67U, 0xdbU, 0xb7U,
0x3bU, 0x6eU, 0x15U, 0x45U,
0x4fU, 0x0eU, 0xb1U, 0xabU,
0xd4U, 0x59U, 0x7fU, 0x9aU,
0x1bU, 0x07U, 0x8eU, 0x3fU,
0x5bU, 0x5aU, 0x6bU, 0xc7U,
)
val actual = function.hash(
ubyteArrayOf()
)
assertContentEquals(expected, actual)
}
}

View File

@@ -0,0 +1,34 @@
package com.infendro.hash.keccak.sha3
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
class `SHA3-256 Test` {
val function = `SHA3-256`
@Test
fun `bytes and block`() {
assertEquals(32, function.bytes)
assertEquals(256, function.bits)
assertEquals(136, function.block)
}
@Test
fun `empty bytearray`() {
val expected = ubyteArrayOf(
0xa7U, 0xffU, 0xc6U, 0xf8U,
0xbfU, 0x1eU, 0xd7U, 0x66U,
0x51U, 0xc1U, 0x47U, 0x56U,
0xa0U, 0x61U, 0xd6U, 0x62U,
0xf5U, 0x80U, 0xffU, 0x4dU,
0xe4U, 0x3bU, 0x49U, 0xfaU,
0x82U, 0xd8U, 0x0aU, 0x4bU,
0x80U, 0xf8U, 0x43U, 0x4aU,
)
val actual = function.hash(
ubyteArrayOf()
)
assertContentEquals(expected, actual)
}
}

View File

@@ -0,0 +1,38 @@
package com.infendro.hash.keccak.sha3
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
class `SHA3-384 Test` {
val function = `SHA3-384`
@Test
fun `bytes and block`() {
assertEquals(48, function.bytes)
assertEquals(384, function.bits)
assertEquals(104, function.block)
}
@Test
fun `empty bytearray`() {
val expected = ubyteArrayOf(
0x0cU, 0x63U, 0xa7U, 0x5bU,
0x84U, 0x5eU, 0x4fU, 0x7dU,
0x01U, 0x10U, 0x7dU, 0x85U,
0x2eU, 0x4cU, 0x24U, 0x85U,
0xc5U, 0x1aU, 0x50U, 0xaaU,
0xaaU, 0x94U, 0xfcU, 0x61U,
0x99U, 0x5eU, 0x71U, 0xbbU,
0xeeU, 0x98U, 0x3aU, 0x2aU,
0xc3U, 0x71U, 0x38U, 0x31U,
0x26U, 0x4aU, 0xdbU, 0x47U,
0xfbU, 0x6bU, 0xd1U, 0xe0U,
0x58U, 0xd5U, 0xf0U, 0x04U,
)
val actual = function.hash(
ubyteArrayOf()
)
assertContentEquals(expected, actual)
}
}

View File

@@ -0,0 +1,42 @@
package com.infendro.hash.keccak.sha3
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
class `SHA3-512 Test` {
val function = `SHA3-512`
@Test
fun `bytes and block`() {
assertEquals(64, function.bytes)
assertEquals(512, function.bits)
assertEquals(72, function.block)
}
@Test
fun `empty bytearray`() {
val expected = ubyteArrayOf(
0xa6U, 0x9fU, 0x73U, 0xccU,
0xa2U, 0x3aU, 0x9aU, 0xc5U,
0xc8U, 0xb5U, 0x67U, 0xdcU,
0x18U, 0x5aU, 0x75U, 0x6eU,
0x97U, 0xc9U, 0x82U, 0x16U,
0x4fU, 0xe2U, 0x58U, 0x59U,
0xe0U, 0xd1U, 0xdcU, 0xc1U,
0x47U, 0x5cU, 0x80U, 0xa6U,
0x15U, 0xb2U, 0x12U, 0x3aU,
0xf1U, 0xf5U, 0xf9U, 0x4cU,
0x11U, 0xe3U, 0xe9U, 0x40U,
0x2cU, 0x3aU, 0xc5U, 0x58U,
0xf5U, 0x00U, 0x19U, 0x9dU,
0x95U, 0xb6U, 0xd3U, 0xe3U,
0x01U, 0x75U, 0x85U, 0x86U,
0x28U, 0x1dU, 0xcdU, 0x26U,
)
val actual = function.hash(
ubyteArrayOf()
)
assertContentEquals(expected, actual)
}
}

View File

@@ -0,0 +1,34 @@
package com.infendro.hash.keccak.shake
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
class `SHAKE128 Test` {
val function = SHAKE128(32)
@Test
fun `bytes and block`() {
assertEquals(32, function.bytes)
assertEquals(256, function.bits)
assertEquals(168, function.block)
}
@Test
fun `empty bytearray`() {
val expected = ubyteArrayOf(
0x7fU, 0x9cU, 0x2bU, 0xa4U,
0xe8U, 0x8fU, 0x82U, 0x7dU,
0x61U, 0x60U, 0x45U, 0x50U,
0x76U, 0x05U, 0x85U, 0x3eU,
0xd7U, 0x3bU, 0x80U, 0x93U,
0xf6U, 0xefU, 0xbcU, 0x88U,
0xebU, 0x1aU, 0x6eU, 0xacU,
0xfaU, 0x66U, 0xefU, 0x26U,
)
val actual = function.hash(
ubyteArrayOf()
)
assertContentEquals(expected, actual)
}
}

View File

@@ -0,0 +1,42 @@
package com.infendro.hash.keccak.shake
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
class `SHAKE256 Test` {
val function = SHAKE256(64)
@Test
fun `bytes and block`() {
assertEquals(64, function.bytes)
assertEquals(512, function.bits)
assertEquals(136, function.block)
}
@Test
fun `empty bytearray`() {
val expected = ubyteArrayOf(
0x46U, 0xb9U, 0xddU, 0x2bU,
0x0bU, 0xa8U, 0x8dU, 0x13U,
0x23U, 0x3bU, 0x3fU, 0xebU,
0x74U, 0x3eU, 0xebU, 0x24U,
0x3fU, 0xcdU, 0x52U, 0xeaU,
0x62U, 0xb8U, 0x1bU, 0x82U,
0xb5U, 0x0cU, 0x27U, 0x64U,
0x6eU, 0xd5U, 0x76U, 0x2fU,
0xd7U, 0x5dU, 0xc4U, 0xddU,
0xd8U, 0xc0U, 0xf2U, 0x00U,
0xcbU, 0x05U, 0x01U, 0x9dU,
0x67U, 0xb5U, 0x92U, 0xf6U,
0xfcU, 0x82U, 0x1cU, 0x49U,
0x47U, 0x9aU, 0xb4U, 0x86U,
0x40U, 0x29U, 0x2eU, 0xacU,
0xb3U, 0xb7U, 0xc4U, 0xbeU,
)
val actual = function.hash(
ubyteArrayOf()
)
assertContentEquals(expected, actual)
}
}

View File

@@ -0,0 +1,30 @@
package com.infendro.hash.md
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
class `MD5 Test` {
val function = MD5
@Test
fun `bytes and block`() {
assertEquals(16, function.bytes)
assertEquals(128, function.bits)
assertEquals(64, function.block)
}
@Test
fun `empty bytearray`() {
val expected = ubyteArrayOf(
0xd4U, 0x1dU, 0x8cU, 0xd9U,
0x8fU, 0x00U, 0xb2U, 0x04U,
0xe9U, 0x80U, 0x09U, 0x98U,
0xecU, 0xf8U, 0x42U, 0x7eU,
)
val actual = function.hash(
ubyteArrayOf()
)
assertContentEquals(expected, actual)
}
}

View File

@@ -0,0 +1,31 @@
package com.infendro.hash.sha1
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
class `SHA1 Test` {
val function = SHA1
@Test
fun `bytes and block`() {
assertEquals(20, function.bytes)
assertEquals(160, function.bits)
assertEquals(64, function.block)
}
@Test
fun `empty bytearray`() {
val expected = ubyteArrayOf(
0xdaU, 0x39U, 0xa3U, 0xeeU,
0x5eU, 0x6bU, 0x4bU, 0x0dU,
0x32U, 0x55U, 0xbfU, 0xefU,
0x95U, 0x60U, 0x18U, 0x90U,
0xafU, 0xd8U, 0x07U, 0x09U,
)
val actual = function.hash(
ubyteArrayOf()
)
assertContentEquals(expected, actual)
}
}

View File

@@ -0,0 +1,33 @@
package com.infendro.hash.sha2
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
class `SHA-224 Test` {
val function = `SHA-224`
@Test
fun `bytes and block`() {
assertEquals(28, function.bytes)
assertEquals(224, function.bits)
assertEquals(64, function.block)
}
@Test
fun `empty bytearray`() {
val expected = ubyteArrayOf(
0xd1U, 0x4aU, 0x02U, 0x8cU,
0x2aU, 0x3aU, 0x2bU, 0xc9U,
0x47U, 0x61U, 0x02U, 0xbbU,
0x28U, 0x82U, 0x34U, 0xc4U,
0x15U, 0xa2U, 0xb0U, 0x1fU,
0x82U, 0x8eU, 0xa6U, 0x2aU,
0xc5U, 0xb3U, 0xe4U, 0x2fU,
)
val actual = function.hash(
ubyteArrayOf()
)
assertContentEquals(expected, actual)
}
}

View File

@@ -0,0 +1,34 @@
package com.infendro.hash.sha2
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
class `SHA-256 Test` {
val function = `SHA-256`
@Test
fun `bytes and block`() {
assertEquals(32, function.bytes)
assertEquals(256, function.bits)
assertEquals(64, function.block)
}
@Test
fun `empty bytearray`() {
val expected = ubyteArrayOf(
0xe3U, 0xb0U, 0xc4U, 0x42U,
0x98U, 0xfcU, 0x1cU, 0x14U,
0x9aU, 0xfbU, 0xf4U, 0xc8U,
0x99U, 0x6fU, 0xb9U, 0x24U,
0x27U, 0xaeU, 0x41U, 0xe4U,
0x64U, 0x9bU, 0x93U, 0x4cU,
0xa4U, 0x95U, 0x99U, 0x1bU,
0x78U, 0x52U, 0xb8U, 0x55U,
)
val actual = function.hash(
ubyteArrayOf()
)
assertContentEquals(expected, actual)
}
}

View File

@@ -0,0 +1,38 @@
package com.infendro.hash.sha2
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
class `SHA-384 Test` {
val function = `SHA-384`
@Test
fun `bytes and block`() {
assertEquals(48, function.bytes)
assertEquals(384, function.bits)
assertEquals(128, function.block)
}
@Test
fun `empty bytearray`() {
val expected = ubyteArrayOf(
0x38U, 0xb0U, 0x60U, 0xa7U,
0x51U, 0xacU, 0x96U, 0x38U,
0x4cU, 0xd9U, 0x32U, 0x7eU,
0xb1U, 0xb1U, 0xe3U, 0x6aU,
0x21U, 0xfdU, 0xb7U, 0x11U,
0x14U, 0xbeU, 0x07U, 0x43U,
0x4cU, 0x0cU, 0xc7U, 0xbfU,
0x63U, 0xf6U, 0xe1U, 0xdaU,
0x27U, 0x4eU, 0xdeU, 0xbfU,
0xe7U, 0x6fU, 0x65U, 0xfbU,
0xd5U, 0x1aU, 0xd2U, 0xf1U,
0x48U, 0x98U, 0xb9U, 0x5bU,
)
val actual = function.hash(
ubyteArrayOf()
)
assertContentEquals(expected, actual)
}
}

View File

@@ -0,0 +1,42 @@
package com.infendro.hash.sha2
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
class `SHA-512 Test` {
val function = `SHA-512`
@Test
fun `bytes and block`() {
assertEquals(64, function.bytes)
assertEquals(512, function.bits)
assertEquals(128, function.block)
}
@Test
fun `empty bytearray`() {
val expected = ubyteArrayOf(
0xcfU, 0x83U, 0xe1U, 0x35U,
0x7eU, 0xefU, 0xb8U, 0xbdU,
0xf1U, 0x54U, 0x28U, 0x50U,
0xd6U, 0x6dU, 0x80U, 0x07U,
0xd6U, 0x20U, 0xe4U, 0x05U,
0x0bU, 0x57U, 0x15U, 0xdcU,
0x83U, 0xf4U, 0xa9U, 0x21U,
0xd3U, 0x6cU, 0xe9U, 0xceU,
0x47U, 0xd0U, 0xd1U, 0x3cU,
0x5dU, 0x85U, 0xf2U, 0xb0U,
0xffU, 0x83U, 0x18U, 0xd2U,
0x87U, 0x7eU, 0xecU, 0x2fU,
0x63U, 0xb9U, 0x31U, 0xbdU,
0x47U, 0x41U, 0x7aU, 0x81U,
0xa5U, 0x38U, 0x32U, 0x7aU,
0xf9U, 0x27U, 0xdaU, 0x3eU,
)
val actual = function.hash(
ubyteArrayOf()
)
assertContentEquals(expected, actual)
}
}