Module blake3
The BLAKE3 cryptographic hash function.
BLAKE3 is a modern cryptographic hash function that is fast, secure, and highly parallelizable. When you need a cryptographic hash function and otherwise have no constraints, pick BLAKE3.
The primary hashing interface is the hash function for simple one-shot hashing,
and the Hasher type for incremental hashing.
BLAKE3 also supports keyed hashing via keyed_hash for message authentication,
and key derivation via derive_key for generating cryptographic keys from context strings.
Unlike traditional hash functions that produce a fixed output size,
BLAKE3 is an extendable-output function (XOF).
While it defaults to 32-byte output like SHA-256,
it can produce arbitrarily long output through finalize_xof,
which returns an OutputReader that can generate unlimited hash bytes.
BLAKE3 achieves high performance through its tree-based structure, which allows parallel hashing of large inputs. When compiled with SIMD support (enabled by default), it can be significantly faster than SHA-256 and SHA-512.
The hash function is designed to be secure as:
- A general-purpose cryptographic hash (collision resistance, preimage resistance)
- A message authentication code (MAC) when used with a key
- A key derivation function (KDF)
- A pseudorandom function (PRF)
Examples
Basic hashing of data:
use blake3;
let hash = hash;
println!;
assert_eq!;
Incremental hashing with the Hasher type:
use Hasher;
let mut hasher = new;
hasher.update;
hasher.update;
hasher.update;
let hash = hasher.finalize;
assert_eq!;
Using keyed hashing for message authentication:
use blake3;
let key = ;
let mac = keyed_hash;
// Verify the MAC
let verification = keyed_hash;
assert_eq!;
Extended output for generating arbitrary-length hash output:
use Hasher;
let mut hasher = new;
hasher.update;
let mut extended_output = ;
let mut xof = hasher.finalize_xof;
xof.fill;
// Can generate more output from the same XOF reader
let mut more_output = ;
xof.fill;
Structs
- Hash An output of the default size, 32 bytes, which provides constant-time equality checking.
- Hasher An incremental hash state that can accept any number of writes.
-
HexError
The error type for
Hash::from_hex. -
OutputReader
An incremental reader for extended output, returned by
Hasher::finalize_xof.
Functions
- derive_key The key derivation function.
- hash The default hash function.
- keyed_hash The keyed hash function.