std/hash

Hashes and checksums, in plain C in the runtime, checked against the reference vectors and Python's hashlib.

import "std/hash"

print(digest_hex("sha256", to_bytes("abc")))
Hasher h = hasher("blake3")?
h.update(to_bytes("part one"))
h.update(to_bytes("part two"))
print(hex_of(h.finish()))
print(hex_of(hash_file("sha256", "build/app")?))

HASH_ALGORITHMS are md5, sha1, sha224, sha256, sha384, sha512, sha3_224, sha3_256, sha3_384, sha3_512, blake2b, blake2s, blake3, crc32, crc32c, adler32, and xxhash64. MD5 and SHA-1 are broken for security: use them only where an old protocol or format asks for them. The checksums find accidents, not attacks.

Function Result
hasher(algorithm, key, length) a Hasher, fed with update(data) and update_text(text) and ended with finish(). key makes BLAKE2b (up to 64 bytes), BLAKE2s (up to 32), and BLAKE3 (32) keyed hashes, and is xxHash64's seed (8 bytes); length asks BLAKE2 for a shorter hash and BLAKE3 for one of any length
digest(algorithm, data), digest_hex(algorithm, data) a hash at once, as bytes or in hexadecimal
hash_file(algorithm, path) a file's hash, read a part at a time
hex_of(data) bytes in lowercase hexadecimal
md5, sha1, sha224, sha256, sha384, sha512, sha3_256, sha3_512, blake2b, blake2s, blake3 each, of bytes
crc32(data), crc32c(data), adler32(data) a u32
xxhash64(data, seed) a u64