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

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

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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)

View File

@@ -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)

View File

@@ -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),
)
}
}
}

View File

@@ -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(),
)
}
}
}

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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