initial commit

This commit is contained in:
2025-07-12 19:25:45 +02:00
commit 519cf204e5
17 changed files with 1290 additions and 0 deletions

View File

@@ -0,0 +1,7 @@
package com.infendro.hash
sealed class HashFunction {
abstract fun hash(
value: ByteArray,
): ByteArray
}

View File

@@ -0,0 +1,109 @@
package com.infendro.hash
import com.infendro.hash.util.ByteOrder.LITTLE_ENDIAN
import com.infendro.hash.util.addAll
import com.infendro.hash.util.toByteArray
import com.infendro.hash.util.toUIntArray
object MD5 : HashFunction() {
private val s = arrayOf(
7, 12, 17, 22,
7, 12, 17, 22,
7, 12, 17, 22,
7, 12, 17, 22,
5, 9, 14, 20,
5, 9, 14, 20,
5, 9, 14, 20,
5, 9, 14, 20,
4, 11, 16, 23,
4, 11, 16, 23,
4, 11, 16, 23,
4, 11, 16, 23,
6, 10, 15, 21,
6, 10, 15, 21,
6, 10, 15, 21,
6, 10, 15, 21,
)
private val k = arrayOf(
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,
)
override fun hash(
value: ByteArray,
): ByteArray {
var a0 = 0x67452301U
var b0 = 0xefcdab89U
var c0 = 0x98badcfeU
var d0 = 0x10325476U
val ml = value.size.toULong() * 8UL
val paddedValue = buildList {
addAll(value)
add(0x80.toByte())
while (size % 64 != 56) {
add(0x00.toByte())
}
addAll(ml.toByteArray(LITTLE_ENDIAN))
}
val chunks = paddedValue
.chunked(64)
for (chunk in chunks) {
val w = buildList {
addAll(chunk.toUIntArray(LITTLE_ENDIAN))
}
var a = a0
var b = b0
var c = c0
var d = d0
for (i in 0..<64) {
val f = when (i) {
in 0..15 -> (b and c) or (b.inv() and d)
in 16..31 -> (d and b) or (d.inv() and c)
in 32..47 -> b xor c xor d
else -> c xor (b or d.inv())
}
val g = when (i) {
in 0..15 -> i
in 16..31 -> (5 * i + 1) % 16
in 32..47 -> (3 * i + 5) % 16
else -> (7 * i) % 16
}
a = d
d = c
c = b
b += (f + a + k[i] + w[g]).rotateLeft(s[i])
}
a0 += a
b0 += b
c0 += c
d0 += d
}
return byteArrayOf(
*a0.toByteArray(LITTLE_ENDIAN),
*b0.toByteArray(LITTLE_ENDIAN),
*c0.toByteArray(LITTLE_ENDIAN),
*d0.toByteArray(LITTLE_ENDIAN),
)
}
}

View File

@@ -0,0 +1,80 @@
package com.infendro.hash
import com.infendro.hash.util.addAll
import com.infendro.hash.util.toByteArray
import com.infendro.hash.util.toUIntArray
object SHA1 : HashFunction() {
override fun hash(
value: ByteArray,
): ByteArray {
var h0 = 0x67452301U
var h1 = 0xefcdaB89U
var h2 = 0x98badcfeU
var h3 = 0x10325476U
var h4 = 0xc3d2e1f0U
val ml = value.size.toULong() * 8UL
val paddedValue = buildList {
addAll(value)
add(0x80.toByte())
while (size % 64 != 56) {
add(0x00.toByte())
}
addAll(ml.toByteArray())
}
val chunks = paddedValue
.chunked(64)
for (chunk in chunks) {
val w = buildList {
addAll(chunk.toUIntArray())
for (i in 16..<80) {
val word = (get(i - 3) xor get(i - 8) xor get(i - 14) xor get(i - 16)).rotateLeft(1)
add(word)
}
}
var a = h0
var b = h1
var c = h2
var d = h3
var e = h4
for (i in 0..<80) {
val f = when (i) {
in 0..<20 -> (b and c) or (b.inv() and d)
in 20..<40 -> b xor c xor d
in 40..<60 -> (b and c) or (b and d) or (c and d)
else -> b xor c xor d
}
val k = when (i) {
in 0..<20 -> 0x5a827999U
in 20..<40 -> 0x6ed9eba1U
in 40..<60 -> 0x8f1bbcdcU
else -> 0xca62c1d6U
}
val temp = a.rotateLeft(5) + f + e + k + w[i]
e = d
d = c
c = b.rotateLeft(30)
b = a
a = temp
}
h0 += a
h1 += b
h2 += c
h3 += d
h4 += e
}
return byteArrayOf(
*h0.toByteArray(),
*h1.toByteArray(),
*h2.toByteArray(),
*h3.toByteArray(),
*h4.toByteArray(),
)
}
}

