diff --git a/src/commonMain/kotlin/com/infendro/hash/HashFunction.kt b/src/commonMain/kotlin/com/infendro/hash/HashFunction.kt index 676de51..34e40d9 100644 --- a/src/commonMain/kotlin/com/infendro/hash/HashFunction.kt +++ b/src/commonMain/kotlin/com/infendro/hash/HashFunction.kt @@ -1,13 +1,13 @@ package com.infendro.hash -abstract class HashFunction { - abstract val bytes: Int +interface HashFunction { + val bytes: Int val bits: Int get() = bytes * 8 - abstract val block: Int + val block: Int - abstract fun hash( + fun hash( value: UByteArray, ): UByteArray } diff --git a/src/commonMain/kotlin/com/infendro/hash/blake/Blake-224.kt b/src/commonMain/kotlin/com/infendro/hash/blake/Blake-224.kt index d9765a3..68d02ef 100644 --- a/src/commonMain/kotlin/com/infendro/hash/blake/Blake-224.kt +++ b/src/commonMain/kotlin/com/infendro/hash/blake/Blake-224.kt @@ -4,7 +4,7 @@ import com.infendro.bytearray.toUByteArray import com.infendro.bytearray.toUIntArray import com.infendro.hash.HashFunction -object `Blake-224` : HashFunction() { +object `Blake-224` : HashFunction { override val bytes: Int get() = 28 diff --git a/src/commonMain/kotlin/com/infendro/hash/blake/Blake-256.kt b/src/commonMain/kotlin/com/infendro/hash/blake/Blake-256.kt index 380d424..f55bf6e 100644 --- a/src/commonMain/kotlin/com/infendro/hash/blake/Blake-256.kt +++ b/src/commonMain/kotlin/com/infendro/hash/blake/Blake-256.kt @@ -4,7 +4,7 @@ import com.infendro.bytearray.toUByteArray import com.infendro.bytearray.toUIntArray import com.infendro.hash.HashFunction -object `Blake-256` : HashFunction() { +object `Blake-256` : HashFunction { override val bytes: Int get() = 32 diff --git a/src/commonMain/kotlin/com/infendro/hash/blake/Blake-384.kt b/src/commonMain/kotlin/com/infendro/hash/blake/Blake-384.kt index 45518e3..47f9bcb 100644 --- a/src/commonMain/kotlin/com/infendro/hash/blake/Blake-384.kt +++ b/src/commonMain/kotlin/com/infendro/hash/blake/Blake-384.kt @@ -4,7 +4,7 @@ import com.infendro.bytearray.toUByteArray import com.infendro.bytearray.toULongArray import com.infendro.hash.HashFunction -object `Blake-384` : HashFunction() { +object `Blake-384` : HashFunction { override val bytes: Int get() = 48 diff --git a/src/commonMain/kotlin/com/infendro/hash/blake/Blake-512.kt b/src/commonMain/kotlin/com/infendro/hash/blake/Blake-512.kt index d1329fb..9a1821c 100644 --- a/src/commonMain/kotlin/com/infendro/hash/blake/Blake-512.kt +++ b/src/commonMain/kotlin/com/infendro/hash/blake/Blake-512.kt @@ -4,7 +4,7 @@ import com.infendro.bytearray.toUByteArray import com.infendro.bytearray.toULongArray import com.infendro.hash.HashFunction -object `Blake-512` : HashFunction() { +object `Blake-512` : HashFunction { override val bytes: Int get() = 64 diff --git a/src/commonMain/kotlin/com/infendro/hash/blake2/Blake2b.kt b/src/commonMain/kotlin/com/infendro/hash/blake2/Blake2b.kt index 0142418..95bc274 100644 --- a/src/commonMain/kotlin/com/infendro/hash/blake2/Blake2b.kt +++ b/src/commonMain/kotlin/com/infendro/hash/blake2/Blake2b.kt @@ -7,7 +7,7 @@ import com.infendro.hash.HashFunction class Blake2b( override val bytes: Int, -) : HashFunction() { +) : HashFunction { init { if (bytes !in 1..64) throw IllegalArgumentException() } diff --git a/src/commonMain/kotlin/com/infendro/hash/blake2/Blake2s.kt b/src/commonMain/kotlin/com/infendro/hash/blake2/Blake2s.kt index b18c252..63460eb 100644 --- a/src/commonMain/kotlin/com/infendro/hash/blake2/Blake2s.kt +++ b/src/commonMain/kotlin/com/infendro/hash/blake2/Blake2s.kt @@ -7,7 +7,7 @@ import com.infendro.hash.HashFunction class Blake2s( override val bytes: Int, -) : HashFunction() { +) : HashFunction { init { if (bytes !in 1..32) throw IllegalArgumentException() } diff --git a/src/commonMain/kotlin/com/infendro/hash/keccak/Keccak.kt b/src/commonMain/kotlin/com/infendro/hash/keccak/Keccak.kt index 7d92f9e..15a0cb2 100644 --- a/src/commonMain/kotlin/com/infendro/hash/keccak/Keccak.kt +++ b/src/commonMain/kotlin/com/infendro/hash/keccak/Keccak.kt @@ -6,16 +6,11 @@ import com.infendro.bytearray.toULongArray import com.infendro.hash.HashFunction import kotlin.math.min -open class Keccak( +abstract class Keccak( override val bytes: Int, private val rate: Int, private val domain: UByte, -) : HashFunction() { - init { - if (bytes < 1) throw IllegalArgumentException() - if (rate !in 8..168) throw IllegalArgumentException() - } - +) : HashFunction { override val block: Int get() = rate @@ -108,7 +103,7 @@ open class Keccak( } } - fun squeeze( + private fun squeeze( state: ULongArray, ): UByteArray { return buildList { diff --git a/src/commonMain/kotlin/com/infendro/hash/md/MD5.kt b/src/commonMain/kotlin/com/infendro/hash/md/MD5.kt index a34127c..132ed5c 100644 --- a/src/commonMain/kotlin/com/infendro/hash/md/MD5.kt +++ b/src/commonMain/kotlin/com/infendro/hash/md/MD5.kt @@ -5,7 +5,7 @@ import com.infendro.bytearray.toUByteArray import com.infendro.bytearray.toUIntArray import com.infendro.hash.HashFunction -object MD5 : HashFunction() { +object MD5 : HashFunction { override val bytes: Int get() = 16 diff --git a/src/commonMain/kotlin/com/infendro/hash/sha1/SHA1.kt b/src/commonMain/kotlin/com/infendro/hash/sha1/SHA1.kt index c0f4c16..236b0b5 100644 --- a/src/commonMain/kotlin/com/infendro/hash/sha1/SHA1.kt +++ b/src/commonMain/kotlin/com/infendro/hash/sha1/SHA1.kt @@ -4,7 +4,7 @@ import com.infendro.bytearray.toUByteArray import com.infendro.bytearray.toUIntArray import com.infendro.hash.HashFunction -object SHA1 : HashFunction() { +object SHA1 : HashFunction { override val bytes: Int get() = 20 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 f051958..d08ed67 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,7 @@ import com.infendro.bytearray.toUByteArray import com.infendro.bytearray.toUIntArray import com.infendro.hash.HashFunction -object `SHA-224` : HashFunction() { +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 e97b49d..b47a39c 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,7 @@ import com.infendro.bytearray.toUByteArray import com.infendro.bytearray.toUIntArray import com.infendro.hash.HashFunction -object `SHA-256` : HashFunction() { +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 b2c6fa6..fb536cf 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,7 @@ import com.infendro.bytearray.toUByteArray import com.infendro.bytearray.toULongArray import com.infendro.hash.HashFunction -object `SHA-384` : HashFunction() { +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 3df9481..c0dc89c 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,7 @@ import com.infendro.bytearray.toUByteArray import com.infendro.bytearray.toULongArray import com.infendro.hash.HashFunction -object `SHA-512` : HashFunction() { +object `SHA-512` : HashFunction { override val bytes: Int get() = 64