hash - cryptographic-style digests
Enable with use hash;. Computes fixed-size digests over bytes with common cryptographic-style algorithms (MD5, SHA-1, SHA-2, SHA-3, BLAKE2). Output is raw bytes; hex- or base64-encode it with the encoding library for a string.
For non-cryptographic checksums (transport integrity rather than content addressing), use crc instead.
use encoding;
use io;
use hash;
def input as bytes init encoding.encode("utf-8", "abc");
def digest as bytes init hash.compute("sha256", $input);
io.printf("sha256 digest is %d bytes\n", len($digest));Algorithm selection
One verb per operation, with the algorithm passed as a string, as in encoding.encode(codec, s). Algorithm names are lowercase.
| Algo string | Output width | Notes |
|---|---|---|
"md5" | 16 bytes | Non-cryptographic (broken for collision resistance): integrity / caching / legacy interop, never security. |
"sha1" | 20 bytes | Non-cryptographic (broken for collision resistance): legacy formats (e.g. SCRAM-SHA-1) only, never security. |
"sha256" | 32 bytes | The default choice for new code. |
"sha384" | 48 bytes | Truncated SHA-512; used by some TLS / JOSE suites. |
"sha512" | 64 bytes | Wider SHA-2 digest; used by some HMAC / TOTP variants. |
"sha3-224" / "sha3-256" / "sha3-384" / "sha3-512" | 28 / 32 / 48 / 64 bytes | SHA-3 (FIPS 202), the Keccak family; formats that name it. |
"blake2b-256" / "blake2b-384" / "blake2b-512" | 32 / 48 / 64 bytes | BLAKE2b (RFC 7693), unkeyed; faster than SHA-2 in software. |
"blake2s-256" | 32 bytes | BLAKE2s, the 32-bit-friendly variant. |
SHA-3 comes from Go's standard library, BLAKE2 from golang.org/x/crypto; every name works on both binaries and in compute, hmac and stream alike.
Passing an unknown algorithm is a positioned runtime error that lists the supported set: hash.compute: unknown digest algorithm "md4"; known: "md5" (non-cryptographic), "sha1" (non-cryptographic), "sha256", "sha384", "sha512", "sha3-224", ....
One-shot
| Call | Returns | Notes |
|---|---|---|
hash.compute(algo, b) | bytes | Full digest of the entire input. |
hash.hmac(algo, message, key) | bytes | Keyed-hash MAC (RFC 2104) over the same algorithms. |
hash.equal(a, b) | bool | Constant-time equality of two bytes (see below). |
The algorithm is always a value (compute("sha256", b)), the same shape as hmac and crypto.deriveKey: one convention across the hash / crypto family, so a runtime-negotiated algorithm (SCRAM, JWT) is a variable, not a dispatch ladder. There are no hash.sha256(b) shortcuts (see rejected.md).
HMAC
hash.hmac(algo, message, key) computes the keyed-hash message authentication code (RFC 2104), the primitive behind JWT (HS256), TOTP, AWS SigV4, and webhook signatures. key and message are bytes; the result is the raw MAC as bytes (hex / base64 via encoding, matching compute).
use io;
use hash;
use encoding;
def key as bytes init encoding.encode("utf-8", "secret");
def msg as bytes init encoding.encode("utf-8", "payload");
def mac as bytes init hash.hmac("sha256", $msg, $key);
io.printf("%s\n", encoding.toText("hex", $mac));To verify, recompute the MAC over the same message and compare it to the received one (comparing the full digests, not a prefix) with hash.equal.
Constant-time comparison
hash.equal(a, b) reports whether two bytes are identical in constant time: the comparison takes the same time whether the values differ in the first byte or the last (crypto/subtle.ConstantTimeCompare). Use it to check a received MAC, digest, or token against the expected one: a plain == on the raw bytes (or a byte-by-byte loop that returns early) leaks where the first mismatch is through its timing, which over many attempts lets an attacker forge a valid MAC one byte at a time. Values of different lengths compare unequal.
use hash;
# expected and received are both `bytes` (e.g. two MACs)
if (hash.equal($expected, $received)) {
# authentic
}Reach for hash.equal whenever the comparison is a security decision; ordinary == on non-secret data is fine.
Streaming
For inputs too large to hold in memory (files, large network reads), feed chunks into a stream handle and finalize:
use hash;
def s as hash.Stream init hash.stream("sha256");
hash.update($s, $chunkOne);
hash.update($s, $chunkTwo);
def digest as bytes init hash.finalize($s);| Call | Returns | Notes |
|---|---|---|
hash.stream(algo) | hash.Stream | Allocate a fresh handle for the named algorithm. |
hash.update($s, $bytes) | null | Feed one chunk. Mutates the handle's state by side effect. |
hash.finalize($s) | bytes | Compute the digest and consume the handle. A later update / finalize throws kind "closed". |
hash.discard($s) | null | Drop a stream without computing its digest, releasing its state. The abort path for a handle opened but abandoned. On a finalized or discarded stream it does nothing, so defer hash.discard($s); is safe. |
hash.Stream carries an integer id field that indexes into a Go-side map of live state. Treat it as an opaque token; do not read or mutate the field. At most 4096 streams are open at once; each needs a matching finalize or discard.
Composing with encoding
There are no convenience wrappers like hash.md5String(s), so there is one way to do it: strings become bytes through encoding.encode, the digest stays as bytes, and encoding.toText("hex", $digest) hex-encodes it, as the example examples/hash.j does.
Errors
All errors are positioned at the call site.
| Cause | Example message |
|---|---|
| Wrong arity | hash.compute expects 2 arguments (algo, bytes), got 1 |
| Wrong argument type | hash.compute: second argument must be bytes, got string |
| Unknown algorithm | hash.compute: unknown digest algorithm "md4"; known: "md5" (non-cryptographic), "sha1" (non-cryptographic), "sha256", ... |
Reuse of a finalized or discarded stream (kind "closed") | hash.update: stream 3 has already been finalized or discarded, or never existed |
Leaked streams (kind "limit") | hash.stream: too many open streams (limit 4096); each needs a matching hash.finalize or discard |
See also
- crc.md - non-cryptographic checksums (CRC-32, CRC-64).
- encoding.md - hex / base64 rendering of a digest.
- crypto.md - key derivation, encryption, and signatures.
Academy talks
| Talk | What it shows |
|---|---|
| Crypto, tokens and certificates with Jennifer | hashes, HMACs and constant-time comparison |
| Storing data in the cloud: S3, WebDAV, Koofr and Seafile | checksums to verify uploaded files |