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

Construct a new Hasher for the regular hash function.

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

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

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.

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.

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();

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.

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

Block size

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

Reader

Retrieve XOF reader and consume hasher instance.

Retrieve XOF reader and reset hasher instance state.

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

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

Output size for fixed output digest

Write result into provided array and consume the hasher instance.

Write result into provided array and reset the hasher instance.

Retrieve result and consume the hasher instance.

Retrieve result and reset the hasher instance.

Output size of the [Mac]

Update MAC state with the given data.

Reset Mac instance.

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

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

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

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

Initialize new MAC instance from key with fixed size.

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

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

Digest input data. Read more

Digest input data in a chained manner.

This is equivalent to update.

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

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

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

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

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

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

Attempts to write multiple buffers into this writer. Read more

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

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Output size for Digest

Create new hasher instance

Digest data, updating the internal state. Read more

Digest input data in a chained manner.

Retrieve result and consume hasher instance.

Retrieve result and reset hasher instance. Read more

Reset hasher instance to its initial state.

Get output size of the hasher

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

Digest input data. Read more

Retrieve result and reset hasher instance

Retrieve result and consume boxed hasher instance

Reset hasher instance to its initial state.

Get output size of the hasher

Clone hasher state into a boxed trait object

Performs the conversion.

Performs the conversion.

Should always be Self

The resulting type after obtaining ownership.

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

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

recently added

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.