Implement streaming API
This commit is contained in:
@@ -1,66 +1,66 @@
|
||||
package com.infendro.hash.blake2
|
||||
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
import com.infendro.bytes.bytearray.toLongArray
|
||||
import com.infendro.bytes.bytearray.copyInto
|
||||
import com.infendro.bytes.longarray.toByteArray
|
||||
import com.infendro.hash.HashFunction
|
||||
import com.infendro.hash.util.align
|
||||
|
||||
class Blake2b(
|
||||
override val bytes: Int,
|
||||
) : HashFunction {
|
||||
class Blake2b(bytes: Int) : HashFunction(bytes, 128) {
|
||||
init {
|
||||
require(bytes in 1..64)
|
||||
}
|
||||
|
||||
override val block: Int
|
||||
get() = 128
|
||||
private val h = initial.copyOf()
|
||||
.also { h ->
|
||||
h[0] = h[0] xor (0x01010000L or bytes.toLong())
|
||||
}
|
||||
|
||||
private val initial = longArrayOf(
|
||||
+0x6a09e667f3bcc908L, -0x4498517a7b3558c5L,
|
||||
+0x3c6ef372fe94f82bL, -0x5ab00ac5a0e2c90fL,
|
||||
+0x510e527fade682d1L, -0x64fa9773d4c193e1L,
|
||||
+0x1f83d9abfb41bd6bL, +0x5be0cd19137e2179L,
|
||||
)
|
||||
private val m = LongArray(16)
|
||||
private val v = LongArray(16)
|
||||
|
||||
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.toLong()
|
||||
val input = pad(value)
|
||||
val h = initial.copyOf()
|
||||
override fun reset() {
|
||||
super.reset()
|
||||
initial.copyInto(h)
|
||||
h[0] = h[0] xor (0x01010000L or bytes.toLong())
|
||||
var t = 0L
|
||||
}
|
||||
|
||||
for (i in input.indices step block) {
|
||||
t += minOf(length - t, 128)
|
||||
val block = input.sliceArray(i..<(i + block))
|
||||
compress(h, block, t, t < 1024)
|
||||
override fun update(byte: Byte) {
|
||||
if (buffer.isFull()) {
|
||||
process()
|
||||
buffer.reset()
|
||||
}
|
||||
buffer.append(byte)
|
||||
length++
|
||||
}
|
||||
|
||||
override fun update(bytes: ByteArray) {
|
||||
if (buffer.isFull() && bytes.isNotEmpty()) {
|
||||
process()
|
||||
buffer.reset()
|
||||
}
|
||||
var i = 0
|
||||
while (i + buffer.free < bytes.size) {
|
||||
val remaining = buffer.free
|
||||
buffer.append(bytes, startIndex = i, endIndex = i + remaining)
|
||||
process()
|
||||
buffer.reset()
|
||||
i += remaining
|
||||
}
|
||||
buffer.append(bytes, startIndex = i)
|
||||
}
|
||||
|
||||
override fun process() = compress(buffer.value, length, false)
|
||||
|
||||
private fun finalize() = compress(buffer.value, length, true)
|
||||
|
||||
override fun digestInto(destination: ByteArray, destinationOffset: Int) {
|
||||
finalize()
|
||||
h.toByteArray(order = LITTLE_ENDIAN).copyInto(destination, destinationOffset, endIndex = bytes)
|
||||
reset()
|
||||
}
|
||||
|
||||
private fun pad(value: ByteArray): ByteArray {
|
||||
val size = maxOf(value.size.align(block), block)
|
||||
return ByteArray(size).also {
|
||||
value.copyInto(it)
|
||||
}
|
||||
}
|
||||
|
||||
private fun compress(h: LongArray, block: ByteArray, t: Long, last: Boolean) {
|
||||
val m = block.toLongArray()
|
||||
val v = LongArray(16)
|
||||
private fun compress(block: ByteArray, t: Long, last: Boolean) {
|
||||
block.copyInto(m)
|
||||
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
|
||||
@@ -95,4 +95,26 @@ class Blake2b(
|
||||
v[c] += v[d]
|
||||
v[b] = (v[b] xor v[c]).rotateRight(63)
|
||||
}
|
||||
|
||||
companion object {
|
||||
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),
|
||||
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),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,69 +1,70 @@
|
||||
package com.infendro.hash.blake2
|
||||
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
import com.infendro.bytes.bytearray.toIntArray
|
||||
import com.infendro.bytes.bytearray.copyInto
|
||||
import com.infendro.bytes.intarray.toByteArray
|
||||
import com.infendro.hash.HashFunction
|
||||
import com.infendro.hash.util.align
|
||||
|
||||
class Blake2s(
|
||||
override val bytes: Int,
|
||||
) : HashFunction {
|
||||
class Blake2s(bytes: Int) : HashFunction(bytes, 64) {
|
||||
init {
|
||||
require(bytes in 1..32)
|
||||
}
|
||||
|
||||
override val block: Int
|
||||
get() = 64
|
||||
private val h = initial.copyOf()
|
||||
.also { h ->
|
||||
h[0] = h[0] xor (0x01010000 or bytes)
|
||||
}
|
||||
|
||||
private val initial = intArrayOf(
|
||||
+0x6a09e667, -0x4498517b,
|
||||
+0x3c6ef372, -0x5ab00ac6,
|
||||
+0x510e527f, -0x64fa9774,
|
||||
+0x1f83d9ab, +0x5be0cd19,
|
||||
)
|
||||
private val m = IntArray(16)
|
||||
private val v = IntArray(16)
|
||||
|
||||
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.toLong()
|
||||
val input = pad(value)
|
||||
val h = initial.copyOf()
|
||||
override fun reset() {
|
||||
super.reset()
|
||||
initial.copyInto(h)
|
||||
h[0] = h[0] xor (0x01010000 or bytes)
|
||||
var t = 0
|
||||
}
|
||||
|
||||
for (i in input.indices step block) {
|
||||
t += minOf(length - t, 64).toInt()
|
||||
val block = input.sliceArray(i..<(i + block))
|
||||
compress(h, block, t, t < 1024)
|
||||
override fun update(byte: Byte) {
|
||||
if (buffer.isFull()) {
|
||||
process()
|
||||
buffer.reset()
|
||||
}
|
||||
buffer.append(byte)
|
||||
length++
|
||||
}
|
||||
|
||||
override fun update(bytes: ByteArray) {
|
||||
if (buffer.isFull() && bytes.isNotEmpty()) {
|
||||
process()
|
||||
buffer.reset()
|
||||
}
|
||||
var i = 0
|
||||
while (i + buffer.free < bytes.size) {
|
||||
val remaining = buffer.free
|
||||
buffer.append(bytes, startIndex = i, endIndex = i + remaining)
|
||||
process()
|
||||
buffer.reset()
|
||||
i += remaining
|
||||
}
|
||||
buffer.append(bytes, startIndex = i)
|
||||
}
|
||||
|
||||
override fun process() = compress(buffer.value, length, false)
|
||||
|
||||
private fun finalize() = compress(buffer.value, length, true)
|
||||
|
||||
override fun digestInto(destination: ByteArray, destinationOffset: Int) {
|
||||
finalize()
|
||||
h.toByteArray(order = LITTLE_ENDIAN).copyInto(destination, destinationOffset, endIndex = bytes)
|
||||
reset()
|
||||
}
|
||||
|
||||
private fun pad(value: ByteArray): ByteArray {
|
||||
val size = maxOf(value.size.align(block), block)
|
||||
return ByteArray(size).also {
|
||||
value.copyInto(it)
|
||||
}
|
||||
}
|
||||
|
||||
private fun compress(h: IntArray, block: ByteArray, t: Int, last: Boolean) {
|
||||
val m = block.toIntArray()
|
||||
val v = IntArray(16)
|
||||
private fun compress(block: ByteArray, t: Long, last: Boolean) {
|
||||
block.copyInto(m)
|
||||
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
|
||||
v[12] = v[12] xor t.toInt()
|
||||
v[13] = v[13] xor (t ushr 32).toInt()
|
||||
if (last) v[14] = v[14].inv()
|
||||
|
||||
repeat(10) { r ->
|
||||
@@ -95,4 +96,26 @@ class Blake2s(
|
||||
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,
|
||||
)
|
||||
|
||||
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),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user