diff --git a/src/commonMain/kotlin/com/infendro/hash/util/Buffer.kt b/src/commonMain/kotlin/com/infendro/hash/Buffer.kt similarity index 79% rename from src/commonMain/kotlin/com/infendro/hash/util/Buffer.kt rename to src/commonMain/kotlin/com/infendro/hash/Buffer.kt index 7f35e10..81ae8ee 100644 --- a/src/commonMain/kotlin/com/infendro/hash/util/Buffer.kt +++ b/src/commonMain/kotlin/com/infendro/hash/Buffer.kt @@ -1,11 +1,12 @@ -package com.infendro.hash.util +package com.infendro.hash import com.infendro.bytes.ByteOrder -import com.infendro.bytes.ByteOrder.BIG_ENDIAN import com.infendro.bytes.int.copyInto import com.infendro.bytes.long.copyInto -internal class Buffer(val size: Int) { +class Buffer( + val size: Int, +) { val value = ByteArray(size) var offset = 0 @@ -34,12 +35,12 @@ internal class Buffer(val size: Int) { offset += (endIndex - startIndex) } - fun append(n: Int, order: ByteOrder = BIG_ENDIAN) { + fun append(n: Int, order: ByteOrder = ByteOrder.BIG_ENDIAN) { n.copyInto(value, offset, order = order) offset += 4 } - fun append(n: Long, order: ByteOrder = BIG_ENDIAN) { + fun append(n: Long, order: ByteOrder = ByteOrder.BIG_ENDIAN) { n.copyInto(value, offset, order = order) offset += 8 } diff --git a/src/commonMain/kotlin/com/infendro/hash/HashFunction.kt b/src/commonMain/kotlin/com/infendro/hash/HashFunction.kt index ccf72ba..d9acd6b 100644 --- a/src/commonMain/kotlin/com/infendro/hash/HashFunction.kt +++ b/src/commonMain/kotlin/com/infendro/hash/HashFunction.kt @@ -1,7 +1,5 @@ package com.infendro.hash -import com.infendro.hash.util.Buffer - abstract class HashFunction( val bytes: Int, val block: Int, @@ -9,8 +7,8 @@ abstract class HashFunction( val bits: Int get() = bytes * 8 - internal val buffer = Buffer(block) - internal var length = 0L + protected val buffer = Buffer(block) + protected var length = 0L open fun reset() { buffer.reset() @@ -29,28 +27,18 @@ abstract class HashFunction( open fun update(bytes: ByteArray) { var i = 0 - // (partial) first block - val free = buffer.free - if (i + free < bytes.size) { + var free = buffer.free + while (i + free < bytes.size) { buffer.append(bytes, startIndex = i, endIndex = i + free) length += free process() buffer.reset() - i += free - } - - // full blocks - while (i + block < bytes.size) { - buffer.append(bytes, startIndex = i, endIndex = i + block) - length += block - process() - buffer.reset() + free = buffer.free i += block } - // (partial) last block buffer.append(bytes, startIndex = i) - length += bytes.size - i + length += (bytes.size - i) if (buffer.isFull()) { process() buffer.reset() diff --git a/src/commonMain/kotlin/com/infendro/hash/blake2/Blake2b.kt b/src/commonMain/kotlin/com/infendro/hash/blake2/Blake2b.kt index e0db34b..3ec27a2 100644 --- a/src/commonMain/kotlin/com/infendro/hash/blake2/Blake2b.kt +++ b/src/commonMain/kotlin/com/infendro/hash/blake2/Blake2b.kt @@ -38,15 +38,21 @@ class Blake2b(bytes: Int) : HashFunction(bytes, 128) { process() buffer.reset() } + var i = 0 - while (i + buffer.free < bytes.size) { - val remaining = buffer.free - buffer.append(bytes, startIndex = i, endIndex = i + remaining) + + var free = buffer.free + while (i + free < bytes.size) { + buffer.append(bytes, startIndex = i, endIndex = i + free) + length += free process() buffer.reset() - i += remaining + free = buffer.free + i += block } + buffer.append(bytes, startIndex = i) + length += (bytes.size - i) } override fun process() = compress(buffer.value, length, false) diff --git a/src/commonMain/kotlin/com/infendro/hash/blake2/Blake2s.kt b/src/commonMain/kotlin/com/infendro/hash/blake2/Blake2s.kt index ed8c74d..64d3830 100644 --- a/src/commonMain/kotlin/com/infendro/hash/blake2/Blake2s.kt +++ b/src/commonMain/kotlin/com/infendro/hash/blake2/Blake2s.kt @@ -38,15 +38,21 @@ class Blake2s(bytes: Int) : HashFunction(bytes, 64) { process() buffer.reset() } + var i = 0 - while (i + buffer.free < bytes.size) { - val remaining = buffer.free - buffer.append(bytes, startIndex = i, endIndex = i + remaining) + + var free = buffer.free + while (i + free < bytes.size) { + buffer.append(bytes, startIndex = i, endIndex = i + free) + length += free process() buffer.reset() - i += remaining + free = buffer.free + i += block } + buffer.append(bytes, startIndex = i) + length += (bytes.size - i) } override fun process() = compress(buffer.value, length, false)