Implement streaming API

This commit is contained in:
2026-03-11 18:53:00 +01:00
parent cb3ff7ec6c
commit 328d4b957e
34 changed files with 936 additions and 593 deletions

View File

@@ -1,46 +1,85 @@
package com.infendro.hash.blake
import com.infendro.bytes.bytearray.copyInto
import com.infendro.bytes.intarray.copyInto
import com.infendro.bytes.long.copyInto
import com.infendro.hash.HashFunction
import com.infendro.hash.util.align
import com.infendro.hash.blake.`Blake-256`.Companion.c
import com.infendro.hash.blake.`Blake-256`.Companion.sigma
object `Blake-224` : HashFunction {
override val bytes: Int
get() = 28
class `Blake-224` : HashFunction(28, 64) {
private val h = initial.copyOf()
override val block: Int
get() = 64
private val m = IntArray(16)
private val v = IntArray(16)
private val initial = intArrayOf(
-0x3efa6128, +0x367cd507,
+0x3070dd17, -0x08f1a6c7,
-0x003ff4cf, +0x68581511,
+0x64f98fa7, -0x4105b05c,
)
override fun hashInto(value: ByteArray, destination: ByteArray, destinationOffset: Int) {
val length = value.size * 8L
val input = pad(value)
val h = initial.copyOf()
var t = 0
for (i in input.indices step block) {
t += minOf(length - t, 512).toInt()
val block = input.sliceArray(i..<(i + block))
`Blake-256`.compress(h, block, t)
}
h.copyInto(destination, destinationOffset, endIndex = 7)
override fun reset() {
super.reset()
initial.copyInto(h)
}
private fun pad(value: ByteArray): ByteArray {
val length = value.size * 8L
override fun process() = compress(buffer.value, length * 8L)
val size = (value.size + 9).align(block)
return ByteArray(size).also {
value.copyInto(it)
it[value.size] = -0x80
length.copyInto(it, size - 8)
private fun finalize() {
buffer.append(0x80.toByte())
if (buffer.free < 8) {
process()
buffer.reset()
}
buffer.jumpTo(56)
buffer.append(length * 8L)
process()
}
override fun digestInto(destination: ByteArray, destinationOffset: Int) {
finalize()
h.copyInto(destination, destinationOffset, endIndex = 7)
reset()
}
private fun compress(block: ByteArray, t: Long) {
block.copyInto(m)
for (i in 0..<8) v[i] = h[i]
for (i in 0..<4) v[i + 8] = c[i]
for (i in 0..<2) v[i + 12] = c[i + 4] xor t.toInt()
for (i in 0..<2) v[i + 14] = c[i + 6] xor (t ushr 32).toInt()
repeat(14) { r ->
val s = sigma[r % 10]
g(v, 0, 4, 8, 12, m[s[0]] xor c[s[1]], m[s[1]] xor c[s[0]])
g(v, 1, 5, 9, 13, m[s[2]] xor c[s[3]], m[s[3]] xor c[s[2]])
g(v, 2, 6, 10, 14, m[s[4]] xor c[s[5]], m[s[5]] xor c[s[4]])
g(v, 3, 7, 11, 15, m[s[6]] xor c[s[7]], m[s[7]] xor c[s[6]])
g(v, 0, 5, 10, 15, m[s[8]] xor c[s[9]], m[s[9]] xor c[s[8]])
g(v, 1, 6, 11, 12, m[s[10]] xor c[s[11]], m[s[11]] xor c[s[10]])
g(v, 2, 7, 8, 13, m[s[12]] xor c[s[13]], m[s[13]] xor c[s[12]])
g(v, 3, 4, 9, 14, m[s[14]] xor c[s[15]], m[s[15]] xor c[s[14]])
}
for (i in 0..<8) {
h[i] = h[i] xor v[i] xor v[i + 8]
}
}
private fun g(v: IntArray, a: Int, b: Int, c: Int, d: Int, x: Int, y: Int) {
v[a] += v[b] + x
v[d] = (v[d] xor v[a]).rotateRight(16)
v[c] += v[d]
v[b] = (v[b] xor v[c]).rotateRight(12)
v[a] += v[b] + y
v[d] = (v[d] xor v[a]).rotateRight(8)
v[c] += v[d]
v[b] = (v[b] xor v[c]).rotateRight(7)
}
companion object {
private val initial = intArrayOf(
-0x3efa6128, +0x367cd507,
+0x3070dd17, -0x08f1a6c7,
-0x003ff4cf, +0x68581511,
+0x64f98fa7, -0x4105b05c,
)
}
}

View File

@@ -1,82 +1,47 @@
package com.infendro.hash.blake
import com.infendro.bytes.bytearray.toIntArray
import com.infendro.bytes.bytearray.copyInto
import com.infendro.bytes.intarray.copyInto
import com.infendro.bytes.long.copyInto
import com.infendro.hash.HashFunction
import com.infendro.hash.util.align
object `Blake-256` : HashFunction {
override val bytes: Int
get() = 32
class `Blake-256` : HashFunction(32, 64) {
private val h = initial.copyOf()
override val block: Int
get() = 64
private val m = IntArray(16)
private val v = IntArray(16)
private val initial = intArrayOf(
+0x6a09e667, -0x4498517b,
+0x3c6ef372, -0x5ab00ac6,
+0x510e527f, -0x64fa9774,
+0x1f83d9ab, +0x5be0cd19,
)
override fun reset() {
super.reset()
initial.copyInto(h)
}
private val c = intArrayOf(
+0x243f6a88, -0x7a5cf72d,
+0x13198a2e, +0x03707344,
-0x5bf6c7de, +0x299f31d0,
+0x082efa98, -0x13b19377,
+0x452821e6, +0x38d01377,
-0x41ab9931, +0x34e90c6c,
-0x3f53d649, -0x3683af23,
+0x3f84d5b5, -0x4ab8f6e9,
)
override fun process() = compress(buffer.value, length * 8L)
private val sigma = arrayOf(
intArrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15),
intArrayOf(14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3),
intArrayOf(11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4),
intArrayOf(7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8),
intArrayOf(9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13),
intArrayOf(2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9),
intArrayOf(12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11),
intArrayOf(13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10),
intArrayOf(6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5),
intArrayOf(10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0),
)
override fun hashInto(value: ByteArray, destination: ByteArray, destinationOffset: Int) {
val length = value.size * 8L
val input = pad(value)
val h = initial.copyOf()
var t = 0
for (i in input.indices step block) {
t += minOf(length - t, 512).toInt()
val block = input.sliceArray(i..<(i + block))
compress(h, block, t)
private fun finalize() {
buffer.append(0x80.toByte())
if (buffer.free < 9) {
process()
buffer.reset()
}
buffer.jumpTo(55)
buffer.append(0x01.toByte())
buffer.append(length * 8L)
process()
}
override fun digestInto(destination: ByteArray, destinationOffset: Int) {
finalize()
h.copyInto(destination, destinationOffset)
reset()
}
private fun pad(value: ByteArray): ByteArray {
val length = value.size * 8L
val size = (value.size + 10).align(block)
return ByteArray(size).also {
value.copyInto(it)
it[value.size] = -0x80
it[size - 9] = 0x01
length.copyInto(it, size - 8)
}
}
internal fun compress(h: IntArray, block: ByteArray, t: Int) {
val m = block.toIntArray()
val v = IntArray(16)
private fun compress(block: ByteArray, t: Long) {
block.copyInto(m)
for (i in 0..<8) v[i] = h[i]
for (i in 0..<4) v[i + 8] = c[i]
for (i in 0..<2) v[i + 12] = c[i + 4] xor t
for (i in 0..<2) v[i + 14] = c[i + 6]
for (i in 0..<2) v[i + 12] = c[i + 4] xor t.toInt()
for (i in 0..<2) v[i + 14] = c[i + 6] xor (t ushr 32).toInt()
repeat(14) { r ->
val s = sigma[r % 10]
@@ -107,4 +72,37 @@ object `Blake-256` : HashFunction {
v[c] += v[d]
v[b] = (v[b] xor v[c]).rotateRight(7)
}
companion object {
private val initial = intArrayOf(
+0x6a09e667, -0x4498517b,
+0x3c6ef372, -0x5ab00ac6,
+0x510e527f, -0x64fa9774,
+0x1f83d9ab, +0x5be0cd19,
)
internal val c = intArrayOf(
+0x243f6a88, -0x7a5cf72d,
+0x13198a2e, +0x03707344,
-0x5bf6c7de, +0x299f31d0,
+0x082efa98, -0x13b19377,
+0x452821e6, +0x38d01377,
-0x41ab9931, +0x34e90c6c,
-0x3f53d649, -0x3683af23,
+0x3f84d5b5, -0x4ab8f6e9,
)
internal val sigma = arrayOf(
intArrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15),
intArrayOf(14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3),
intArrayOf(11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4),
intArrayOf(7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8),
intArrayOf(9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13),
intArrayOf(2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9),
intArrayOf(12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11),
intArrayOf(13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10),
intArrayOf(6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5),
intArrayOf(10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0),
)
}
}

View File

@@ -1,46 +1,86 @@
package com.infendro.hash.blake
import com.infendro.bytes.long.copyInto
import com.infendro.bytes.bytearray.copyInto
import com.infendro.bytes.longarray.copyInto
import com.infendro.hash.HashFunction
import com.infendro.hash.util.align
import com.infendro.hash.blake.`Blake-512`.Companion.c
import com.infendro.hash.blake.`Blake-512`.Companion.sigma
object `Blake-384` : HashFunction {
override val bytes: Int
get() = 48
class `Blake-384` : HashFunction(48, 128) {
private val h = initial.copyOf()
override val block: Int
get() = 128
private val m = LongArray(16)
private val v = LongArray(16)
private val initial = longArrayOf(
-0x344462a23efa6128, +0x629a292a367cd507,
-0x6ea6fea5cf8f22e9, +0x152fecd8f70e5939,
+0x67332667ffc00b31, -0x714bb57897a7eaef,
-0x24f3d1f29b067059, +0x47b5481dbefa4fa4,
)
override fun hashInto(value: ByteArray, destination: ByteArray, destinationOffset: Int) {
val length = value.size * 8L
val input = pad(value)
val h = initial.copyOf()
var t = 0L
for (i in input.indices step block) {
t += minOf(length - t, 1024)
val block = input.sliceArray(i..<(i + block))
`Blake-512`.compress(h, block, t)
}
h.copyInto(destination, destinationOffset, endIndex = 6)
override fun reset() {
super.reset()
initial.copyInto(h)
}
private fun pad(value: ByteArray): ByteArray {
val length = value.size * 8L
override fun process() = compress(buffer.value, length * 8L)
val size = (value.size + 17).align(block)
return ByteArray(size).also {
value.copyInto(it)
it[value.size] = -0x80
length.copyInto(it, size - 8)
private fun finalize() {
buffer.append(0x80.toByte())
if (buffer.free < 16) {
process()
buffer.reset()
}
buffer.jumpTo(112)
buffer.append(0L)
buffer.append(length * 8L)
process()
}
override fun digestInto(destination: ByteArray, destinationOffset: Int) {
finalize()
h.copyInto(destination, destinationOffset, endIndex = 6)
reset()
}
private fun compress(block: ByteArray, t: Long) {
block.copyInto(m)
for (i in 0..<8) v[i] = h[i]
for (i in 0..<4) v[i + 8] = c[i]
for (i in 0..<2) v[i + 12] = c[i + 4] xor t
for (i in 0..<2) v[i + 14] = c[i + 6]
repeat(16) { r ->
val s = sigma[r % 10]
g(v, 0, 4, 8, 12, m[s[0]] xor c[s[1]], m[s[1]] xor c[s[0]])
g(v, 1, 5, 9, 13, m[s[2]] xor c[s[3]], m[s[3]] xor c[s[2]])
g(v, 2, 6, 10, 14, m[s[4]] xor c[s[5]], m[s[5]] xor c[s[4]])
g(v, 3, 7, 11, 15, m[s[6]] xor c[s[7]], m[s[7]] xor c[s[6]])
g(v, 0, 5, 10, 15, m[s[8]] xor c[s[9]], m[s[9]] xor c[s[8]])
g(v, 1, 6, 11, 12, m[s[10]] xor c[s[11]], m[s[11]] xor c[s[10]])
g(v, 2, 7, 8, 13, m[s[12]] xor c[s[13]], m[s[13]] xor c[s[12]])
g(v, 3, 4, 9, 14, m[s[14]] xor c[s[15]], m[s[15]] xor c[s[14]])
}
for (i in 0..<8) {
h[i] = h[i] xor v[i] xor v[i + 8]
}
}
private fun g(v: LongArray, a: Int, b: Int, c: Int, d: Int, x: Long, y: Long) {
v[a] += v[b] + x
v[d] = (v[d] xor v[a]).rotateRight(32)
v[c] += v[d]
v[b] = (v[b] xor v[c]).rotateRight(25)
v[a] += v[b] + y
v[d] = (v[d] xor v[a]).rotateRight(16)
v[c] += v[d]
v[b] = (v[b] xor v[c]).rotateRight(11)
}
companion object {
private val initial = longArrayOf(
-0x344462a23efa6128, +0x629a292a367cd507,
-0x6ea6fea5cf8f22e9, +0x152fecd8f70e5939,
+0x67332667ffc00b31, -0x714bb57897a7eaef,
-0x24f3d1f29b067059, +0x47b5481dbefa4fa4,
)
}
}

View File

@@ -1,78 +1,44 @@
package com.infendro.hash.blake
import com.infendro.bytes.bytearray.toLongArray
import com.infendro.bytes.long.copyInto
import com.infendro.bytes.bytearray.copyInto
import com.infendro.bytes.longarray.copyInto
import com.infendro.hash.HashFunction
import com.infendro.hash.util.align
object `Blake-512` : HashFunction {
override val bytes: Int
get() = 64
class `Blake-512` : HashFunction(64, 128) {
private val h = initial.copyOf()
override val block: Int
get() = 128
private val m = LongArray(16)
private val v = LongArray(16)
private val initial = longArrayOf(
+0x6a09e667f3bcc908L, -0x4498517a7b3558c5L,
+0x3c6ef372fe94f82bL, -0x5ab00ac5a0e2c90fL,
+0x510e527fade682d1L, -0x64fa9773d4c193e1L,
+0x1f83d9abfb41bd6bL, +0x5be0cd19137e2179L,
)
override fun reset() {
super.reset()
initial.copyInto(h)
}
private val c = longArrayOf(
+0x243f6a8885a308d3L, +0x13198a2e03707344L,
-0x5bf6c7ddd660ce30L, +0x082efa98ec4e6c89L,
+0x452821e638d01377L, -0x41ab9930cb16f394L,
-0x3f53d6483683af23L, +0x3f84d5b5b5470917L,
-0x6de92a26768604e5L, -0x2ecef45967204a54L,
+0x2ffd72dbd01adfb7L, -0x471e501295d9816aL,
-0x45836fba0ed38067L, +0x24a19947b3916cf7L,
+0x0801f2e2858efc16L, +0x636920d871574e69L,
)
override fun process() = compress(buffer.value, length * 8L)
private val sigma = arrayOf(
intArrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15),
intArrayOf(14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3),
intArrayOf(11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4),
intArrayOf(7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8),
intArrayOf(9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13),
intArrayOf(2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9),
intArrayOf(12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11),
intArrayOf(13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10),
intArrayOf(6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5),
intArrayOf(10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0),
)
override fun hashInto(value: ByteArray, destination: ByteArray, destinationOffset: Int) {
val length = value.size * 8L
val input = pad(value)
val h = initial.copyOf()
var t = 0L
for (i in input.indices step block) {
t += minOf(length - t, 1024)
val block = input.sliceArray(i..<(i + block))
compress(h, block, t)
private fun finalize() {
buffer.append(0x80.toByte())
if (buffer.free < 17) {
process()
buffer.reset()
}
buffer.jumpTo(111)
buffer.append(0x01.toByte())
buffer.append(0L)
buffer.append(length * 8L)
process()
}
override fun digestInto(destination: ByteArray, destinationOffset: Int) {
finalize()
h.copyInto(destination, destinationOffset)
reset()
}
private fun pad(value: ByteArray): ByteArray {
val length = value.size * 8L
val size = (value.size + 18).align(block)
return ByteArray(size).also {
value.copyInto(it)
it[value.size] = -0x80
it[size - 17] = 0x01
length.copyInto(it, size - 8)
}
}
internal fun compress(h: LongArray, block: ByteArray, t: Long) {
val m = block.toLongArray()
val v = LongArray(16)
private fun compress(block: ByteArray, t: Long) {
block.copyInto(m)
for (i in 0..<8) v[i] = h[i]
for (i in 0..<4) v[i + 8] = c[i]
for (i in 0..<2) v[i + 12] = c[i + 4] xor t
@@ -107,4 +73,37 @@ object `Blake-512` : HashFunction {
v[c] += v[d]
v[b] = (v[b] xor v[c]).rotateRight(11)
}
companion object {
private val initial = longArrayOf(
+0x6a09e667f3bcc908L, -0x4498517a7b3558c5L,
+0x3c6ef372fe94f82bL, -0x5ab00ac5a0e2c90fL,
+0x510e527fade682d1L, -0x64fa9773d4c193e1L,
+0x1f83d9abfb41bd6bL, +0x5be0cd19137e2179L,
)
internal val c = longArrayOf(
+0x243f6a8885a308d3L, +0x13198a2e03707344L,
-0x5bf6c7ddd660ce30L, +0x082efa98ec4e6c89L,
+0x452821e638d01377L, -0x41ab9930cb16f394L,
-0x3f53d6483683af23L, +0x3f84d5b5b5470917L,
-0x6de92a26768604e5L, -0x2ecef45967204a54L,
+0x2ffd72dbd01adfb7L, -0x471e501295d9816aL,
-0x45836fba0ed38067L, +0x24a19947b3916cf7L,
+0x0801f2e2858efc16L, +0x636920d871574e69L,
)
internal val sigma = arrayOf(
intArrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15),
intArrayOf(14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3),
intArrayOf(11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4),
intArrayOf(7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8),
intArrayOf(9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13),
intArrayOf(2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9),
intArrayOf(12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11),
intArrayOf(13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10),
intArrayOf(6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5),
intArrayOf(10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0),
)
}
}