Yokan / rebuild
Course 1 8624561

Digests.wasm.kt

composeApp/src/wasmJsMain/kotlin/eu/friedcutlet/yokan/util/Digests.wasm.kt

implementation100 lines
1package eu.friedcutlet.yokan.util23// Hashes must be real (cache keys, tracker signatures), and SubtleCrypto's4// digest is async — so both digests are implemented in plain Kotlin.56actual fun sha256(data: ByteArray): ByteArray {7    val k = intArrayOf(8        0x428a2f98, 0x71374491, -0x4a3f0431, -0x164a245b, 0x3956c25b, 0x59f111f1, -0x6dc07d5c, -0x54e3a12b,9        -0x27f85568, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, -0x7f214e02, -0x6423f959, -0x3e640e8c,10        -0x1b64963f, -0x1041b87a, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,11        -0x67c1aeae, -0x57ce3993, -0x4ffcd838, -0x40a68039, -0x391ff40d, -0x2a586eb9, 0x06ca6351, 0x14292967,12        0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, -0x7e3d36d2, -0x6d8dd37b,13        -0x5d40175f, -0x57e599b5, -0x3db47490, -0x3893ae5d, -0x2e6d17e7, -0x2966f9dc, -0xbf1ca7b, 0x106aa070,14        0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,15        0x748f82ee, 0x78a5636f, -0x7b3787ec, -0x7338fdf8, -0x6f410006, -0x5baf9315, -0x41065c09, -0x398e870e,16    )17    var h0 = 0x6a09e667; var h1 = -0x4498517b; var h2 = 0x3c6ef372; var h3 = -0x5ab00ac618    var h4 = 0x510e527f; var h5 = -0x64fa9774; var h6 = 0x1f83d9ab; var h7 = 0x5be0cd1919    val padded = mdPad(data, bigEndian = true)20    val w = IntArray(64)21    var block = 022    while (block < padded.size) {23        for (t in 0 until 16) {24            val i = block + t * 425            w[t] = ((padded[i].toInt() and 0xff) shl 24) or ((padded[i + 1].toInt() and 0xff) shl 16) or26                ((padded[i + 2].toInt() and 0xff) shl 8) or (padded[i + 3].toInt() and 0xff)27        }28        for (t in 16 until 64) {29            val s0 = w[t - 15].rotateRight(7) xor w[t - 15].rotateRight(18) xor (w[t - 15] ushr 3)30            val s1 = w[t - 2].rotateRight(17) xor w[t - 2].rotateRight(19) xor (w[t - 2] ushr 10)31            w[t] = w[t - 16] + s0 + w[t - 7] + s132        }33        var a = h0; var b = h1; var c = h2; var d = h3; var e = h4; var f = h5; var g = h6; var h = h734        for (t in 0 until 64) {35            val s1 = e.rotateRight(6) xor e.rotateRight(11) xor e.rotateRight(25)36            val ch = (e and f) xor (e.inv() and g)37            val temp1 = h + s1 + ch + k[t] + w[t]38            val s0 = a.rotateRight(2) xor a.rotateRight(13) xor a.rotateRight(22)39            val maj = (a and b) xor (a and c) xor (b and c)40            val temp2 = s0 + maj41            h = g; g = f; f = e; e = d + temp1; d = c; c = b; b = a; a = temp1 + temp242        }43        h0 += a; h1 += b; h2 += c; h3 += d; h4 += e; h5 += f; h6 += g; h7 += h44        block += 6445    }46    return intArrayOf(h0, h1, h2, h3, h4, h5, h6, h7).toBigEndianBytes()47}4849actual fun sha1(data: ByteArray): ByteArray {50    var h0 = 0x67452301; var h1 = -0x10325477; var h2 = -0x67452302; var h3 = 0x10325476; var h4 = -0x3c2d1e1051    val padded = mdPad(data, bigEndian = true)52    val w = IntArray(80)53    var block = 054    while (block < padded.size) {55        for (t in 0 until 16) {56            val i = block + t * 457            w[t] = ((padded[i].toInt() and 0xff) shl 24) or ((padded[i + 1].toInt() and 0xff) shl 16) or58                ((padded[i + 2].toInt() and 0xff) shl 8) or (padded[i + 3].toInt() and 0xff)59        }60        for (t in 16 until 80) w[t] = (w[t - 3] xor w[t - 8] xor w[t - 14] xor w[t - 16]).rotateLeft(1)61        var a = h0; var b = h1; var c = h2; var d = h3; var e = h462        for (t in 0 until 80) {63            val (f, k) = when {64                t < 20 -> ((b and c) or (b.inv() and d)) to 0x5a82799965                t < 40 -> (b xor c xor d) to 0x6ed9eba166                t < 60 -> ((b and c) or (b and d) or (c and d)) to -0x70e4432467                else -> (b xor c xor d) to -0x359d3e2a68            }69            val temp = a.rotateLeft(5) + f + e + k + w[t]70            e = d; d = c; c = b.rotateLeft(30); b = a; a = temp71        }72        h0 += a; h1 += b; h2 += c; h3 += d; h4 += e73        block += 6474    }75    return intArrayOf(h0, h1, h2, h3, h4).toBigEndianBytes()76}7778/** Merkle–Damgård padding: 0x80, zeros, 64-bit big-endian bit length. */79private fun mdPad(data: ByteArray, bigEndian: Boolean): ByteArray {80    require(bigEndian)81    val bitLength = data.size.toLong() * 882    val paddedSize = ((data.size + 8) / 64 + 1) * 6483    val padded = data.copyOf(paddedSize)84    padded[data.size] = 0x80.toByte()85    for (i in 0 until 8) {86        padded[paddedSize - 1 - i] = ((bitLength ushr (8 * i)) and 0xff).toByte()87    }88    return padded89}9091private fun IntArray.toBigEndianBytes(): ByteArray {92    val out = ByteArray(size * 4)93    forEachIndexed { index, value ->94        out[index * 4] = (value ushr 24).toByte()95        out[index * 4 + 1] = (value ushr 16).toByte()96        out[index * 4 + 2] = (value ushr 8).toByte()97        out[index * 4 + 3] = value.toByte()98    }99    return out100}101

Search chapters, reference and source filenames.