std

source of std::sha256

1// SHA-256 (FIPS 180-4), which the package registry checks files with.23import std::string;45const ROUNDS: [u32 : 64] = [6    0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,7    0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,8    0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,9    0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,10    0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,11    0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,12    0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,13    0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f214];1516// The 32-byte digest of the bytes.17exp fn sha256(bytes: &[u8]) -> [u8 : 32] {18    var state: [u32 : 8] = [19        0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd1920    ];21    const whole = bytes.length() / 64;22    for ([..whole]) |k| {23        compress(&var state, &bytes[k * 64..k * 64 + 64]);24    }25    // the rest, a 1 bit, zeros, then the length in bits, in one or two blocks26    var tail: [u8 : 128] = [0 : 128];27    const rest = bytes.length() - whole * 64;28    tail[..rest] = bytes[whole * 64..];29    tail[rest] = 0x80;30    const blocks: u64 = if (rest < 56) 1 else 2;31    const bits = bytes.length() * 8;32    for ([..8 to u64]) |k| {33        tail[blocks * 64 - 1 - k] = ((bits >> (k * 8)) & 0xff) to u8;34    }35    for ([..blocks]) |k| {36        compress(&var state, &tail[k * 64..k * 64 + 64]);37    }38    var digest: [u8 : 32] = [0 : 32];39    for ([..8]) |k| {40        for ([..4]) |b| {41            digest[k * 4 + b] = ((state[k] >> ((3 - b) * 8) to u32) & 0xff) to u8;42        }43    }44    return digest;45}4647// The digest as 64 lower-case hexadecimal digits.48exp fn sha256_text(bytes: &[u8]) -> String {49    const digest = sha256(&bytes);50    var out = String::empty();51    for (digest) |byte| {52        out.push(hex_digit(byte >> 4));53        out.push(hex_digit(byte & 15));54    }55    return move out;56}5758fn hex_digit(value: u8) -> u8 {59    return if (value < 10) '0' + value else 'a' + value - 10;60}6162fn rotate(x: u32, n: u32) -> u32 {63    return (x >> n) | (x << (32 - n));64}6566fn compress(state: &var [u32 : 8], block: &[u8]) {67    var w: [u32 : 64] = [0 : 64];68    for ([..16]) |k| {69        w[k] = (block[k * 4] to u32 << 24)70            | (block[k * 4 + 1] to u32 << 16)71            | (block[k * 4 + 2] to u32 << 8)72            | block[k * 4 + 3] to u32;73    }74    for ([16..64]) |k| {75        const s0 = rotate(w[k - 15], 7) ^ rotate(w[k - 15], 18) ^ (w[k - 15] >> 3);76        const s1 = rotate(w[k - 2], 17) ^ rotate(w[k - 2], 19) ^ (w[k - 2] >> 10);77        w[k] = w[k - 16] +% s0 +% w[k - 7] +% s1;78    }79    var a = state[0];80    var b = state[1];81    var c = state[2];82    var d = state[3];83    var e = state[4];84    var f = state[5];85    var g = state[6];86    var h = state[7];87    for ([..64]) |k| {88        const s1 = rotate(e, 6) ^ rotate(e, 11) ^ rotate(e, 25);89        const choice = (e & f) ^ (~e & g);90        const t1 = h +% s1 +% choice +% ROUNDS[k] +% w[k];91        const s0 = rotate(a, 2) ^ rotate(a, 13) ^ rotate(a, 22);92        const majority = (a & b) ^ (a & c) ^ (b & c);93        const t2 = s0 +% majority;94        h = g;95        g = f;96        f = e;97        e = d +% t1;98        d = c;99        c = b;100        b = a;101        a = t1 +% t2;102    }103    state[0] = state[0] +% a;104    state[1] = state[1] +% b;105    state[2] = state[2] +% c;106    state[3] = state[3] +% d;107    state[4] = state[4] +% e;108    state[5] = state[5] +% f;109    state[6] = state[6] +% g;110    state[7] = state[7] +% h;111}