Struct infinitree::Hasher

source ·
pub struct Hasher { /* private fields */ }
Expand description

An incremental hash state that can accept any number of writes.

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.

When the rayon Cargo feature is enabled, the update_rayon method is available for multithreaded hashing.

Performance note: The update method can’t take full advantage of SIMD optimizations if its input buffer is too small or oddly sized. Using a 16 KiB buffer, or any multiple of that, enables all currently supported SIMD instruction sets.

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

source

pub fn new() -> Hasher

Construct a new Hasher for the regular hash function.

source

pub fn new_keyed(key: &[u8; 32]) -> Hasher

Construct a new Hasher for the keyed hash function. See keyed_hash.

source

pub fn new_derive_key(context: &str) -> Hasher

Construct a new Hasher for the key derivation function. See derive_key. The context string should be hardcoded, globally unique, and application-specific.

source

pub fn reset(&mut self) -> &mut Hasher

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.

source

pub fn update(&mut self, input: &[u8]) -> &mut Hasher

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 below (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. The 8 KiB buffer currently used by std::io::copy is enough to leverage AVX2, for example, but not enough to leverage AVX-512. A 16 KiB buffer is large enough to leverage all currently supported SIMD instruction sets.

source

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.

source

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.

source

pub fn count(&self) -> u64

Return the total number of bytes hashed so far.

Trait Implementations§

source§

impl Clone for Hasher

source§

fn clone(&self) -> Hasher

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Hasher

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl Default for Hasher

source§

fn default() -> Hasher

Returns the “default value” for a type. Read more
source§

impl Write for Hasher

source§

fn write(&mut self, input: &[u8]) -> Result<usize, Error>

This is equivalent to update.

source§

fn flush(&mut self) -> Result<(), Error>

Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
1.36.0 · source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · source§

fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · source§

fn by_ref(&mut self) -> &mut Selfwhere Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> FieldWriter for Twhere T: Write + Send,

source§

fn write_next(&mut self, obj: impl Serialize + Send)

Write the next obj into the index
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> RmpWrite for Twhere T: Write,

§

type Error = Error

§

fn write_bytes(&mut self, buf: &[u8]) -> Result<(), <T as RmpWrite>::Error>

Write a slice of bytes to the underlying stream Read more
§

fn write_u8(&mut self, val: u8) -> Result<(), Self::Error>

Write a single byte to this stream
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<W> WriteBytesExt for Wwhere W: Write + ?Sized,

§

fn write_u8(&mut self, n: u8) -> Result<(), Error>

Writes an unsigned 8 bit integer to the underlying writer. Read more
§

fn write_i8(&mut self, n: i8) -> Result<(), Error>

Writes a signed 8 bit integer to the underlying writer. Read more
§

fn write_u16<T>(&mut self, n: u16) -> Result<(), Error>where T: ByteOrder,

Writes an unsigned 16 bit integer to the underlying writer. Read more
§

fn write_i16<T>(&mut self, n: i16) -> Result<(), Error>where T: ByteOrder,

Writes a signed 16 bit integer to the underlying writer. Read more
§

fn write_u24<T>(&mut self, n: u32) -> Result<(), Error>where T: ByteOrder,

Writes an unsigned 24 bit integer to the underlying writer. Read more
§

fn write_i24<T>(&mut self, n: i32) -> Result<(), Error>where T: ByteOrder,

Writes a signed 24 bit integer to the underlying writer. Read more
§

fn write_u32<T>(&mut self, n: u32) -> Result<(), Error>where T: ByteOrder,

Writes an unsigned 32 bit integer to the underlying writer. Read more
§

fn write_i32<T>(&mut self, n: i32) -> Result<(), Error>where T: ByteOrder,

Writes a signed 32 bit integer to the underlying writer. Read more
§

fn write_u48<T>(&mut self, n: u64) -> Result<(), Error>where T: ByteOrder,

Writes an unsigned 48 bit integer to the underlying writer. Read more
§

fn write_i48<T>(&mut self, n: i64) -> Result<(), Error>where T: ByteOrder,

Writes a signed 48 bit integer to the underlying writer. Read more
§

fn write_u64<T>(&mut self, n: u64) -> Result<(), Error>where T: ByteOrder,

Writes an unsigned 64 bit integer to the underlying writer. Read more
§

fn write_i64<T>(&mut self, n: i64) -> Result<(), Error>where T: ByteOrder,

Writes a signed 64 bit integer to the underlying writer. Read more
§

fn write_u128<T>(&mut self, n: u128) -> Result<(), Error>where T: ByteOrder,

Writes an unsigned 128 bit integer to the underlying writer.
§

fn write_i128<T>(&mut self, n: i128) -> Result<(), Error>where T: ByteOrder,

Writes a signed 128 bit integer to the underlying writer.
§

fn write_uint<T>(&mut self, n: u64, nbytes: usize) -> Result<(), Error>where T: ByteOrder,

Writes an unsigned n-bytes integer to the underlying writer. Read more
§

fn write_int<T>(&mut self, n: i64, nbytes: usize) -> Result<(), Error>where T: ByteOrder,

Writes a signed n-bytes integer to the underlying writer. Read more
§

fn write_uint128<T>(&mut self, n: u128, nbytes: usize) -> Result<(), Error>where T: ByteOrder,

Writes an unsigned n-bytes integer to the underlying writer. Read more
§

fn write_int128<T>(&mut self, n: i128, nbytes: usize) -> Result<(), Error>where T: ByteOrder,

Writes a signed n-bytes integer to the underlying writer. Read more
§

fn write_f32<T>(&mut self, n: f32) -> Result<(), Error>where T: ByteOrder,

Writes a IEEE754 single-precision (4 bytes) floating point number to the underlying writer. Read more
§

fn write_f64<T>(&mut self, n: f64) -> Result<(), Error>where T: ByteOrder,

Writes a IEEE754 double-precision (8 bytes) floating point number to the underlying writer. Read more
source§

impl<T> Transaction for Twhere T: Send + Sync + Write,