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
impl Hasher
pub fn new_keyed(key: &[u8; 32]) -> Hasher ⓘ
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 ⓘ
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 ⓘ
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 ⓘ
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
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 ⓘ
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
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>
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
impl BlockSizeUser for Hasher
§impl ExtendableOutput for Hasher
impl ExtendableOutput for Hasher
§type Reader = OutputReader
type Reader = OutputReader
§fn finalize_xof(self) -> <Hasher as ExtendableOutput>::Reader ⓘ
fn finalize_xof(self) -> <Hasher as ExtendableOutput>::Reader ⓘ
§fn finalize_xof_into(self, out: &mut [u8])
fn finalize_xof_into(self, out: &mut [u8])
out.§impl ExtendableOutputReset for Hasher
impl ExtendableOutputReset for Hasher
§fn finalize_xof_reset(&mut self) -> <Hasher as ExtendableOutput>::Reader ⓘ
fn finalize_xof_reset(&mut self) -> <Hasher as ExtendableOutput>::Reader ⓘ
§fn finalize_xof_reset_into(&mut self, out: &mut [u8])
fn finalize_xof_reset_into(&mut self, out: &mut [u8])
out, and reset the hasher state.§impl FixedOutput for Hasher
impl FixedOutput for Hasher
§fn finalize_into(
self,
out: &mut GenericArray<u8, <Hasher as OutputSizeUser>::OutputSize>,
)
fn finalize_into( self, out: &mut GenericArray<u8, <Hasher as OutputSizeUser>::OutputSize>, )
§fn finalize_fixed(self) -> GenericArray<u8, Self::OutputSize>
fn finalize_fixed(self) -> GenericArray<u8, Self::OutputSize>
§impl FixedOutputReset for Hasher
impl FixedOutputReset for Hasher
§fn finalize_into_reset(
&mut self,
out: &mut GenericArray<u8, <Hasher as OutputSizeUser>::OutputSize>,
)
fn finalize_into_reset( &mut self, out: &mut GenericArray<u8, <Hasher as OutputSizeUser>::OutputSize>, )
§fn finalize_fixed_reset(&mut self) -> GenericArray<u8, Self::OutputSize>
fn finalize_fixed_reset(&mut self) -> GenericArray<u8, Self::OutputSize>
§impl HasherExt for Hasher
impl HasherExt for Hasher
§fn new_from_context_key(context_key: &[u8; 32]) -> Hasher ⓘ
fn new_from_context_key(context_key: &[u8; 32]) -> Hasher ⓘ
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 ⓘ
fn set_input_offset(&mut self, offset: u64) -> &mut Hasher ⓘ
Hasher to process a chunk or subtree starting at offset bytes into the
whole input. Read more§fn finalize_non_root(&self) -> [u8; 32]
fn finalize_non_root(&self) -> [u8; 32]
§impl KeyInit for Hasher
impl KeyInit for Hasher
§impl KeySizeUser for Hasher
impl KeySizeUser for Hasher
§impl OutputSizeUser for Hasher
impl OutputSizeUser for Hasher
§impl Write for Hasher
Available on crate feature std only.
impl Write for Hasher
std only.§fn flush(&mut self) -> Result<(), Error>
fn flush(&mut self) -> Result<(), Error>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored)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§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Conv for T
impl<T> Conv for T
§impl<D> Digest for D
impl<D> Digest for D
§fn new_with_prefix(data: impl AsRef<[u8]>) -> Dwhere
D: Default,
fn new_with_prefix(data: impl AsRef<[u8]>) -> Dwhere
D: Default,
§fn chain_update(self, data: impl AsRef<[u8]>) -> D
fn chain_update(self, data: impl AsRef<[u8]>) -> D
§fn finalize(self) -> GenericArray<u8, <D as OutputSizeUser>::OutputSize>
fn finalize(self) -> GenericArray<u8, <D as OutputSizeUser>::OutputSize>
§fn finalize_into(
self,
out: &mut GenericArray<u8, <D as OutputSizeUser>::OutputSize>,
)
fn finalize_into( self, out: &mut GenericArray<u8, <D as OutputSizeUser>::OutputSize>, )
§fn finalize_reset(
&mut self,
) -> GenericArray<u8, <D as OutputSizeUser>::OutputSize>where
D: FixedOutputReset,
fn finalize_reset(
&mut self,
) -> GenericArray<u8, <D as OutputSizeUser>::OutputSize>where
D: FixedOutputReset,
§fn finalize_into_reset(
&mut self,
out: &mut GenericArray<u8, <D as OutputSizeUser>::OutputSize>,
)where
D: FixedOutputReset,
fn finalize_into_reset(
&mut self,
out: &mut GenericArray<u8, <D as OutputSizeUser>::OutputSize>,
)where
D: FixedOutputReset,
§fn output_size() -> usize
fn output_size() -> usize
§fn digest(
data: impl AsRef<[u8]>,
) -> GenericArray<u8, <D as OutputSizeUser>::OutputSize>
fn digest( data: impl AsRef<[u8]>, ) -> GenericArray<u8, <D as OutputSizeUser>::OutputSize>
data.§impl<D> DynDigest for D
impl<D> DynDigest for D
§fn finalize_reset(&mut self) -> Box<[u8]>
fn finalize_reset(&mut self) -> Box<[u8]>
§fn finalize_into(self, buf: &mut [u8]) -> Result<(), InvalidBufferSize>
fn finalize_into(self, buf: &mut [u8]) -> Result<(), InvalidBufferSize>
§fn finalize_into_reset(
&mut self,
buf: &mut [u8],
) -> Result<(), InvalidBufferSize>
fn finalize_into_reset( &mut self, buf: &mut [u8], ) -> Result<(), InvalidBufferSize>
§fn output_size(&self) -> usize
fn output_size(&self) -> usize
§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 Twhere
T: Update + FixedOutput + MacMarker,
impl<T> Mac for Twhere
T: Update + FixedOutput + MacMarker,
§fn new(key: &GenericArray<u8, <T as KeySizeUser>::KeySize>) -> Twhere
T: KeyInit,
fn new(key: &GenericArray<u8, <T as KeySizeUser>::KeySize>) -> Twhere
T: KeyInit,
§fn new_from_slice(key: &[u8]) -> Result<T, InvalidLength>where
T: KeyInit,
fn new_from_slice(key: &[u8]) -> Result<T, InvalidLength>where
T: KeyInit,
§fn chain_update(self, data: impl AsRef<[u8]>) -> T
fn chain_update(self, data: impl AsRef<[u8]>) -> T
§fn finalize(self) -> CtOutput<T>
fn finalize(self) -> CtOutput<T>
Mac] computation as a [CtOutput] and consume
[Mac] instance.§fn finalize_reset(&mut self) -> CtOutput<T>where
T: FixedOutputReset,
fn finalize_reset(&mut self) -> CtOutput<T>where
T: FixedOutputReset,
Mac] computation as a [CtOutput] and reset
[Mac] instance.§fn verify(
self,
tag: &GenericArray<u8, <T as OutputSizeUser>::OutputSize>,
) -> Result<(), MacError>
fn verify( self, tag: &GenericArray<u8, <T as OutputSizeUser>::OutputSize>, ) -> Result<(), MacError>
§fn verify_reset(
&mut self,
tag: &GenericArray<u8, <T as OutputSizeUser>::OutputSize>,
) -> Result<(), MacError>where
T: FixedOutputReset,
fn verify_reset(
&mut self,
tag: &GenericArray<u8, <T as OutputSizeUser>::OutputSize>,
) -> Result<(), MacError>where
T: FixedOutputReset,
Mac] instance.§fn verify_slice(self, tag: &[u8]) -> Result<(), MacError>
fn verify_slice(self, tag: &[u8]) -> Result<(), MacError>
§fn verify_slice_reset(&mut self, tag: &[u8]) -> Result<(), MacError>where
T: FixedOutputReset,
fn verify_slice_reset(&mut self, tag: &[u8]) -> Result<(), MacError>where
T: FixedOutputReset,
Mac] instance. Read more§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.