Hash.SHA256

lib/Hash/SHA256.gob

SHA-256, as FIPS 180-4 specifies it: a Merkle-Damgard construction over 512-bit blocks, with a 256-bit state of eight 32-bit words.

Int is 64 bits wide, so every 32-bit word lives in an Int with its high half zero. An addition can produce 33 bits and a rotation can shift a word past bit 31, so each result is masked back to 32 bits before it is stored or used again. Words are never negative, which is why Int.shr (arithmetic) is the logical shift here.

type SHA256

The state of a hash in progress. block holds the bytes of the current partial block, filled how many of them there are, and length the total fed so far in bytes. schedule is the 64-word message schedule, kept here so that compressing a block allocates nothing.

fun new : fun() -> SHA256
fun update : fun(SHA256, Array Byte, Int, Int) -> Unit

Feed bytes from index from up to, not including, to.

fun updateText : fun(SHA256, String) -> Unit

Feed the UTF-8 bytes of s.

fun finish : fun(SHA256) -> Array Byte

The 32-byte digest of everything fed so far. h is left as it was, so more input may follow and finish may be asked again.

fun hex : fun(Array Byte) -> String

Lowercase hexadecimal, two digits a byte.

fun ofBytes : fun(Array Byte) -> String
fun ofText : fun(String) -> String