Struct blake3::Hasher[][src]

pub struct Hasher { /* fields omitted */ }
Expand description

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

In addition to its inherent methods, this type implements several commonly used traits from the digest and crypto_mac crates.

Performance note: The update and update_with_join methods perform poorly when the caller’s input buffer is small. See their method docs below. A 16 KiB buffer is large enough to leverage 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

impl Hasher[src]

pub fn new() -> Self[src]

Construct a new Hasher for the regular hash function.

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

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

pub fn new_derive_key(context: &str) -> Self[src]

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 Self[src]

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. However, depending on how much inlining the optimizer does, moving a Hasher might copy its entire CV stack, most of which is useless uninitialized bytes. This methods avoids that copy.

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

Add input bytes to the hash state. You can call this any number of times.

This method is always single-threaded. For multi-threading support, see update_with_join below.

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.

pub fn update_with_join<J: Join>(&mut self, input: &[u8]) -> &mut Self[src]

Add input bytes to the hash state, as with update, but potentially using multi-threading. See the example below, and the join module for a more detailed explanation.

To get any performance benefit from multi-threading, the input buffer size needs to be very large. As a rule of thumb on x86_64, there is no benefit to multi-threading inputs less than 128 KiB. Other platforms have different thresholds, and in general you need to benchmark your specific use case. Where possible, memory mapping an entire input file is recommended, to take maximum advantage of multi-threading without needing to tune a specific buffer size. Where memory mapping is not possible, good multi-threading performance requires doing IO on a background thread, to avoid sleeping all your worker threads while the input buffer is (serially) refilled. This is quite complicated compared to memory mapping.

Example

// Hash a large input using multi-threading. Note that multi-threading
// comes with some overhead, and it can actually hurt performance for small
// inputs. The meaning of "small" varies, however, depending on the
// platform and the number of threads. (On x86_64, the cutoff tends to be
// around 128 KiB.) You should benchmark your own use case to see whether
// multi-threading helps.
let input: &[u8] = some_large_input();
let mut hasher = blake3::Hasher::new();
hasher.update_with_join::<blake3::join::RayonJoin>(input);
let hash = hasher.finalize();

pub fn finalize(&self) -> Hash[src]

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

Notable traits for OutputReader

impl Read for OutputReader
[src]

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.

Trait Implementations

impl BlockInput for Hasher[src]

type BlockSize = U64

Block size

impl Clone for Hasher[src]

fn clone(&self) -> Hasher

Notable traits for Hasher

impl Write for Hasher
[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl Debug for Hasher[src]

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

Formats the value using the given formatter. Read more

impl Default for Hasher[src]

fn default() -> Self[src]

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

impl ExtendableOutput for Hasher[src]

type Reader = OutputReader

Reader

fn finalize_xof(self) -> Self::Reader[src]

Retrieve XOF reader and consume hasher instance.

fn finalize_xof_reset(&mut self) -> Self::Reader[src]

Retrieve XOF reader and reset hasher instance state.

fn finalize_boxed(self, n: usize) -> Box<[u8], Global>[src]

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

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

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

impl FixedOutput for Hasher[src]

type OutputSize = U32

Output size for fixed output digest

fn finalize_into(self, out: &mut GenericArray<u8, Self::OutputSize>)[src]

Write result into provided array and consume the hasher instance.

fn finalize_into_reset(&mut self, out: &mut GenericArray<u8, Self::OutputSize>)[src]

Write result into provided array and reset the hasher instance.

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

Retrieve result and consume the hasher instance.

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

Retrieve result and reset the hasher instance.

impl Mac for Hasher[src]

type OutputSize = U32

Output size of the [Mac]

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

Update MAC state with the given data.

fn reset(&mut self)[src]

Reset Mac instance.

fn finalize(self) -> Output<Self>[src]

Obtain the result of a Mac computation as a Output and consume Mac instance. Read more

fn finalize_reset(&mut self) -> Output<Self>[src]

Obtain the result of a Mac computation as a Output and reset Mac instance. Read more

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

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

impl NewMac for Hasher[src]

type KeySize = U32

Key size in bytes with which cipher guaranteed to be initialized.

fn new(key: &Key<Self>) -> Self[src]

Initialize new MAC instance from key with fixed size.

fn new_varkey(key: &[u8]) -> Result<Self, InvalidKeyLength>[src]

Initialize new MAC instance from key with variable size. Read more

impl Reset for Hasher[src]

fn reset(&mut self)[src]

Reset hasher instance to its initial state and return current state.

impl Update for Hasher[src]

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

Digest input data. Read more

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

Digest input data in a chained manner.

impl Write for Hasher[src]

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

This is equivalent to update.

fn flush(&mut self) -> Result<()>[src]

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

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

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

fn is_write_vectored(&self) -> bool[src]

🔬 This is a nightly-only experimental API. (can_vector)

Determines if this Writer has an efficient write_vectored implementation. Read more

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

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

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

🔬 This is a nightly-only experimental API. (write_all_vectored)

Attempts to write multiple buffers into this writer. Read more

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

Writes a formatted string into this writer, returning any error encountered. Read more

fn by_ref(&mut self) -> &mut Self1.0.0[src]

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

Auto Trait Implementations

impl RefUnwindSafe for Hasher

impl Send for Hasher

impl Sync for Hasher

impl Unpin for Hasher

impl UnwindSafe for Hasher

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<D> Digest for D where
    D: Update + FixedOutput + Reset + Clone + Default
[src]

type OutputSize = <D as FixedOutput>::OutputSize

Output size for Digest

pub fn new() -> D[src]

Create new hasher instance

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

Digest data, updating the internal state. Read more

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

Digest input data in a chained manner.

pub fn finalize(self) -> GenericArray<u8, <D as Digest>::OutputSize>[src]

Retrieve result and consume hasher instance.

pub fn finalize_reset(&mut self) -> GenericArray<u8, <D as Digest>::OutputSize>[src]

Retrieve result and reset hasher instance. Read more

pub fn reset(&mut self)[src]

Reset hasher instance to its initial state.

pub fn output_size() -> usize[src]

Get output size of the hasher

pub fn digest(data: &[u8]) -> GenericArray<u8, <D as Digest>::OutputSize>[src]

Convenience function to compute hash of the data. It will handle hasher creation, data feeding and finalization. Read more

impl<D> DynDigest for D where
    D: 'static + Update + FixedOutput + Reset + Clone
[src]

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

Digest input data. Read more

pub fn finalize_reset(&mut self) -> Box<[u8], Global>[src]

Retrieve result and reset hasher instance

pub fn finalize(self: Box<D, Global>) -> Box<[u8], Global>[src]

Retrieve result and consume boxed hasher instance

pub fn reset(&mut self)[src]

Reset hasher instance to its initial state.

pub fn output_size(&self) -> usize[src]

Get output size of the hasher

pub fn box_clone(&self) -> Box<dyn DynDigest + 'static, Global>[src]

Clone hasher state into a boxed trait object

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> Pointable for T

pub const ALIGN: usize

The alignment of pointer.

type Init = T

The type for initializers.

pub unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more

pub unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more

pub unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more

pub unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

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

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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

Performs the conversion.