diff --git a/src/commonMain/kotlin/com/infendro/hash/keccak/Keccak.kt b/src/commonMain/kotlin/com/infendro/hash/keccak/Keccak.kt index 7323e00..51db5b8 100644 --- a/src/commonMain/kotlin/com/infendro/hash/keccak/Keccak.kt +++ b/src/commonMain/kotlin/com/infendro/hash/keccak/Keccak.kt @@ -6,11 +6,16 @@ import com.infendro.bytes.long.copyInto import com.infendro.hash.HashFunction import com.infendro.hash.util.ceil -abstract class Keccak( +open class Keccak( override val bytes: Int, private val rate: Int, private val domain: Byte, ) : HashFunction { + init { + require(bytes > 0) + require(rate in 8..192 step 8) + } + override val block: Int get() = rate @@ -64,10 +69,12 @@ abstract class Keccak( private fun squeeze(state: LongArray, b: LongArray, c: LongArray, d: LongArray, destination: ByteArray) { val buffer = ByteArray(8) + val threshold = rate / 8 + var consumed = 0 var i = 0 while (consumed < bytes) { - if (i == rate / 8) { + if (i == threshold) { permute(state, b, c, d) i = 0 }