(* Sha256: the SHA-256 of some bytes, as 64 lower-case hex digits, to check the site's archives against the sums published beside them. It needs only Buf and Str. It leaves out a streaming interface: the whole message is given at once. *) module Sha256; import Buf, Str, Heap, Check; const BlockLen = 64; (* bytes in a block, the unit the hash works on *) Digits = "0123456789abcdef"; (* the first 32 bits of the cube roots of the first 64 primes *) K: [64]U32 = [ 0428A2F98H, 071374491H, 0B5C0FBCFH, 0E9B5DBA5H, 03956C25BH, 059F111F1H, 0923F82A4H, 0AB1C5ED5H, 0D807AA98H, 012835B01H, 0243185BEH, 0550C7DC3H, 072BE5D74H, 080DEB1FEH, 09BDC06A7H, 0C19BF174H, 0E49B69C1H, 0EFBE4786H, 00FC19DC6H, 0240CA1CCH, 02DE92C6FH, 04A7484AAH, 05CB0A9DCH, 076F988DAH, 0983E5152H, 0A831C66DH, 0B00327C8H, 0BF597FC7H, 0C6E00BF3H, 0D5A79147H, 006CA6351H, 014292967H, 027B70A85H, 02E1B2138H, 04D2C6DFCH, 053380D13H, 0650A7354H, 0766A0ABBH, 081C2C92EH, 092722C85H, 0A2BFE8A1H, 0A81A664BH, 0C24B8B70H, 0C76C51A3H, 0D192E819H, 0D6990624H, 0F40E3585H, 0106AA070H, 019A4C116H, 01E376C08H, 02748774CH, 034B0BCB5H, 0391C0CB3H, 04ED8AA4AH, 05B9CCA4FH, 0682E6FF3H, 0748F82EEH, 078A5636FH, 084C87814H, 08CC70208H, 090BEFFFAH, 0A4506CEBH, 0BEF9A3F7H, 0C67178F2H]; type (* The eight words of the running hash. *) State = [8]U32; (* Mixes the 64 bytes p into the state h. *) proc block(var h: State; p: []const U8); var w: [64]U32; a, b, c, d, e, f, g, hh, t1, t2, s0, s1: U32; i: INT; begin for i := 0 to 15 do w[i] := U32(Str.be(p[4 * i..4 * i + 4])); end; for i := 16 to 63 do s0 := ROR(w[i - 15], 7) xor ROR(w[i - 15], 18) xor (w[i - 15] shr 3); s1 := ROR(w[i - 2], 17) xor ROR(w[i - 2], 19) xor (w[i - 2] shr 10); w[i] := w[i - 16] + s0 + w[i - 7] + s1; end; a := h[0]; b := h[1]; c := h[2]; d := h[3]; e := h[4]; f := h[5]; g := h[6]; hh := h[7]; for i := 0 to 63 do s1 := ROR(e, 6) xor ROR(e, 11) xor ROR(e, 25); t1 := hh + s1 + ((e & f) xor (~e & g)) + K[i] + w[i]; s0 := ROR(a, 2) xor ROR(a, 13) xor ROR(a, 22); t2 := s0 + ((a & b) xor (a & c) xor (b & c)); hh := g; g := f; f := e; e := d + t1; d := c; c := b; b := a; a := t1 + t2; end; h[0] := h[0] + a; h[1] := h[1] + b; h[2] := h[2] + c; h[3] := h[3] + d; h[4] := h[4] + e; h[5] := h[5] + f; h[6] := h[6] + g; h[7] := h[7] + hh; end; (* Adds the SHA-256 of data to b, as 64 hex digits. *) proc hex*(data: []const U8; var b: Buf.Buf); var h: State; tail: [2 * BlockLen]U8; whole, rest, n, i, j: INT; word: U32; begin h := [06A09E667H, 0BB67AE85H, 03C6EF372H, 0A54FF53AH, 0510E527FH, 09B05688CH, 01F83D9ABH, 05BE0CD19H]; whole := LEN(data) div BlockLen * BlockLen; for i := 0 to whole - 1 by BlockLen do block(h, data[i..i + BlockLen]); end; (* the rest, a 1 bit, zeros, and the length in bits, in one or two blocks *) rest := LEN(data) - whole; FILL(tail, 0); COPY(tail[0..rest], data[whole..LEN(data)]); tail[rest] := 80H; n := BlockLen; if rest >= BlockLen - 8 then n := 2 * BlockLen; end; Str.putBE(tail[n - 8..n], U64(LEN(data)) * 8); for i := 0 to n - 1 by BlockLen do block(h, tail[i..i + BlockLen]); end; for i := 0 to 7 do word := h[i]; for j := 7 to 0 by -1 do Buf.add(b, Digits[INT(word shr U32(4 * j)) & 0FH]); end; end; end; (* The module's test: the standard examples, one block, two blocks, and a message whose padding needs a second block. *) proc main*; var b: Buf.Buf; m: [1000]U8; begin Buf.init(b, Heap.heap); hex("", b); Check.str("empty", Buf.bytes(b), "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); Buf.reset(b); hex("abc", b); Check.str("abc", Buf.bytes(b), "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"); Buf.reset(b); hex("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", b); Check.str("two blocks", Buf.bytes(b), "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"); Buf.reset(b); FILL(m, 'a'); hex(m[0..55], b); Check.str("55 bytes", Buf.bytes(b), "9f4390f8d30c2dd92ec9f095b65e2b9ae9b0a925a5258e241c9f1e910f734318"); Buf.reset(b); hex(m[0..1000], b); Check.str("1000 bytes", Buf.bytes(b), "41edece42d63e8d9bf515a9ba6932e1c20cbc9f5a5d134645adb5db1b9737ea3"); end; end Sha256.