sha2

SHA-256 (FIPS 180-4) in pure Dawn, with an incremental digest and a one-shot hex helper.

use sha2/sha256

let one = sha256.hex(bytes)                       # 64 lower-case hex digits
let d = sha256.new() |> sha256.update(a) |> sha256.update(b)
let both = sha256.finish(d)                       # the digest of a then b

Digest is an opaque, immutable value: update answers a new one, and finish does not consume it, so feeding can go on after a finish.

It is not fast, about 25 to 30 MB/s on either backend; the header of src/sha256.dawn has the numbers.

Changes between versions: CHANGELOG.md.

API

sha2/sha256

Digest

type Digest

A digest in progress: the eight chaining words, the bytes of the block that is not full yet, and how many bytes have been fed in (the padding needs the total, and it is a bit count that must survive past 2^32).

sha256.new

fn new() -> Digest

A fresh digest, chained from the fractional parts of the square roots of the first eight primes.

sha256.update

fn update(d: Digest, b: Bytes) -> Digest

Feed bytes in. Any number, any number of times.

Whole blocks are compressed where they lie in b; only a block that straddles two calls is copied, once, to join its halves.

sha256.finish

fn finish(d: Digest) -> String

The digest as 64 lower-case hex digits. The state is not consumed -- it is a value, so finish can be called on a prefix and feeding can go on.

sha256.hex

fn hex(b: Bytes) -> String

The digest of b alone.