View File

@@ -0,0 +1,100 @@
package com.infendro.hash
import com.infendro.hash.util.addAll
import com.infendro.hash.util.toByteArray
import com.infendro.hash.util.toUIntArray
object SHA224 : HashFunction() {
private val k = arrayOf(
0x428a2f98U, 0x71374491U, 0xb5c0fbcfU, 0xe9b5dba5U, 0x3956c25bU, 0x59f111f1U, 0x923f82a4U, 0xab1c5ed5U,
0xd807aa98U, 0x12835b01U, 0x243185beU, 0x550c7dc3U, 0x72be5d74U, 0x80deb1feU, 0x9bdc06a7U, 0xc19bf174U,
0xe49b69c1U, 0xefbe4786U, 0x0fc19dc6U, 0x240ca1ccU, 0x2de92c6fU, 0x4a7484aaU, 0x5cb0a9dcU, 0x76f988daU,
0x983e5152U, 0xa831c66dU, 0xb00327c8U, 0xbf597fc7U, 0xc6e00bf3U, 0xd5a79147U, 0x06ca6351U, 0x14292967U,
0x27b70a85U, 0x2e1b2138U, 0x4d2c6dfcU, 0x53380d13U, 0x650a7354U, 0x766a0abbU, 0x81c2c92eU, 0x92722c85U,
0xa2bfe8a1U, 0xa81a664bU, 0xc24b8b70U, 0xc76c51a3U, 0xd192e819U, 0xd6990624U, 0xf40e3585U, 0x106aa070U,
0x19a4c116U, 0x1e376c08U, 0x2748774cU, 0x34b0bcb5U, 0x391c0cb3U, 0x4ed8aa4aU, 0x5b9cca4fU, 0x682e6ff3U,
0x748f82eeU, 0x78a5636fU, 0x84c87814U, 0x8cc70208U, 0x90befffaU, 0xa4506cebU, 0xbef9a3f7U, 0xc67178f2U,
)
override fun hash(
value: ByteArray,
): ByteArray {
var h0 = 0xc1059ed8U
var h1 = 0x367cd507U
var h2 = 0x3070dd17U
var h3 = 0xf70e5939U
var h4 = 0xffc00b31U
var h5 = 0x68581511U
var h6 = 0x64f98fa7U
var h7 = 0xbefa4fa4U
val ml = value.size.toULong() * 8UL
val paddedValue = buildList {
addAll(value)
add(0x80.toByte())
while (size % 64 != 56) {
add(0x00.toByte())
}
addAll(ml.toByteArray())
}
val chunks = paddedValue
.chunked(64)
for (chunk in chunks) {
val w = buildList {
addAll(chunk.toUIntArray())
for (i in 16..<64) {
val s0 = get(i - 15).rotateRight(7) xor get(i - 15).rotateRight(18) xor (get(i - 15) shr 3)
val s1 = get(i - 2).rotateRight(17) xor get(i - 2).rotateRight(19) xor (get(i - 2) shr 10)
add(get(i - 16) + s0 + get(i - 7) + s1)
}
}
var a = h0
var b = h1
var c = h2
var d = h3
var e = h4
var f = h5
var g = h6
var h = h7
for (i in 0..<64) {
val s1 = e.rotateRight(6) xor e.rotateRight(11) xor e.rotateRight(25)
val ch = (e and f) xor (e.inv() and g)
val temp1 = h + s1 + ch + k[i] + w[i]
val s0 = a.rotateRight(2) xor a.rotateRight(13) xor a.rotateRight(22)
val maj = (a and b) xor (a and c) xor (b and c)
val temp2 = s0 + maj
h = g
g = f
f = e
e = d + temp1
d = c
c = b
b = a
a = temp1 + temp2
}
h0 += a
h1 += b
h2 += c
h3 += d
h4 += e
h5 += f
h6 += g
h7 += h
}
return byteArrayOf(
*h0.toByteArray(),
*h1.toByteArray(),
*h2.toByteArray(),
*h3.toByteArray(),
*h4.toByteArray(),
*h5.toByteArray(),
*h6.toByteArray(),
)
}
}

