hash-blake3 or hash-sha2 only.Expand description
Cryptographic hash functions.
Three algorithms ship in 0.4.0, exposed through a consistent free-function API plus matching streaming hashers for inputs that don’t fit in memory:
| Algorithm | One-shot | Streaming | Output | Feature |
|---|---|---|---|---|
| BLAKE3 | blake3() | Blake3Hasher | 32 B | hash-blake3 |
| BLAKE3 XOF | blake3_long() | Blake3Hasher | N B | hash-blake3 |
| SHA-256 | sha256() | Sha256Hasher | 32 B | hash-sha2 |
| SHA-512 | sha512() | Sha512Hasher | 64 B | hash-sha2 |
§Choosing a hash
Pick BLAKE3 unless you have a reason not to. It is the fastest
cryptographic hash on every modern platform — typically 4–10× faster
than SHA-256 on x86_64 with AVX2, and faster still on the wide vector
units of Apple Silicon. It is also tree-structured and SIMD-friendly,
so very large inputs hash at near-memcpy bandwidth.
Pick SHA-256 when you need ecosystem interop — TLS, JWT, Bitcoin, certificate fingerprints, any spec that names SHA-256 explicitly.
Pick SHA-512 when interop demands the wider output (some certificate authorities, some old protocols) or when running on a 64-bit machine where SHA-512 happens to be faster than SHA-256 (it processes 64-bit words natively).
§No-key, no-MAC
These functions hash data only. For keyed hashing (BLAKE3 keyed mode,
HMAC-SHA2), see the mac module — that’s the
Phase 0.5.0 surface and is the right home for authentication-tag
semantics. Using a raw hash function as a MAC is a security mistake;
we do not expose Hash::with_key here for that reason.
§Example
use crypt_io::hash;
let digest = hash::blake3(b"the quick brown fox");
assert_eq!(digest.len(), 32);With a streaming hasher:
use crypt_io::hash::Blake3Hasher;
let mut h = Blake3Hasher::new();
h.update(b"first chunk");
h.update(b" second chunk");
let digest = h.finalize();
assert_eq!(digest.len(), 32);Structs§
- Blake3
Hasher hash-blake3 - Streaming BLAKE3 hasher for inputs that don’t fit in memory or arrive in chunks.
- Sha256
Hasher hash-sha2 - Streaming SHA-256 hasher for inputs that don’t fit in memory.
- Sha512
Hasher hash-sha2 - Streaming SHA-512 hasher for inputs that don’t fit in memory.
Constants§
- BLAK
E3_ OUTPUT_ LEN hash-blake3 - Length of a BLAKE3 digest, in bytes. Equal to
32. - SHA256_
OUTPUT_ LEN hash-sha2 - Length of a SHA-256 digest, in bytes. Equal to
32. - SHA512_
OUTPUT_ LEN hash-sha2 - Length of a SHA-512 digest, in bytes. Equal to
64.
Functions§
- blake3
hash-blake3 - Compute a 32-byte BLAKE3 digest of
data. - blake3_
long hash-blake3 - Compute a BLAKE3 digest of arbitrary length via the extendable-output function (XOF) mode.
- sha256
hash-sha2 - Compute a 32-byte SHA-256 digest of
data. - sha512
hash-sha2 - Compute a 64-byte SHA-512 digest of
data.