Compare commits

...

3 Commits

Author SHA1 Message Date
8627e2c562 Update README.md 2026-04-16 16:11:28 +00:00
8400f76964 Upgrade Dependency
All checks were successful
/ test (pull_request) Successful in 47s
2026-03-11 22:24:59 +01:00
e74c35ef40 Refactor 2026-03-11 21:58:51 +01:00
6 changed files with 35 additions and 34 deletions

View File

@@ -1,6 +1,6 @@
# hash-kt
# hash.kt
`hash-kt` is a Kotlin Multiplatform library that provides common [Cryptographic Hash Functions](https://en.wikipedia.org/wiki/Cryptographic_hash_function).
`hash.kt` is a Kotlin Multiplatform library that provides common [Cryptographic Hash Functions](https://en.wikipedia.org/wiki/Cryptographic_hash_function).
Supported algorithms:
- MD5

View File

@@ -1,7 +1,7 @@
[versions]
infendro = "1.1.0"
kotlin = "2.3.0"
bytes = "1.4.0"
bytes = "1.4.1"
[plugins]
infendro = { id = "com.infendro.plugin", version.ref = "infendro" }

View File

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

View File

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

View File

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

View File

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