View File

@@ -0,0 +1,101 @@
package com.infendro.hash
import com.infendro.hash.util.addAll
import com.infendro.hash.util.toByteArray
import com.infendro.hash.util.toUIntArray
object SHA256 : HashFunction() {
private val k = arrayOf(
0x428a2f98U, 0x71374491U, 0xb5c0fbcfU, 0xe9b5dba5U, 0x3956c25bU, 0x59f111f1U, 0x923f82a4U, 0xab1c5ed5U,
0xd807aa98U, 0x12835b01U, 0x243185beU, 0x550c7dc3U, 0x72be5d74U, 0x80deb1feU, 0x9bdc06a7U, 0xc19bf174U,
0xe49b69c1U, 0xefbe4786U, 0x0fc19dc6U, 0x240ca1ccU, 0x2de92c6fU, 0x4a7484aaU, 0x5cb0a9dcU, 0x76f988daU,
0x983e5152U, 0xa831c66dU, 0xb00327c8U, 0xbf597fc7U, 0xc6e00bf3U, 0xd5a79147U, 0x06ca6351U, 0x14292967U,
0x27b70a85U, 0x2e1b2138U, 0x4d2c6dfcU, 0x53380d13U, 0x650a7354U, 0x766a0abbU, 0x81c2c92eU, 0x92722c85U,
0xa2bfe8a1U, 0xa81a664bU, 0xc24b8b70U, 0xc76c51a3U, 0xd192e819U, 0xd6990624U, 0xf40e3585U, 0x106aa070U,
0x19a4c116U, 0x1e376c08U, 0x2748774cU, 0x34b0bcb5U, 0x391c0cb3U, 0x4ed8aa4aU, 0x5b9cca4fU, 0x682e6ff3U,
0x748f82eeU, 0x78a5636fU, 0x84c87814U, 0x8cc70208U, 0x90befffaU, 0xa4506cebU, 0xbef9a3f7U, 0xc67178f2U,
)
override fun hash(
value: ByteArray,
): ByteArray {
var h0 = 0x6a09e667U
var h1 = 0xbb67ae85U
var h2 = 0x3c6ef372U
var h3 = 0xa54ff53aU
var h4 = 0x510e527fU
var h5 = 0x9b05688cU
var h6 = 0x1f83d9abU
var h7 = 0x5be0cd19U
val ml = value.size.toULong() * 8UL
val paddedValue = buildList {
addAll(value)
add(0x80.toByte())
while (size % 64 != 56) {
add(0x00.toByte())
}
addAll(ml.toByteArray())
}
val chunks = paddedValue
.chunked(64)
for (chunk in chunks) {
val w = buildList {
addAll(chunk.toUIntArray())
for (i in 16..<64) {
val s0 = get(i - 15).rotateRight(7) xor get(i - 15).rotateRight(18) xor (get(i - 15) shr 3)
val s1 = get(i - 2).rotateRight(17) xor get(i - 2).rotateRight(19) xor (get(i - 2) shr 10)
add(get(i - 16) + s0 + get(i - 7) + s1)
}
}
var a = h0
var b = h1
var c = h2
var d = h3
var e = h4
var f = h5
var g = h6
var h = h7
for (i in 0..<64) {
val s0 = a.rotateRight(2) xor a.rotateRight(13) xor a.rotateRight(22)
val s1 = e.rotateRight(6) xor e.rotateRight(11) xor e.rotateRight(25)
val ch = (e and f) xor (e.inv() and g)
val temp1 = h + s1 + ch + k[i] + w[i]
val maj = (a and b) xor (a and c) xor (b and c)
val temp2 = s0 + maj
h = g
g = f
f = e
e = d + temp1
d = c
c = b
b = a
a = temp1 + temp2
}
h0 += a
h1 += b
h2 += c
h3 += d
h4 += e
h5 += f
h6 += g
h7 += h
}
return byteArrayOf(
*h0.toByteArray(),
*h1.toByteArray(),
*h2.toByteArray(),
*h3.toByteArray(),
*h4.toByteArray(),
*h5.toByteArray(),
*h6.toByteArray(),
*h7.toByteArray(),
)
}
}

