ludic/docs/language/crypto/crypto-sha256.md
Orkuncakilkaya 9ae69e64b4
All checks were successful
docs / build-and-deploy (push) Successful in 3s
feat(stdlib): Crypto.* — SHA-256 + HMAC-SHA256, constant-time verify (#19)
The security-sensitive counterpart to the fast, non-cryptographic Hash.*
library: standard, test-vector-backed hashing for signed saves and message
integrity, kept in its own namespace so nobody reaches for the wrong tool.

  Crypto.sha256(s)                SHA-256 -> 64-char lowercase hex
  Crypto.hmac_sha256(key, msg)    HMAC-SHA256 -> 64-char hex
  Crypto.verify_hmac(key, msg, mac)  recompute + constant-time compare -> bool
  Crypto.hex(s)                   lowercase hex of a string's bytes
  Crypto.ct_equal(a, b)           constant-time string equality

The primitives are implemented from scratch in plain integer LLVM IR
(FIPS 180-4 / RFC 2104): no libc crypto, no data-dependent branches in the
compression rounds, so a given input hashes to the same 32 bytes on every
platform and run. Digests are returned as hex strings, not raw bytes, because
a `str` is null-terminated and a raw digest can contain a NUL. MAC checks use
a non-short-circuiting compare so timing does not leak how much of a forged tag
was correct.

Emitted on demand via g_uses_cryptort, mirroring the emit_hash prelude gate.
Scoped to the deterministic, known-answer-testable core; OS-backed
random_bytes (the one piece that can't be validated by test vectors) is left
for a follow-up.

Tested against published SHA-256 vectors (empty/"abc"/fox + 55/56/64-byte
multi-block padding) and HMAC-SHA256 vectors; wired into the self-host suite as
`crypto`. Docs: a new Crypto section with honest "what this protects / does
not" guidance, one page per method, all fences checked and in the inventory.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-30 13:37:02 +03:00

1 KiB

id name category kind tokens sig tip order ns member
crypto-sha256 Crypto.sha256 crypto namespace-method Crypto.sha256 Crypto.sha256(s) -> string SHA-256 of a string, as 64 hex characters. 1 Crypto sha256

Computes the SHA-256 digest of the bytes of s and returns it as a 64-character lowercase hex string. This is the standard, FIPS 180-4 algorithm — the same digest every other conforming implementation produces — so it is deterministic across platforms and runs and is backed by published known-answer vectors. Use it to fingerprint content, or as the building block under Crypto.hmac_sha256 for signing. On its own SHA-256 is not a message authentication code: anyone can recompute it, so it proves what the data is, not who produced it.

Parameters:

  • s — the string whose bytes are hashed
program Fingerprint {
  entry {
    let digest = Crypto.sha256("abc")
    print(digest)   # ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
  }
}