refactor padding

This commit is contained in:
2026-01-22 12:44:59 +01:00
parent 5547e77c58
commit e138ec0b6d
11 changed files with 53 additions and 53 deletions

View File

@@ -38,10 +38,10 @@ object `Blake-224` : HashFunction {
val length = value.size.toULong() * 8UL
val size = (value.size + 9).align(block)
val result = ByteArray(size)
value.copyInto(result)
result[value.size] = 0x80U.toByte()
length.copyInto(result, result.size - 8)
return result
return ByteArray(size).also {
value.copyInto(it)
it[value.size] = 0x80U.toByte()
length.copyInto(it, size - 8)
}
}
}

View File

@@ -63,12 +63,12 @@ object `Blake-256` : HashFunction {
val length = value.size.toULong() * 8UL
val size = (value.size + 10).align(block)
val result = ByteArray(size)
value.copyInto(result)
result[value.size] = 0x80U.toByte()
result[result.size - 9] = 0x01
length.copyInto(result, result.size - 8)
return result
return ByteArray(size).also {
value.copyInto(it)
it[value.size] = 0x80U.toByte()
it[size - 9] = 0x01
length.copyInto(it, size - 8)
}
}
internal fun compress(h: IntArray, value: ByteArray, offset: Int, m: IntArray, v: IntArray, t: UInt) {

View File

@@ -38,10 +38,10 @@ object `Blake-384` : HashFunction {
val length = value.size.toULong() * 8UL
val size = (value.size + 17).align(block)
val result = ByteArray(size)
value.copyInto(result)
result[value.size] = 0x80U.toByte()
length.copyInto(result, result.size - 8)
return result
return ByteArray(size).also {
value.copyInto(it)
it[value.size] = 0x80U.toByte()
length.copyInto(it, size - 8)
}
}
}

View File

@@ -63,12 +63,12 @@ object `Blake-512` : HashFunction {
val length = value.size.toULong() * 8UL
val size = (value.size + 18).align(block)
val result = ByteArray(size)
value.copyInto(result)
result[value.size] = 0x80U.toByte()
result[result.size - 17] = 0x01
length.copyInto(result, result.size - 8)
return result
return ByteArray(size).also {
value.copyInto(it)
it[value.size] = 0x80U.toByte()
it[size - 17] = 0x01
length.copyInto(it, size - 8)
}
}
internal fun compress(h: LongArray, value: ByteArray, offset: Int, m: LongArray, v: LongArray, t: ULong) {

View File

@@ -54,9 +54,9 @@ class Blake2b(
private fun pad(value: ByteArray): ByteArray {
val size = maxOf(value.size.align(block), block)
val result = ByteArray(size)
value.copyInto(result)
return result
return ByteArray(size).also {
value.copyInto(it)
}
}
private fun compress(

View File

@@ -54,9 +54,9 @@ class Blake2s(
private fun pad(value: ByteArray): ByteArray {
val size = maxOf(value.size.align(block), block)
val result = ByteArray(size)
value.copyInto(result)
return result
return ByteArray(size).also {
value.copyInto(it)
}
}
private fun compress(

View File

@@ -53,11 +53,11 @@ open class Keccak(
private fun pad(value: ByteArray): ByteArray {
val size = (value.size + 2).align(block)
val result = ByteArray(size)
value.copyInto(result)
result[value.size] = domain
result[result.lastIndex] = 0x80U.toByte()
return result
return ByteArray(size).also {
value.copyInto(it)
it[value.size] = domain
it[size - 1] = 0x80U.toByte()
}
}
private fun absorb(state: LongArray, value: ByteArray, offset: Int, w: LongArray) {

View File

@@ -65,11 +65,11 @@ object MD5 : HashFunction {
val length = value.size.toULong() * 8UL
val size = (value.size + 9).align(block)
val result = ByteArray(size)
value.copyInto(result)
result[value.size] = 0x80U.toByte()
length.copyInto(result, result.size - 8, order = LITTLE_ENDIAN)
return result
return ByteArray(size).also {
value.copyInto(it)
it[value.size] = 0x80U.toByte()
length.copyInto(it, size - 8, order = LITTLE_ENDIAN)
}
}
private fun process(h: IntArray, value: ByteArray, offset: Int, w: IntArray, v: IntArray) {

View File

@@ -40,11 +40,11 @@ object SHA1 : HashFunction {
val length = value.size.toULong() * 8UL
val size = (value.size + 9).align(block)
val result = ByteArray(size)
value.copyInto(result)
result[value.size] = 0x80U.toByte()
length.copyInto(result, result.size - 8)
return result
return ByteArray(size).also {
value.copyInto(it)
it[value.size] = 0x80U.toByte()
length.copyInto(it, size - 8)
}
}
private fun process(h: IntArray, value: ByteArray, offset: Int, w: IntArray, v: IntArray) {

View File

@@ -47,11 +47,11 @@ object `SHA-256` : HashFunction {
val length = value.size.toULong() * 8UL
val size = (value.size + 9).align(block)
val result = ByteArray(size)
value.copyInto(result)
result[value.size] = 0x80U.toByte()
length.copyInto(result, result.size - 8)
return result
return ByteArray(size).also {
value.copyInto(it)
it[value.size] = 0x80U.toByte()
length.copyInto(it, size - 8)
}
}
internal fun process(h: IntArray, value: ByteArray, offset: Int, w: IntArray, v: IntArray) {

View File

@@ -59,11 +59,11 @@ object `SHA-512` : HashFunction {
val length = value.size.toULong() * 8UL
val size = (value.size + 17).align(block)
val result = ByteArray(size)
value.copyInto(result)
result[value.size] = 0x80U.toByte()
length.copyInto(result, result.size - 8)
return result
return ByteArray(size).also {
value.copyInto(it)
it[value.size] = 0x80U.toByte()
length.copyInto(it, size - 8)
}
}
internal fun process(h: LongArray, value: ByteArray, offset: Int, w: LongArray, v: LongArray) {