View File

@@ -0,0 +1,107 @@
package com.infendro.hash
import com.infendro.hash.util.addAll
import com.infendro.hash.util.toByteArray
import com.infendro.hash.util.toULongArray
object SHA384 : HashFunction() {
private val k = arrayOf(
0x428a2f98d728ae22UL, 0x7137449123ef65cdUL, 0xb5c0fbcfec4d3b2fUL, 0xe9b5dba58189dbbcUL, 0x3956c25bf348b538UL,
0x59f111f1b605d019UL, 0x923f82a4af194f9bUL, 0xab1c5ed5da6d8118UL, 0xd807aa98a3030242UL, 0x12835b0145706fbeUL,
0x243185be4ee4b28cUL, 0x550c7dc3d5ffb4e2UL, 0x72be5d74f27b896fUL, 0x80deb1fe3b1696b1UL, 0x9bdc06a725c71235UL,
0xc19bf174cf692694UL, 0xe49b69c19ef14ad2UL, 0xefbe4786384f25e3UL, 0x0fc19dc68b8cd5b5UL, 0x240ca1cc77ac9c65UL,
0x2de92c6f592b0275UL, 0x4a7484aa6ea6e483UL, 0x5cb0a9dcbd41fbd4UL, 0x76f988da831153b5UL, 0x983e5152ee66dfabUL,
0xa831c66d2db43210UL, 0xb00327c898fb213fUL, 0xbf597fc7beef0ee4UL, 0xc6e00bf33da88fc2UL, 0xd5a79147930aa725UL,
0x06ca6351e003826fUL, 0x142929670a0e6e70UL, 0x27b70a8546d22ffcUL, 0x2e1b21385c26c926UL, 0x4d2c6dfc5ac42aedUL,
0x53380d139d95b3dfUL, 0x650a73548baf63deUL, 0x766a0abb3c77b2a8UL, 0x81c2c92e47edaee6UL, 0x92722c851482353bUL,
0xa2bfe8a14cf10364UL, 0xa81a664bbc423001UL, 0xc24b8b70d0f89791UL, 0xc76c51a30654be30UL, 0xd192e819d6ef5218UL,
0xd69906245565a910UL, 0xf40e35855771202aUL, 0x106aa07032bbd1b8UL, 0x19a4c116b8d2d0c8UL, 0x1e376c085141ab53UL,
0x2748774cdf8eeb99UL, 0x34b0bcb5e19b48a8UL, 0x391c0cb3c5c95a63UL, 0x4ed8aa4ae3418acbUL, 0x5b9cca4f7763e373UL,
0x682e6ff3d6b2b8a3UL, 0x748f82ee5defb2fcUL, 0x78a5636f43172f60UL, 0x84c87814a1f0ab72UL, 0x8cc702081a6439ecUL,
0x90befffa23631e28UL, 0xa4506cebde82bde9UL, 0xbef9a3f7b2c67915UL, 0xc67178f2e372532bUL, 0xca273eceea26619cUL,
0xd186b8c721c0c207UL, 0xeada7dd6cde0eb1eUL, 0xf57d4f7fee6ed178UL, 0x06f067aa72176fbaUL, 0x0a637dc5a2c898a6UL,
0x113f9804bef90daeUL, 0x1b710b35131c471bUL, 0x28db77f523047d84UL, 0x32caab7b40c72493UL, 0x3c9ebe0a15c9bebcUL,
0x431d67c49c100d4cUL, 0x4cc5d4becb3e42b6UL, 0x597f299cfc657e2aUL, 0x5fcb6fab3ad6faecUL, 0x6c44198c4a475817UL,
)
override fun hash(
value: ByteArray,
): ByteArray {
var h0 = 0xcbbb9d5dc1059ed8UL
var h1 = 0x629a292a367cd507UL
var h2 = 0x9159015a3070dd17UL
var h3 = 0x152fecd8f70e5939UL
var h4 = 0x67332667ffc00b31UL
var h5 = 0x8eb44a8768581511UL
var h6 = 0xdb0c2e0d64f98fa7UL
var h7 = 0x47b5481dbefa4fa4UL
val ml = value.size.toULong() * 8UL
val paddedValue = buildList {
addAll(value)
add(0x80.toByte())
while (size % 128 != 120) {
add(0x00.toByte())
}
addAll(ml.toByteArray())
}
val chunks = paddedValue
.chunked(128)
for (chunk in chunks) {
val w = buildList {
addAll(chunk.toULongArray())
for (i in 16..<80) {
val s0 = get(i - 15).rotateRight(1) xor get(i - 15).rotateRight(8) xor (get(i - 15) shr 7)
val s1 = get(i - 2).rotateRight(19) xor get(i - 2).rotateRight(61) xor (get(i - 2) shr 6)
add(get(i - 16) + s0 + get(i - 7) + s1)
}
}
var a = h0
var b = h1
var c = h2
var d = h3
var e = h4
var f = h5
var g = h6
var h = h7
for (i in 0..<80) {
val s0 = a.rotateRight(28) xor a.rotateRight(34) xor a.rotateRight(39)
val s1 = e.rotateRight(14) xor e.rotateRight(18) xor e.rotateRight(41)
val ch = (e and f) xor (e.inv() and g)
val temp1 = h + s1 + ch + k[i] + w[i]
val maj = (a and b) xor (a and c) xor (b and c)
val temp2 = s0 + maj
h = g
g = f
f = e
e = d + temp1
d = c
c = b
b = a
a = temp1 + temp2
}
h0 += a
h1 += b
h2 += c
h3 += d
h4 += e
h5 += f
h6 += g
h7 += h
}
return byteArrayOf(
*h0.toByteArray(),
*h1.toByteArray(),
*h2.toByteArray(),
*h3.toByteArray(),
*h4.toByteArray(),
*h5.toByteArray(),
)
}
}

