From 11154e4bea0e7ef0f1aa0660f12155fdd9fc87f2 Mon Sep 17 00:00:00 2001 From: Infendro Date: Mon, 10 Nov 2025 17:40:58 +0100 Subject: [PATCH] implement tests also fix MD5 --- build.gradle.kts | 3 ++ gradle/libs.versions.toml | 1 + .../kotlin/com/infendro/hash/HashFunction.kt | 2 - .../kotlin/com/infendro/hash/keccak/Keccak.kt | 15 +++---- .../com/infendro/hash/keccak/sha3/SHA3-224.kt | 6 ++- .../com/infendro/hash/keccak/sha3/SHA3-256.kt | 6 ++- .../com/infendro/hash/keccak/sha3/SHA3-384.kt | 6 ++- .../com/infendro/hash/keccak/sha3/SHA3-512.kt | 6 ++- .../infendro/hash/keccak/shake/SHAKE128.kt | 5 ++- .../infendro/hash/keccak/shake/SHAKE256.kt | 5 ++- .../kotlin/com/infendro/hash/md/MD5.kt | 6 +-- .../kotlin/com/infendro/hash/sha1/SHA1.kt | 3 +- .../kotlin/com/infendro/hash/sha2/SHA-224.kt | 1 - .../kotlin/com/infendro/hash/sha2/SHA-256.kt | 1 - .../kotlin/com/infendro/hash/sha2/SHA-384.kt | 1 - .../kotlin/com/infendro/hash/sha2/SHA-512.kt | 1 - .../com/infendro/hash/keccak/sha3/SHA3-224.kt | 33 +++++++++++++++ .../com/infendro/hash/keccak/sha3/SHA3-256.kt | 34 +++++++++++++++ .../com/infendro/hash/keccak/sha3/SHA3-384.kt | 39 +++++++++++++++++ .../com/infendro/hash/keccak/sha3/SHA3-512.kt | 42 +++++++++++++++++++ .../infendro/hash/keccak/shake/SHAKE128.kt | 34 +++++++++++++++ .../infendro/hash/keccak/shake/SHAKE256.kt | 42 +++++++++++++++++++ .../kotlin/com/infendro/hash/md/MD5.kt | 30 +++++++++++++ .../kotlin/com/infendro/hash/sha1/SHA1.kt | 31 ++++++++++++++ .../kotlin/com/infendro/hash/sha2/SHA-224.kt | 33 +++++++++++++++ .../kotlin/com/infendro/hash/sha2/SHA-256.kt | 34 +++++++++++++++ .../kotlin/com/infendro/hash/sha2/SHA-384.kt | 38 +++++++++++++++++ .../kotlin/com/infendro/hash/sha2/SHA-512.kt | 42 +++++++++++++++++++ 28 files changed, 470 insertions(+), 30 deletions(-) create mode 100644 src/commonTest/kotlin/com/infendro/hash/keccak/sha3/SHA3-224.kt create mode 100644 src/commonTest/kotlin/com/infendro/hash/keccak/sha3/SHA3-256.kt create mode 100644 src/commonTest/kotlin/com/infendro/hash/keccak/sha3/SHA3-384.kt create mode 100644 src/commonTest/kotlin/com/infendro/hash/keccak/sha3/SHA3-512.kt create mode 100644 src/commonTest/kotlin/com/infendro/hash/keccak/shake/SHAKE128.kt create mode 100644 src/commonTest/kotlin/com/infendro/hash/keccak/shake/SHAKE256.kt create mode 100644 src/commonTest/kotlin/com/infendro/hash/md/MD5.kt create mode 100644 src/commonTest/kotlin/com/infendro/hash/sha1/SHA1.kt create mode 100644 src/commonTest/kotlin/com/infendro/hash/sha2/SHA-224.kt create mode 100644 src/commonTest/kotlin/com/infendro/hash/sha2/SHA-256.kt create mode 100644 src/commonTest/kotlin/com/infendro/hash/sha2/SHA-384.kt create mode 100644 src/commonTest/kotlin/com/infendro/hash/sha2/SHA-512.kt diff --git a/build.gradle.kts b/build.gradle.kts index c59315b..6c78e33 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -22,6 +22,9 @@ kotlin { commonMain.dependencies { implementation(libs.bytearray) } + commonTest.dependencies { + implementation(libs.test) + } } compilerOptions { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index fd1cb28..d04322b 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -7,3 +7,4 @@ multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotl [libraries] bytearray = { module = "com.infendro:bytearray", version.ref = "bytearray" } +test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" } diff --git a/src/commonMain/kotlin/com/infendro/hash/HashFunction.kt b/src/commonMain/kotlin/com/infendro/hash/HashFunction.kt index c9aeef0..676de51 100644 --- a/src/commonMain/kotlin/com/infendro/hash/HashFunction.kt +++ b/src/commonMain/kotlin/com/infendro/hash/HashFunction.kt @@ -2,8 +2,6 @@ package com.infendro.hash abstract class HashFunction { abstract val bytes: Int - - @Suppress("UNUSED") val bits: Int get() = bytes * 8 diff --git a/src/commonMain/kotlin/com/infendro/hash/keccak/Keccak.kt b/src/commonMain/kotlin/com/infendro/hash/keccak/Keccak.kt index 970534f..27bdcb6 100644 --- a/src/commonMain/kotlin/com/infendro/hash/keccak/Keccak.kt +++ b/src/commonMain/kotlin/com/infendro/hash/keccak/Keccak.kt @@ -6,12 +6,13 @@ import com.infendro.bytearray.toULongArray import com.infendro.hash.HashFunction import kotlin.math.min -@Suppress("UNUSED") -open class Keccak internal constructor( - override val bytes: Int, - override val block: Int, +abstract class Keccak internal constructor( + private val rate: Int, private val domain: UByte, ) : HashFunction() { + override val block: Int + get() = rate + private val rc = ulongArrayOf( 0x0000000000000001UL, 0x0000000000008082UL, 0x800000000000808aUL, 0x8000000080008000UL, 0x000000000000808bUL, 0x0000000080000001UL, 0x8000000080008081UL, 0x8000000000008009UL, @@ -35,7 +36,7 @@ open class Keccak internal constructor( val paddedValue = buildList { addAll(value) add(domain) - while ((size % block) != (block - 1)) { + while ((size % rate) != (rate - 1)) { add(0x00U) } add(0x80U) @@ -45,7 +46,7 @@ open class Keccak internal constructor( // absorb val chunks = paddedValue - .chunked(block) { it.toUByteArray() } + .chunked(rate) { it.toUByteArray() } for (chunk in chunks) { val numbers = chunk.toULongArray(LITTLE_ENDIAN) for ((i, number) in numbers.withIndex()) { @@ -58,7 +59,7 @@ open class Keccak internal constructor( return buildList { var i = 0 while (size < bytes) { - if (i == block / 8) { + if (i == rate / 8) { permute(state) i = 0 } diff --git a/src/commonMain/kotlin/com/infendro/hash/keccak/sha3/SHA3-224.kt b/src/commonMain/kotlin/com/infendro/hash/keccak/sha3/SHA3-224.kt index 9b99e80..a62b414 100644 --- a/src/commonMain/kotlin/com/infendro/hash/keccak/sha3/SHA3-224.kt +++ b/src/commonMain/kotlin/com/infendro/hash/keccak/sha3/SHA3-224.kt @@ -2,5 +2,7 @@ package com.infendro.hash.keccak.sha3 import com.infendro.hash.keccak.Keccak -@Suppress("UNUSED") -object `SHA3-224` : Keccak(28, 144, 0x06U) +object `SHA3-224` : Keccak(144, 0x06U) { + override val bytes: Int + get() = 28 +} diff --git a/src/commonMain/kotlin/com/infendro/hash/keccak/sha3/SHA3-256.kt b/src/commonMain/kotlin/com/infendro/hash/keccak/sha3/SHA3-256.kt index 323f3b9..f3f5c56 100644 --- a/src/commonMain/kotlin/com/infendro/hash/keccak/sha3/SHA3-256.kt +++ b/src/commonMain/kotlin/com/infendro/hash/keccak/sha3/SHA3-256.kt @@ -2,5 +2,7 @@ package com.infendro.hash.keccak.sha3 import com.infendro.hash.keccak.Keccak -@Suppress("UNUSED") -object `SHA3-256` : Keccak(32, 136, 0x06U) +object `SHA3-256` : Keccak(136, 0x06U) { + override val bytes: Int + get() = 32 +} diff --git a/src/commonMain/kotlin/com/infendro/hash/keccak/sha3/SHA3-384.kt b/src/commonMain/kotlin/com/infendro/hash/keccak/sha3/SHA3-384.kt index 324d4e8..07f0367 100644 --- a/src/commonMain/kotlin/com/infendro/hash/keccak/sha3/SHA3-384.kt +++ b/src/commonMain/kotlin/com/infendro/hash/keccak/sha3/SHA3-384.kt @@ -2,5 +2,7 @@ package com.infendro.hash.keccak.sha3 import com.infendro.hash.keccak.Keccak -@Suppress("UNUSED") -object `SHA3-384` : Keccak(48, 104, 0x06U) +object `SHA3-384` : Keccak(104, 0x06U) { + override val bytes: Int + get() = 48 +} diff --git a/src/commonMain/kotlin/com/infendro/hash/keccak/sha3/SHA3-512.kt b/src/commonMain/kotlin/com/infendro/hash/keccak/sha3/SHA3-512.kt index f796714..13bb0ee 100644 --- a/src/commonMain/kotlin/com/infendro/hash/keccak/sha3/SHA3-512.kt +++ b/src/commonMain/kotlin/com/infendro/hash/keccak/sha3/SHA3-512.kt @@ -2,5 +2,7 @@ package com.infendro.hash.keccak.sha3 import com.infendro.hash.keccak.Keccak -@Suppress("UNUSED") -object `SHA3-512` : Keccak(64, 72, 0x06U) +object `SHA3-512` : Keccak(72, 0x06U) { + override val bytes: Int + get() = 64 +} diff --git a/src/commonMain/kotlin/com/infendro/hash/keccak/shake/SHAKE128.kt b/src/commonMain/kotlin/com/infendro/hash/keccak/shake/SHAKE128.kt index a998e8f..0f70d8f 100644 --- a/src/commonMain/kotlin/com/infendro/hash/keccak/shake/SHAKE128.kt +++ b/src/commonMain/kotlin/com/infendro/hash/keccak/shake/SHAKE128.kt @@ -2,5 +2,6 @@ package com.infendro.hash.keccak.shake import com.infendro.hash.keccak.Keccak -@Suppress("UNUSED") -class SHAKE128(bytes: Int) : Keccak(bytes, 168, 0x1FU) +class SHAKE128( + override val bytes: Int, +) : Keccak(168, 0x1FU) diff --git a/src/commonMain/kotlin/com/infendro/hash/keccak/shake/SHAKE256.kt b/src/commonMain/kotlin/com/infendro/hash/keccak/shake/SHAKE256.kt index efcb0fc..fa509fb 100644 --- a/src/commonMain/kotlin/com/infendro/hash/keccak/shake/SHAKE256.kt +++ b/src/commonMain/kotlin/com/infendro/hash/keccak/shake/SHAKE256.kt @@ -2,5 +2,6 @@ package com.infendro.hash.keccak.shake import com.infendro.hash.keccak.Keccak -@Suppress("UNUSED") -class SHAKE256(bytes: Int) : Keccak(bytes, 136, 0x1FU) +class SHAKE256( + override val bytes: Int, +) : Keccak(136, 0x1FU) diff --git a/src/commonMain/kotlin/com/infendro/hash/md/MD5.kt b/src/commonMain/kotlin/com/infendro/hash/md/MD5.kt index 1ede110..6d0a240 100644 --- a/src/commonMain/kotlin/com/infendro/hash/md/MD5.kt +++ b/src/commonMain/kotlin/com/infendro/hash/md/MD5.kt @@ -5,7 +5,6 @@ import com.infendro.bytearray.toUByteArray import com.infendro.bytearray.toUIntArray import com.infendro.hash.HashFunction -@Suppress("UNUSED") object MD5 : HashFunction() { override val bytes: Int get() = 16 @@ -94,10 +93,11 @@ object MD5 : HashFunction() { in 32..47 -> (3 * i + 5) % 16 else -> (7 * i) % 16 } + val temp = a a = d d = c c = b - b += (f + a + k[i] + w[g]).rotateLeft(s[i]) + b += (temp + f + k[i] + w[g]).rotateLeft(s[i]) } a0 += a @@ -113,4 +113,4 @@ object MD5 : HashFunction() { *d0.toUByteArray(LITTLE_ENDIAN), ) } -} \ No newline at end of file +} diff --git a/src/commonMain/kotlin/com/infendro/hash/sha1/SHA1.kt b/src/commonMain/kotlin/com/infendro/hash/sha1/SHA1.kt index 3ab84a0..f49a26a 100644 --- a/src/commonMain/kotlin/com/infendro/hash/sha1/SHA1.kt +++ b/src/commonMain/kotlin/com/infendro/hash/sha1/SHA1.kt @@ -4,7 +4,6 @@ import com.infendro.bytearray.toUByteArray import com.infendro.bytearray.toUIntArray import com.infendro.hash.HashFunction -@Suppress("UNUSED") object SHA1 : HashFunction() { override val bytes: Int get() = 20 @@ -84,4 +83,4 @@ object SHA1 : HashFunction() { *h4.toUByteArray(), ) } -} \ No newline at end of file +} diff --git a/src/commonMain/kotlin/com/infendro/hash/sha2/SHA-224.kt b/src/commonMain/kotlin/com/infendro/hash/sha2/SHA-224.kt index 4ae5c5c..823ca22 100644 --- a/src/commonMain/kotlin/com/infendro/hash/sha2/SHA-224.kt +++ b/src/commonMain/kotlin/com/infendro/hash/sha2/SHA-224.kt @@ -4,7 +4,6 @@ import com.infendro.bytearray.toUByteArray import com.infendro.bytearray.toUIntArray import com.infendro.hash.HashFunction -@Suppress("UNUSED") object `SHA-224` : HashFunction() { override val bytes: Int get() = 28 diff --git a/src/commonMain/kotlin/com/infendro/hash/sha2/SHA-256.kt b/src/commonMain/kotlin/com/infendro/hash/sha2/SHA-256.kt index f7e2b2d..10239b9 100644 --- a/src/commonMain/kotlin/com/infendro/hash/sha2/SHA-256.kt +++ b/src/commonMain/kotlin/com/infendro/hash/sha2/SHA-256.kt @@ -4,7 +4,6 @@ import com.infendro.bytearray.toUByteArray import com.infendro.bytearray.toUIntArray import com.infendro.hash.HashFunction -@Suppress("UNUSED") object `SHA-256` : HashFunction() { override val bytes: Int get() = 32 diff --git a/src/commonMain/kotlin/com/infendro/hash/sha2/SHA-384.kt b/src/commonMain/kotlin/com/infendro/hash/sha2/SHA-384.kt index 9bb4af0..90b8fff 100644 --- a/src/commonMain/kotlin/com/infendro/hash/sha2/SHA-384.kt +++ b/src/commonMain/kotlin/com/infendro/hash/sha2/SHA-384.kt @@ -4,7 +4,6 @@ import com.infendro.bytearray.toUByteArray import com.infendro.bytearray.toULongArray import com.infendro.hash.HashFunction -@Suppress("UNUSED") object `SHA-384` : HashFunction() { override val bytes: Int get() = 48 diff --git a/src/commonMain/kotlin/com/infendro/hash/sha2/SHA-512.kt b/src/commonMain/kotlin/com/infendro/hash/sha2/SHA-512.kt index ff704ca..7cfc5e9 100644 --- a/src/commonMain/kotlin/com/infendro/hash/sha2/SHA-512.kt +++ b/src/commonMain/kotlin/com/infendro/hash/sha2/SHA-512.kt @@ -4,7 +4,6 @@ import com.infendro.bytearray.toUByteArray import com.infendro.bytearray.toULongArray import com.infendro.hash.HashFunction -@Suppress("UNUSED") object `SHA-512` : HashFunction() { override val bytes: Int get() = 64 diff --git a/src/commonTest/kotlin/com/infendro/hash/keccak/sha3/SHA3-224.kt b/src/commonTest/kotlin/com/infendro/hash/keccak/sha3/SHA3-224.kt new file mode 100644 index 0000000..19a5982 --- /dev/null +++ b/src/commonTest/kotlin/com/infendro/hash/keccak/sha3/SHA3-224.kt @@ -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) + } +} diff --git a/src/commonTest/kotlin/com/infendro/hash/keccak/sha3/SHA3-256.kt b/src/commonTest/kotlin/com/infendro/hash/keccak/sha3/SHA3-256.kt new file mode 100644 index 0000000..815de1c --- /dev/null +++ b/src/commonTest/kotlin/com/infendro/hash/keccak/sha3/SHA3-256.kt @@ -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) + } +} diff --git a/src/commonTest/kotlin/com/infendro/hash/keccak/sha3/SHA3-384.kt b/src/commonTest/kotlin/com/infendro/hash/keccak/sha3/SHA3-384.kt new file mode 100644 index 0000000..c140c96 --- /dev/null +++ b/src/commonTest/kotlin/com/infendro/hash/keccak/sha3/SHA3-384.kt @@ -0,0 +1,39 @@ +package com.infendro.hash.keccak.sha3 + +import kotlin.test.Test +import kotlin.test.assertContentEquals +import kotlin.test.assertEquals +import kotlin.test.assertSame + +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) + } +} diff --git a/src/commonTest/kotlin/com/infendro/hash/keccak/sha3/SHA3-512.kt b/src/commonTest/kotlin/com/infendro/hash/keccak/sha3/SHA3-512.kt new file mode 100644 index 0000000..1e29b02 --- /dev/null +++ b/src/commonTest/kotlin/com/infendro/hash/keccak/sha3/SHA3-512.kt @@ -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) + } +} diff --git a/src/commonTest/kotlin/com/infendro/hash/keccak/shake/SHAKE128.kt b/src/commonTest/kotlin/com/infendro/hash/keccak/shake/SHAKE128.kt new file mode 100644 index 0000000..9d29cbf --- /dev/null +++ b/src/commonTest/kotlin/com/infendro/hash/keccak/shake/SHAKE128.kt @@ -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) + } +} diff --git a/src/commonTest/kotlin/com/infendro/hash/keccak/shake/SHAKE256.kt b/src/commonTest/kotlin/com/infendro/hash/keccak/shake/SHAKE256.kt new file mode 100644 index 0000000..8086dfe --- /dev/null +++ b/src/commonTest/kotlin/com/infendro/hash/keccak/shake/SHAKE256.kt @@ -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) + } +} diff --git a/src/commonTest/kotlin/com/infendro/hash/md/MD5.kt b/src/commonTest/kotlin/com/infendro/hash/md/MD5.kt new file mode 100644 index 0000000..7d95d69 --- /dev/null +++ b/src/commonTest/kotlin/com/infendro/hash/md/MD5.kt @@ -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) + } +} diff --git a/src/commonTest/kotlin/com/infendro/hash/sha1/SHA1.kt b/src/commonTest/kotlin/com/infendro/hash/sha1/SHA1.kt new file mode 100644 index 0000000..1b8101f --- /dev/null +++ b/src/commonTest/kotlin/com/infendro/hash/sha1/SHA1.kt @@ -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) + } +} diff --git a/src/commonTest/kotlin/com/infendro/hash/sha2/SHA-224.kt b/src/commonTest/kotlin/com/infendro/hash/sha2/SHA-224.kt new file mode 100644 index 0000000..f97d449 --- /dev/null +++ b/src/commonTest/kotlin/com/infendro/hash/sha2/SHA-224.kt @@ -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) + } +} diff --git a/src/commonTest/kotlin/com/infendro/hash/sha2/SHA-256.kt b/src/commonTest/kotlin/com/infendro/hash/sha2/SHA-256.kt new file mode 100644 index 0000000..5bf43c5 --- /dev/null +++ b/src/commonTest/kotlin/com/infendro/hash/sha2/SHA-256.kt @@ -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) + } +} diff --git a/src/commonTest/kotlin/com/infendro/hash/sha2/SHA-384.kt b/src/commonTest/kotlin/com/infendro/hash/sha2/SHA-384.kt new file mode 100644 index 0000000..eb09dc3 --- /dev/null +++ b/src/commonTest/kotlin/com/infendro/hash/sha2/SHA-384.kt @@ -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) + } +} diff --git a/src/commonTest/kotlin/com/infendro/hash/sha2/SHA-512.kt b/src/commonTest/kotlin/com/infendro/hash/sha2/SHA-512.kt new file mode 100644 index 0000000..fd4bbe2 --- /dev/null +++ b/src/commonTest/kotlin/com/infendro/hash/sha2/SHA-512.kt @@ -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) + } +}