Skip to main content

Module hash

Module hash 

Source
Available on crate features 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:

AlgorithmOne-shotStreamingOutputFeature
BLAKE3blake3()Blake3Hasher32 Bhash-blake3
BLAKE3 XOFblake3_long()Blake3HasherN Bhash-blake3
SHA-256sha256()Sha256Hasher32 Bhash-sha2
SHA-512sha512()Sha512Hasher64 Bhash-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§

Blake3Hasherhash-blake3
Streaming BLAKE3 hasher for inputs that don’t fit in memory or arrive in chunks.
Sha256Hasherhash-sha2
Streaming SHA-256 hasher for inputs that don’t fit in memory.
Sha512Hasherhash-sha2
Streaming SHA-512 hasher for inputs that don’t fit in memory.

Constants§

BLAKE3_OUTPUT_LENhash-blake3
Length of a BLAKE3 digest, in bytes. Equal to 32.
SHA256_OUTPUT_LENhash-sha2
Length of a SHA-256 digest, in bytes. Equal to 32.
SHA512_OUTPUT_LENhash-sha2
Length of a SHA-512 digest, in bytes. Equal to 64.

Functions§

blake3hash-blake3
Compute a 32-byte BLAKE3 digest of data.
blake3_longhash-blake3
Compute a BLAKE3 digest of arbitrary length via the extendable-output function (XOF) mode.
sha256hash-sha2
Compute a 32-byte SHA-256 digest of data.
sha512hash-sha2
Compute a 64-byte SHA-512 digest of data.