pub struct Hasher { /* private fields */ }Expand description
An incremental hash state that can accept any number of writes.
The rayon and mmap Cargo features enable additional methods on this
type related to multithreading and memory-mapped IO.
When the traits-preview Cargo feature is enabled, this type implements
several commonly used traits from the
digest crate. However, those
traits aren’t stable, and they’re expected to change in incompatible ways
before that crate reaches 1.0. For that reason, this crate makes no SemVer
guarantees for this feature, and callers who use it should expect breaking
changes between patch versions.
§Examples
// Hash an input incrementally.
let mut hasher = blake3::Hasher::new();
hasher.update(b"foo");
hasher.update(b"bar");
hasher.update(b"baz");
assert_eq!(hasher.finalize(), blake3::hash(b"foobarbaz"));
// Extended output. OutputReader also implements Read and Seek.
let mut output = [0; 1000];
let mut output_reader = hasher.finalize_xof();
output_reader.fill(&mut output);
assert_eq!(&output[..32], blake3::hash(b"foobarbaz").as_bytes());Implementations§
Source§impl Hasher
impl Hasher
Sourcepub fn new_keyed(key: &[u8; 32]) -> Self
pub fn new_keyed(key: &[u8; 32]) -> Self
Construct a new Hasher for the keyed hash function. See
keyed_hash.
Sourcepub fn new_derive_key(context: &str) -> Self
pub fn new_derive_key(context: &str) -> Self
Construct a new Hasher for the key derivation function. See
derive_key. The context string should be hardcoded, globally
unique, and application-specific.
Sourcepub fn reset(&mut self) -> &mut Self
pub fn reset(&mut self) -> &mut Self
Reset the Hasher to its initial state.
This is functionally the same as overwriting the Hasher with a new
one, using the same key or context string if any.
Sourcepub fn update(&mut self, input: &[u8]) -> &mut Self
pub fn update(&mut self, input: &[u8]) -> &mut Self
Add input bytes to the hash state. You can call this any number of times.
This method is always single-threaded. For multithreading support, see
update_rayon (enabled with the rayon Cargo feature).
Note that the degree of SIMD parallelism that update can use is limited by the size of
this input buffer. See update_reader.
Sourcepub fn finalize(&self) -> Hash
pub fn finalize(&self) -> Hash
Finalize the hash state and return the Hash of
the input.
This method is idempotent. Calling it twice will give the same result. You can also add more input and finalize again.
Sourcepub fn finalize_xof(&self) -> OutputReader
pub fn finalize_xof(&self) -> OutputReader
Finalize the hash state and return an OutputReader, which can
supply any number of output bytes.
This method is idempotent. Calling it twice will give the same result. You can also add more input and finalize again.
Sourcepub fn count(&self) -> u64
pub fn count(&self) -> u64
Return the total number of bytes hashed so far.
hazmat::HasherExt::set_input_offset does not affect this value. This only counts bytes
passed to update.
Trait Implementations§
Source§impl HasherExt for Hasher
impl HasherExt for Hasher
Source§fn new_from_context_key(context_key: &[u8; 32]) -> Hasher
fn new_from_context_key(context_key: &[u8; 32]) -> Hasher
Hasher::new_derive_key but using a pre-hashed ContextKey from
hash_derive_key_context. Read moreSource§fn set_input_offset(&mut self, offset: u64) -> &mut Hasher
fn set_input_offset(&mut self, offset: u64) -> &mut Hasher
Hasher to process a chunk or subtree starting at offset bytes into the
whole input. Read more