View File

@@ -0,0 +1,109 @@
package com.infendro.hash
import com.infendro.hash.util.addAll
import com.infendro.hash.util.toByteArray
import com.infendro.hash.util.toULongArray
object SHA512 : HashFunction() {
private val k = arrayOf(
0x428a2f98d728ae22UL, 0x7137449123ef65cdUL, 0xb5c0fbcfec4d3b2fUL, 0xe9b5dba58189dbbcUL, 0x3956c25bf348b538UL,
0x59f111f1b605d019UL, 0x923f82a4af194f9bUL, 0xab1c5ed5da6d8118UL, 0xd807aa98a3030242UL, 0x12835b0145706fbeUL,
0x243185be4ee4b28cUL, 0x550c7dc3d5ffb4e2UL, 0x72be5d74f27b896fUL, 0x80deb1fe3b1696b1UL, 0x9bdc06a725c71235UL,
0xc19bf174cf692694UL, 0xe49b69c19ef14ad2UL, 0xefbe4786384f25e3UL, 0x0fc19dc68b8cd5b5UL, 0x240ca1cc77ac9c65UL,
0x2de92c6f592b0275UL, 0x4a7484aa6ea6e483UL, 0x5cb0a9dcbd41fbd4UL, 0x76f988da831153b5UL, 0x983e5152ee66dfabUL,
0xa831c66d2db43210UL, 0xb00327c898fb213fUL, 0xbf597fc7beef0ee4UL, 0xc6e00bf33da88fc2UL, 0xd5a79147930aa725UL,
0x06ca6351e003826fUL, 0x142929670a0e6e70UL, 0x27b70a8546d22ffcUL, 0x2e1b21385c26c926UL, 0x4d2c6dfc5ac42aedUL,
0x53380d139d95b3dfUL, 0x650a73548baf63deUL, 0x766a0abb3c77b2a8UL, 0x81c2c92e47edaee6UL, 0x92722c851482353bUL,
0xa2bfe8a14cf10364UL, 0xa81a664bbc423001UL, 0xc24b8b70d0f89791UL, 0xc76c51a30654be30UL, 0xd192e819d6ef5218UL,
0xd69906245565a910UL, 0xf40e35855771202aUL, 0x106aa07032bbd1b8UL, 0x19a4c116b8d2d0c8UL, 0x1e376c085141ab53UL,
0x2748774cdf8eeb99UL, 0x34b0bcb5e19b48a8UL, 0x391c0cb3c5c95a63UL, 0x4ed8aa4ae3418acbUL, 0x5b9cca4f7763e373UL,
0x682e6ff3d6b2b8a3UL, 0x748f82ee5defb2fcUL, 0x78a5636f43172f60UL, 0x84c87814a1f0ab72UL, 0x8cc702081a6439ecUL,
0x90befffa23631e28UL, 0xa4506cebde82bde9UL, 0xbef9a3f7b2c67915UL, 0xc67178f2e372532bUL, 0xca273eceea26619cUL,
0xd186b8c721c0c207UL, 0xeada7dd6cde0eb1eUL, 0xf57d4f7fee6ed178UL, 0x06f067aa72176fbaUL, 0x0a637dc5a2c898a6UL,
0x113f9804bef90daeUL, 0x1b710b35131c471bUL, 0x28db77f523047d84UL, 0x32caab7b40c72493UL, 0x3c9ebe0a15c9bebcUL,
0x431d67c49c100d4cUL, 0x4cc5d4becb3e42b6UL, 0x597f299cfc657e2aUL, 0x5fcb6fab3ad6faecUL, 0x6c44198c4a475817UL,
)
override fun hash(
value: ByteArray,
): ByteArray {
var h0 = 0x6a09e667f3bcc908UL
var h1 = 0xbb67ae8584caa73bUL
var h2 = 0x3c6ef372fe94f82bUL
var h3 = 0xa54ff53a5f1d36f1UL
var h4 = 0x510e527fade682d1UL
var h5 = 0x9b05688c2b3e6c1fUL
var h6 = 0x1f83d9abfb41bd6bUL
var h7 = 0x5be0cd19137e2179UL
val ml = value.size.toULong() * 8UL
val paddedValue = buildList {
addAll(value)
add(0x80.toByte())
while (size % 128 != 120) {
add(0x00.toByte())
}
addAll(ml.toByteArray())
}
val chunks = paddedValue
.chunked(128)
for (chunk in chunks) {
val w = buildList {
addAll(chunk.toULongArray())
for (i in 16..<80) {
val s0 = get(i - 15).rotateRight(1) xor get(i - 15).rotateRight(8) xor (get(i - 15) shr 7)
val s1 = get(i - 2).rotateRight(19) xor get(i - 2).rotateRight(61) xor (get(i - 2) shr 6)
add(get(i - 16) + s0 + get(i - 7) + s1)
}
}
var a = h0
var b = h1
var c = h2
var d = h3
var e = h4
var f = h5
var g = h6
var h = h7
for (i in 0..<80) {
val s0 = a.rotateRight(28) xor a.rotateRight(34) xor a.rotateRight(39)
val s1 = e.rotateRight(14) xor e.rotateRight(18) xor e.rotateRight(41)
val ch = (e and f) xor (e.inv() and g)
val temp1 = h + s1 + ch + k[i] + w[i]
val maj = (a and b) xor (a and c) xor (b and c)
val temp2 = s0 + maj
h = g
g = f
f = e
e = d + temp1
d = c
c = b
b = a
a = temp1 + temp2
}
h0 += a
h1 += b
h2 += c
h3 += d
h4 += e
h5 += f
h6 += g
h7 += h
}
return byteArrayOf(
*h0.toByteArray(),
*h1.toByteArray(),
*h2.toByteArray(),
*h3.toByteArray(),
*h4.toByteArray(),
*h5.toByteArray(),
*h6.toByteArray(),
*h7.toByteArray(),
)
}
}

