convert HashFunction to interface

This commit is contained in:
Infendro committed 2025-12-12 14:08:52 +01:00
1 parent 17b765b13c
commit 1ddda4889a
14 files changed
+19 -24

No files matched your search

@@ -1,13 +1,13 @@
package com.infendro.hash package com.infendro.hash
abstract class HashFunction { interface HashFunction {
abstract val bytes: Int val bytes: Int
val bits: Int val bits: Int
get() = bytes * 8 get() = bytes * 8
abstract val block: Int val block: Int
abstract fun hash( fun hash(
value: UByteArray, value: UByteArray,
): UByteArray ): UByteArray
} }
@@ -4,7 +4,7 @@ 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
object `Blake-224` : HashFunction() { object `Blake-224` : HashFunction {
override val bytes: Int override val bytes: Int
get() = 28 get() = 28
@@ -4,7 +4,7 @@ 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
object `Blake-256` : HashFunction() { object `Blake-256` : HashFunction {
override val bytes: Int override val bytes: Int
get() = 32 get() = 32
@@ -4,7 +4,7 @@ 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
object `Blake-384` : HashFunction() { object `Blake-384` : HashFunction {
override val bytes: Int override val bytes: Int
get() = 48 get() = 48
@@ -4,7 +4,7 @@ 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
object `Blake-512` : HashFunction() { object `Blake-512` : HashFunction {
override val bytes: Int override val bytes: Int
get() = 64 get() = 64
@@ -7,7 +7,7 @@ import com.infendro.hash.HashFunction
class Blake2b( class Blake2b(
override val bytes: Int, override val bytes: Int,
) : HashFunction() { ) : HashFunction {
init { init {
if (bytes !in 1..64) throw IllegalArgumentException() if (bytes !in 1..64) throw IllegalArgumentException()
} }
@@ -7,7 +7,7 @@ import com.infendro.hash.HashFunction
class Blake2s( class Blake2s(
override val bytes: Int, override val bytes: Int,
) : HashFunction() { ) : HashFunction {
init { init {
if (bytes !in 1..32) throw IllegalArgumentException() if (bytes !in 1..32) throw IllegalArgumentException()
} }
@@ -6,16 +6,11 @@ import com.infendro.bytearray.toULongArray
import com.infendro.hash.HashFunction import com.infendro.hash.HashFunction
import kotlin.math.min import kotlin.math.min
open class Keccak( abstract class Keccak(
override val bytes: Int, override val bytes: Int,
private val rate: Int, private val rate: Int,
private val domain: UByte, private val domain: UByte,
) : HashFunction() { ) : HashFunction {
init {
if (bytes < 1) throw IllegalArgumentException()
if (rate !in 8..168) throw IllegalArgumentException()
}
override val block: Int override val block: Int
get() = rate get() = rate
@@ -108,7 +103,7 @@ open class Keccak(
} }
} }
fun squeeze( private fun squeeze(
state: ULongArray, state: ULongArray,
): UByteArray { ): UByteArray {
return buildList { return buildList {
@@ -5,7 +5,7 @@ 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
object MD5 : HashFunction() { object MD5 : HashFunction {
override val bytes: Int override val bytes: Int
get() = 16 get() = 16
@@ -4,7 +4,7 @@ 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
object SHA1 : HashFunction() { object SHA1 : HashFunction {
override val bytes: Int override val bytes: Int
get() = 20 get() = 20
@@ -4,7 +4,7 @@ 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
object `SHA-224` : HashFunction() { object `SHA-224` : HashFunction {
override val bytes: Int override val bytes: Int
get() = 28 get() = 28
@@ -4,7 +4,7 @@ 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
object `SHA-256` : HashFunction() { object `SHA-256` : HashFunction {
override val bytes: Int override val bytes: Int
get() = 32 get() = 32
@@ -4,7 +4,7 @@ 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
object `SHA-384` : HashFunction() { object `SHA-384` : HashFunction {
override val bytes: Int override val bytes: Int
get() = 48 get() = 48
@@ -4,7 +4,7 @@ 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
object `SHA-512` : HashFunction() { object `SHA-512` : HashFunction {
override val bytes: Int override val bytes: Int
get() = 64 get() = 64