remove unsigned support
All checks were successful
/ test (pull_request) Successful in 1m45s

This commit is contained in:
2026-02-08 14:04:41 +01:00
parent e138ec0b6d
commit 902cb060a1
38 changed files with 455 additions and 696 deletions

View File

@@ -1,7 +1,7 @@
package com.infendro.hash.blake2
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
import com.infendro.bytes.bytearray.copyInto
import com.infendro.bytes.bytearray.toLongArray
import com.infendro.bytes.longarray.toByteArray
import com.infendro.hash.HashFunction
import com.infendro.hash.util.align
@@ -16,12 +16,12 @@ class Blake2b(
override val block: Int
get() = 128
private val initial = ulongArrayOf(
0x6a09e667f3bcc908UL, 0xbb67ae8584caa73bUL,
0x3c6ef372fe94f82bUL, 0xa54ff53a5f1d36f1UL,
0x510e527fade682d1UL, 0x9b05688c2b3e6c1fUL,
0x1f83d9abfb41bd6bUL, 0x5be0cd19137e2179UL,
).asLongArray()
private val initial = longArrayOf(
+0x6a09e667f3bcc908L, -0x4498517a7b3558c5L,
+0x3c6ef372fe94f82bL, -0x5ab00ac5a0e2c90fL,
+0x510e527fade682d1L, -0x64fa9773d4c193e1L,
+0x1f83d9abfb41bd6bL, +0x5be0cd19137e2179L,
)
private val sigma = arrayOf(
intArrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15),
@@ -41,13 +41,12 @@ class Blake2b(
val input = pad(value)
val h = initial.copyOf()
h[0] = h[0] xor (0x01010000L or bytes.toLong())
val m = LongArray(16)
val v = LongArray(16)
var t = 0UL
for (i in input.indices step block) {
t += minOf(length - t, 128UL)
compress(h, input, i, m, v, t, t < 1024UL)
val block = input.sliceArray(i..<(i + block))
compress(h, block, t, t < 1024UL)
}
h.toByteArray(LITTLE_ENDIAN).copyInto(destination, destinationOffset, endIndex = bytes)
}
@@ -59,16 +58,9 @@ class Blake2b(
}
}
private fun compress(
h: LongArray,
value: ByteArray,
offset: Int,
m: LongArray,
v: LongArray,
t: ULong,
last: Boolean,
) {
value.copyInto(m, startIndex = offset, endIndex = offset + block)
private fun compress(h: LongArray, block: ByteArray, t: ULong, last: Boolean) {
val m = block.toLongArray()
val v = LongArray(16)
for (i in 0..<8) v[i] = h[i]
for (i in 0..<8) v[i + 8] = initial[i]
v[12] = v[12] xor t.toLong()

View File

@@ -1,7 +1,7 @@
package com.infendro.hash.blake2
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
import com.infendro.bytes.bytearray.copyInto
import com.infendro.bytes.bytearray.toIntArray
import com.infendro.bytes.intarray.toByteArray
import com.infendro.hash.HashFunction
import com.infendro.hash.util.align
@@ -16,12 +16,12 @@ class Blake2s(
override val block: Int
get() = 64
private val initial = uintArrayOf(
0x6a09e667U, 0xbb67ae85U,
0x3c6ef372U, 0xa54ff53aU,
0x510e527fU, 0x9b05688cU,
0x1f83d9abU, 0x5be0cd19U,
).asIntArray()
private val initial = intArrayOf(
+0x6a09e667, -0x4498517b,
+0x3c6ef372, -0x5ab00ac6,
+0x510e527f, -0x64fa9774,
+0x1f83d9ab, +0x5be0cd19,
)
private val sigma = arrayOf(
intArrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15),
@@ -41,13 +41,12 @@ class Blake2s(
val input = pad(value)
val h = initial.copyOf()
h[0] = h[0] xor (0x01010000 or bytes)
val m = IntArray(16)
val v = IntArray(16)
var t = 0U
for (i in input.indices step block) {
t += minOf(length - t, 64UL).toUInt()
compress(h, input, i, m, v, t, t < 1024UL)
val block = input.sliceArray(i..<(i + block))
compress(h, block, t, t < 1024UL)
}
h.toByteArray(LITTLE_ENDIAN).copyInto(destination, destinationOffset, endIndex = bytes)
}
@@ -59,16 +58,9 @@ class Blake2s(
}
}
private fun compress(
h: IntArray,
value: ByteArray,
offset: Int,
m: IntArray,
v: IntArray,
t: UInt,
last: Boolean,
) {
value.copyInto(m, startIndex = offset, endIndex = offset + block)
private fun compress(h: IntArray, block: ByteArray, t: UInt, last: Boolean) {
val m = block.toIntArray()
val v = IntArray(16)
for (i in 0..<8) v[i] = h[i]
for (i in 0..<8) v[i + 8] = initial[i]
v[12] = v[12] xor t.toInt()