View File

@@ -0,0 +1,119 @@
package com.infendro.hash.util
import com.infendro.hash.util.ByteOrder.BIG_ENDIAN
import com.infendro.hash.util.ByteOrder.LITTLE_ENDIAN
internal fun MutableCollection<Byte>.addAll(
bytes: ByteArray,
) {
addAll(bytes.toList())
}
enum class ByteOrder {
BIG_ENDIAN,
LITTLE_ENDIAN,
}
internal fun UInt.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = ByteArray(4) { i ->
val offset = (3 - i) * 8
(this shr offset).toByte()
}
return when (order) {
BIG_ENDIAN -> bytes
LITTLE_ENDIAN -> bytes.reversedArray()
}
}
internal fun ULong.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = ByteArray(8) { i ->
val offset = (7 - i) * 8
(this shr offset).toByte()
}
return when (order) {
BIG_ENDIAN -> bytes
LITTLE_ENDIAN -> bytes.reversedArray()
}
}
internal fun ByteArray.toUInt(
order: ByteOrder = BIG_ENDIAN,
): UInt {
if (size != 4) throw Exception()
val bytes = when (order) {
BIG_ENDIAN -> this
LITTLE_ENDIAN -> this.reversedArray()
}
return bytes.fold(0U) { acc, byte ->
(acc shl 8) or (byte.toUInt() and 0xFFU)
}
}
internal fun Collection<Byte>.toUInt(
order: ByteOrder = BIG_ENDIAN,
): UInt {
return toByteArray().toUInt(order)
}
internal fun ByteArray.toULong(
order: ByteOrder = BIG_ENDIAN,
): ULong {
if (size != 8) throw Exception()
val bytes = when (order) {
BIG_ENDIAN -> this
LITTLE_ENDIAN -> this.reversedArray()
}
return bytes.fold(0UL) { acc, byte ->
(acc shl 8) or (byte.toULong() and 0xFFUL)
}
}
internal fun Collection<Byte>.toULong(
order: ByteOrder = BIG_ENDIAN,
): ULong {
return toByteArray().toULong(order)
}
internal fun ByteArray.toUIntArray(
order: ByteOrder = BIG_ENDIAN,
): UIntArray {
if (size % 4 != 0) throw Exception()
return toList()
.chunked(4)
.map { it.toUInt(order) }
.toUIntArray()
}
internal fun Collection<Byte>.toUIntArray(
order: ByteOrder = BIG_ENDIAN,
): UIntArray {
return toByteArray().toUIntArray(order)
}
internal fun ByteArray.toULongArray(
order: ByteOrder = BIG_ENDIAN,
): ULongArray {
if (size % 8 != 0) throw Exception()
return toList()
.chunked(8)
.map { it.toULong(order) }
.toULongArray()
}
internal fun Collection<Byte>.toULongArray(
order: ByteOrder = BIG_ENDIAN,
): ULongArray {
return toByteArray().toULongArray(order)
}