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.md
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
import com.infendro.bytes.bytearray.copyInto
import com.infendro.bytes.bytearray.toIntArray
import com.infendro.bytes.intarray.copyInto
import com.infendro.bytes.ulong.copyInto
import com.infendro.hash.HashFunction
@@ -14,29 +14,29 @@ object MD5 : HashFunction {
override val block: Int
get() = 64
private val initial = uintArrayOf(
0x67452301U, 0xefcdab89U,
0x98badcfeU, 0x10325476U,
).asIntArray()
private val initial = intArrayOf(
+0x67452301, -0x10325477,
-0x67452302, +0x10325476,
)
private val c = uintArrayOf(
0xd76aa478U, 0xe8c7b756U, 0x242070dbU, 0xc1bdceeeU,
0xf57c0fafU, 0x4787c62aU, 0xa8304613U, 0xfd469501U,
0x698098d8U, 0x8b44f7afU, 0xffff5bb1U, 0x895cd7beU,
0x6b901122U, 0xfd987193U, 0xa679438eU, 0x49b40821U,
0xf61e2562U, 0xc040b340U, 0x265e5a51U, 0xe9b6c7aaU,
0xd62f105dU, 0x02441453U, 0xd8a1e681U, 0xe7d3fbc8U,
0x21e1cde6U, 0xc33707d6U, 0xf4d50d87U, 0x455a14edU,
0xa9e3e905U, 0xfcefa3f8U, 0x676f02d9U, 0x8d2a4c8aU,
0xfffa3942U, 0x8771f681U, 0x6d9d6122U, 0xfde5380cU,
0xa4beea44U, 0x4bdecfa9U, 0xf6bb4b60U, 0xbebfbc70U,
0x289b7ec6U, 0xeaa127faU, 0xd4ef3085U, 0x04881d05U,
0xd9d4d039U, 0xe6db99e5U, 0x1fa27cf8U, 0xc4ac5665U,
0xf4292244U, 0x432aff97U, 0xab9423a7U, 0xfc93a039U,
0x655b59c3U, 0x8f0ccc92U, 0xffeff47dU, 0x85845dd1U,
0x6fa87e4fU, 0xfe2ce6e0U, 0xa3014314U, 0x4e0811a1U,
0xf7537e82U, 0xbd3af235U, 0x2ad7d2bbU, 0xeb86d391U,
).asIntArray()
private val c = intArrayOf(
-0x28955b88, -0x173848aa, +0x242070db, -0x3e423112,
-0x0a83f051, +0x4787c62a, -0x57cfb9ed, -0x02b96aff,
+0x698098d8, -0x74bb0851, -0x0000a44f, -0x76a32842,
+0x6b901122, -0x02678e6d, -0x5986bc72, +0x49b40821,
-0x09e1da9e, -0x3fbf4cc0, +0x265e5a51, -0x16493856,
-0x29d0efa3, +0x02441453, -0x275e197f, -0x182c0438,
+0x21e1cde6, -0x3cc8f82a, -0x0b2af279, +0x455a14ed,
-0x561c16fb, -0x03105c08, +0x676f02d9, -0x72d5b376,
-0x0005c6be, -0x788e097f, +0x6d9d6122, -0x021ac7f4,
-0x5b4115bc, +0x4bdecfa9, -0x0944b4a0, -0x41404390,
+0x289b7ec6, -0x155ed806, -0x2b10cf7b, +0x04881d05,
-0x262b2fc7, -0x1924661b, +0x1fa27cf8, -0x3b53a99b,
-0x0bd6ddbc, +0x432aff97, -0x546bdc59, -0x036c5fc7,
+0x655b59c3, -0x70f3336e, -0x00100b83, -0x7a7ba22f,
+0x6fa87e4f, -0x01d31920, -0x5cfebcec, +0x4e0811a1,
-0x08ac817e, -0x42c50dcb, +0x2ad7d2bb, -0x14792c6f,
)
private val shifts = intArrayOf(
7, 12, 17, 22, 7, 12, 17, 22,
@@ -52,11 +52,10 @@ object MD5 : HashFunction {
override fun hashInto(value: ByteArray, destination: ByteArray, destinationOffset: Int) {
val input = pad(value)
val h = initial.copyOf()
val w = IntArray(16)
val v = IntArray(4)
for (i in input.indices step block) {
process(h, input, i, w, v)
val block = input.sliceArray(i..<(i + block))
process(h, block)
}
h.copyInto(destination, destinationOffset, order = LITTLE_ENDIAN)
}
@@ -72,9 +71,9 @@ object MD5 : HashFunction {
}
}
private fun process(h: IntArray, value: ByteArray, offset: Int, w: IntArray, v: IntArray) {
value.copyInto(w, startIndex = offset, endIndex = offset + block, order = LITTLE_ENDIAN)
h.copyInto(v)
private fun process(h: IntArray, block: ByteArray) {
val w = block.toIntArray(LITTLE_ENDIAN)
val v = h.copyOf()
repeat(64) { i ->
var f = when (i) {