Struct Hasher
struct Hasher { ... }
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
#
Implementations
impl Hasher
fn new() -> SelfConstruct a new
Hasherfor the regular hash function.fn new_keyed(key: &[u8; 32]) -> SelfConstruct a new
Hasherfor the keyed hash function. Seekeyed_hash.fn new_derive_key(context: &str) -> SelfConstruct a new
Hasherfor the key derivation function. Seederive_key. The context string should be hardcoded, globally unique, and application-specific.fn reset(self: &mut Self) -> &mut SelfReset the
Hasherto its initial state.This is functionally the same as overwriting the
Hasherwith a new one, using the same key or context string if any.fn update(self: &mut Self, input: &[u8]) -> &mut SelfAdd 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 therayonCargo feature).Note that the degree of SIMD parallelism that
updatecan use is limited by the size of this input buffer. Seeupdate_reader.fn finalize(self: &Self) -> HashFinalize the hash state and return the
Hashof the input.This method is idempotent. Calling it twice will give the same result. You can also add more input and finalize again.
fn finalize_xof(self: &Self) -> OutputReaderFinalize 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.
fn count(self: &Self) -> u64Return the total number of bytes hashed so far.
fn update_reader<impl std::io::Read: std::io::Read>(self: &mut Self, reader: impl std::io::Read) -> std::io::Result<&mut Self>As
update, but reading from astd::io::Readimplementation.Hasherimplementsstd::io::Write, so it's possible to usestd::io::copyto update aHasherfrom any reader. Unfortunately, this standard approach can limit performance, becausecopycurrently uses an internal 8 KiB buffer that isn't big enough to take advantage of all SIMD instruction sets. (In particular, AVX-512 needs a 16 KiB buffer.)update_readeravoids this performance problem and is slightly more convenient.The internal buffer size this method uses may change at any time, and it may be different for different targets. The only guarantee is that it will be large enough for all of this crate's SIMD implementations on the current platform.
The most common implementer of
std::io::Readmight bestd::fs::File, but note that memory mapping can be faster than this method for hashing large files. Seeupdate_mmapandupdate_mmap_rayon, which require themmapand (for the latter)rayonCargo features.This method requires the
stdCargo feature, which is enabled by default.Example
# use std::fs::File; # use std::io; # fn main() -> io::Result<()> { // Hash standard input. let mut hasher = blake3::Hasher::new(); hasher.update_reader(std::io::stdin().lock())?; println!("{}", hasher.finalize()); # Ok(()) # }
impl Clone for Hasher
fn clone(self: &Self) -> Hasher
impl Debug for Hasher
fn fmt(self: &Self, f: &mut fmt::Formatter<'_>) -> fmt::Result
impl Default for Hasher
fn default() -> Self
impl Freeze for Hasher
impl RefUnwindSafe for Hasher
impl Send for Hasher
impl Sync for Hasher
impl Unpin for Hasher
impl UnwindSafe for Hasher
impl Write for Hasher
fn write(self: &mut Self, input: &[u8]) -> std::io::Result<usize>This is equivalent to
update.fn flush(self: &mut Self) -> std::io::Result<()>
impl<T> Any for Hasher
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for Hasher
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for Hasher
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> CloneToUninit for Hasher
unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)
impl<T> From for Hasher
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for Hasher
fn to_owned(self: &Self) -> Tfn clone_into(self: &Self, target: &mut T)
impl<T, U> Into for Hasher
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for Hasher
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for Hasher
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>