Reference

std/digest/lib

std/digest/src/lib.trb

Message digests: Sha256 and Sha512, the SHA-256 and SHA-512 of FIPS 180-4, and the Digest both answer. SHA-256 is what the package manager pins a package by (docs/design/RELEASE.md section 7.2) and what a download is checked against; SHA-512 is what Ed25519 hashes with (std/signature).

Ordinary TorbScript with no native. SHA-256 works over Int, every word kept below 2^32 by a mask, so nothing overflows; SHA-512 over UInt64 and its wrapping sum. The same bytes give the same digest in both back ends.

Examples

print Sha256.of("abc".bytes().toList()).hex()
// prints ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad

var hasher = Sha256()
hasher.add "ab".bytes().toList()
hasher.add "c".bytes().toList()
print hasher.finished().hex()

type Digest

type Digest with Show

What a hash function answers: its bytes, most significant first, shown as lowercase hexadecimal.

field bytes

bytes: List<UInt8>

Not documented.

fn hex

fn hex(): String

Two lowercase hexadecimal digits per byte: the spelling of sha256sum and of a lock file.

fn show

fn show(): String

Not documented.

type Sha256

type Sha256

SHA-256, fed in pieces: Sha256.add takes the next bytes and Sha256.finished answers the digest of everything added so far without changing the hasher, so a hasher can be read, fed more, and read again.

fn of

static fn of(bytes: Bytes): Digest

The digest of bytes in one call.

fn add

var fn add(bytes: Bytes)

Feeds the next bytes.

fn finished

fn finished(): Digest

The digest of everything added so far. The hasher itself is unchanged and may be fed more.