Blake3

Struct Blake3 

pub struct Blake3 { /* 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§

§

impl Hasher

pub fn new() -> Hasher

Construct a new Hasher for the regular hash function.

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

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

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.

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.

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 (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.

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.

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.

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.

pub fn update_reader(&mut self, reader: impl Read) -> Result<&mut Hasher, Error>

As update, but reading from a std::io::Read implementation.

Hasher implements std::io::Write, so it’s possible to use std::io::copy to update a Hasher from any reader. Unfortunately, this standard approach can limit performance, because copy currently 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_reader avoids 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::Read might be std::fs::File, but note that memory mapping can be faster than this method for hashing large files. See update_mmap and update_mmap_rayon, which require the mmap and (for the latter) rayon Cargo features.

This method requires the std Cargo feature, which is enabled by default.

§Example
// Hash standard input.
let mut hasher = blake3::Hasher::new();
hasher.update_reader(std::io::stdin().lock())?;
println!("{}", hasher.finalize());

Trait Implementations§

§

impl BlockSizeUser for Hasher

§

type BlockSize = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, B0>

Size of the block in bytes.
§

fn block_size() -> usize

Return block size in bytes.
§

impl Clone for Hasher

§

fn clone(&self) -> Hasher

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
§

impl Debug for Hasher

§

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

Formats the value using the given formatter. Read more
§

impl Default for Hasher

§

fn default() -> Hasher

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

impl ExtendableOutput for Hasher

§

type Reader = OutputReader

Reader
§

fn finalize_xof(self) -> <Hasher as ExtendableOutput>::Reader

Retrieve XOF reader and consume hasher instance.
§

fn finalize_xof_into(self, out: &mut [u8])

Finalize XOF and write result into out.
§

fn digest_xof(input: impl AsRef<[u8]>, output: &mut [u8])
where Self: Default,

Compute hash of data and write it into output.
§

fn finalize_boxed(self, output_size: usize) -> Box<[u8]>

Retrieve result into a boxed slice of the specified size and consume the hasher. Read more
§

impl ExtendableOutputReset for Hasher

§

fn finalize_xof_reset(&mut self) -> <Hasher as ExtendableOutput>::Reader

Retrieve XOF reader and reset hasher instance state.
§

fn finalize_xof_reset_into(&mut self, out: &mut [u8])

Finalize XOF, write result into out, and reset the hasher state.
§

fn finalize_boxed_reset(&mut self, output_size: usize) -> Box<[u8]>

Retrieve result into a boxed slice of the specified size and reset the hasher state. Read more
§

impl FixedOutput for Hasher

§

fn finalize_into( self, out: &mut GenericArray<u8, <Hasher as OutputSizeUser>::OutputSize>, )

Consume value and write result into provided array.
§

fn finalize_fixed(self) -> GenericArray<u8, Self::OutputSize>

Retrieve result and consume the hasher instance.
§

impl FixedOutputReset for Hasher

§

fn finalize_into_reset( &mut self, out: &mut GenericArray<u8, <Hasher as OutputSizeUser>::OutputSize>, )

Write result into provided array and reset the hasher state.
§

fn finalize_fixed_reset(&mut self) -> GenericArray<u8, Self::OutputSize>

Retrieve result and reset the hasher state.
§

impl HasherExt for Hasher

§

fn new_from_context_key(context_key: &[u8; 32]) -> Hasher

Similar to Hasher::new_derive_key but using a pre-hashed [ContextKey] from [hash_derive_key_context]. Read more
§

fn set_input_offset(&mut self, offset: u64) -> &mut Hasher

Configure the Hasher to process a chunk or subtree starting at offset bytes into the whole input. Read more
§

fn finalize_non_root(&self) -> [u8; 32]

Finalize the non-root hash (“chaining value”) of the current chunk or subtree. Read more
§

impl KeyInit for Hasher

§

fn new(key: &GenericArray<u8, <Hasher as KeySizeUser>::KeySize>) -> Hasher

Create new value from fixed size key.
§

fn new_from_slice(key: &[u8]) -> Result<Self, InvalidLength>

Create new value from variable size key.
§

fn generate_key( rng: impl CryptoRng + RngCore, ) -> GenericArray<u8, Self::KeySize>

Generate random key using the provided CryptoRng.
§

impl KeySizeUser for Hasher

§

type KeySize = UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>

Key size in bytes.
§

fn key_size() -> usize

Return key size in bytes.
§

impl OutputSizeUser for Hasher

§

type OutputSize = UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>

Size of the output in bytes.
§

fn output_size() -> usize

Return output size in bytes.
§

impl Reset for Hasher

§

fn reset(&mut self)

Reset state to its initial value.
§

impl Update for Hasher

§

fn update(&mut self, data: &[u8])

Update state using the provided data.
§

fn chain(self, data: impl AsRef<[u8]>) -> Self
where Self: Sized,

Digest input data in a chained manner.
§

impl Write for Hasher

§

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

This is equivalent to update.

§

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

Flushes 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, args: 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 Self
where Self: Sized,

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

impl HashMarker for Hasher

§

impl MacMarker for Hasher

Auto Trait Implementations§

§

impl Freeze for Hasher

§

impl RefUnwindSafe for Hasher

§

impl Send for Hasher

§

impl Sync for Hasher

§

impl Unpin for Hasher

§

impl UnwindSafe for Hasher

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<D> Digest for D
where D: FixedOutput + Default + Update + HashMarker,

§

fn new() -> D

Create new hasher instance.
§

fn new_with_prefix(data: impl AsRef<[u8]>) -> D
where D: Default,

Create new hasher instance which has processed the provided data.
§

fn update(&mut self, data: impl AsRef<[u8]>)

Process data, updating the internal state.
§

fn chain_update(self, data: impl AsRef<[u8]>) -> D

Process input data in a chained manner.
§

fn finalize(self) -> GenericArray<u8, <D as OutputSizeUser>::OutputSize>

Retrieve result and consume hasher instance.
§

fn finalize_into( self, out: &mut GenericArray<u8, <D as OutputSizeUser>::OutputSize>, )

Write result into provided array and consume the hasher instance.
§

fn finalize_reset( &mut self, ) -> GenericArray<u8, <D as OutputSizeUser>::OutputSize>

Retrieve result and reset hasher instance.
§

fn finalize_into_reset( &mut self, out: &mut GenericArray<u8, <D as OutputSizeUser>::OutputSize>, )

Write result into provided array and reset the hasher instance.
§

fn reset(&mut self)
where D: Reset,

Reset hasher instance to its initial state.
§

fn output_size() -> usize

Get output size of the hasher
§

fn digest( data: impl AsRef<[u8]>, ) -> GenericArray<u8, <D as OutputSizeUser>::OutputSize>

Compute hash of data.
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

§

impl<D> DynDigest for D
where D: Update + FixedOutputReset + Reset + Clone + 'static,

§

fn update(&mut self, data: &[u8])

Digest input data. Read more
§

fn finalize_reset(&mut self) -> Box<[u8]>

Retrieve result and reset hasher instance
§

fn finalize(self: Box<D>) -> Box<[u8]>

Retrieve result and consume boxed hasher instance
§

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

Write result into provided array and consume the hasher instance. Read more
§

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

Write result into provided array and reset the hasher instance. Read more
§

fn reset(&mut self)

Reset hasher instance to its initial state.
§

fn output_size(&self) -> usize

Get output size of the hasher
§

fn box_clone(&self) -> Box<dyn DynDigest>

Clone hasher state into a boxed trait object
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where 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.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Mac for T
where T: Update + FixedOutput + MacMarker,

§

fn new(key: &GenericArray<u8, <T as KeySizeUser>::KeySize>) -> T
where T: KeyInit,

Create new value from fixed size key.
§

fn new_from_slice(key: &[u8]) -> Result<T, InvalidLength>
where T: KeyInit,

Create new value from variable size key.
§

fn update(&mut self, data: &[u8])

Update state using the provided data.
§

fn chain_update(self, data: impl AsRef<[u8]>) -> T

Process input data in a chained manner.
§

fn finalize(self) -> CtOutput<T>

Obtain the result of a [Mac] computation as a [CtOutput] and consume [Mac] instance.
§

fn finalize_reset(&mut self) -> CtOutput<T>

Obtain the result of a [Mac] computation as a [CtOutput] and reset [Mac] instance.
§

fn reset(&mut self)
where T: Reset,

Reset MAC instance to its initial state.
§

fn verify( self, tag: &GenericArray<u8, <T as OutputSizeUser>::OutputSize>, ) -> Result<(), MacError>

Check if tag/code value is correct for the processed input.
§

fn verify_reset( &mut self, tag: &GenericArray<u8, <T as OutputSizeUser>::OutputSize>, ) -> Result<(), MacError>

Check if tag/code value is correct for the processed input and reset [Mac] instance.
§

fn verify_slice(self, tag: &[u8]) -> Result<(), MacError>

Check truncated tag correctness using all bytes of calculated tag. Read more
§

fn verify_slice_reset(&mut self, tag: &[u8]) -> Result<(), MacError>

Check truncated tag correctness using all bytes of calculated tag and reset [Mac] instance. Read more
§

fn verify_truncated_left(self, tag: &[u8]) -> Result<(), MacError>

Check truncated tag correctness using left side bytes (i.e. tag[..n]) of calculated tag. Read more
§

fn verify_truncated_right(self, tag: &[u8]) -> Result<(), MacError>

Check truncated tag correctness using right side bytes (i.e. tag[n..]) of calculated tag. Read more
§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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 T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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 T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<W> WriteBytesExt for W
where 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
§

impl<W> Writer for W
where W: Write,

§

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

Write the given DER-encoded bytes as output.
§

fn write_byte(&mut self, byte: u8) -> Result<(), Error>

Write a single byte.
§

impl<W> WriterBackend for W
where W: Write,

§

fn pb_write_u8(&mut self, x: u8) -> Result<(), Error>

Write a u8
§

fn pb_write_u32(&mut self, x: u32) -> Result<(), Error>

Write a u32
§

fn pb_write_i32(&mut self, x: i32) -> Result<(), Error>

Write a i32
§

fn pb_write_f32(&mut self, x: f32) -> Result<(), Error>

Write a f32
§

fn pb_write_u64(&mut self, x: u64) -> Result<(), Error>

Write a u64
§

fn pb_write_i64(&mut self, x: i64) -> Result<(), Error>

Write a i64
§

fn pb_write_f64(&mut self, x: f64) -> Result<(), Error>

Write a f64
§

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

Write all bytes in buf
§

impl<T> ErasedDestructor for T
where T: 'static,