Trait HasherExt
pub trait HasherExt
Extension methods for Hasher. This is the main entrypoint to the hazmat module.
Required Methods
fn new_from_context_key(context_key: &ContextKey) -> SelfSimilar to
Hasher::new_derive_keybut using a pre-hashedContextKeyfromhash_derive_key_context.The
hash_derive_key_contextfunction is the only valid source of theContextKey. Any other source (hash,keyed_hash, arbitrary bytes from the caller) violates the security requirements.Calling
derive_keyorHasher::new_derive_keyin a loop will re-hash the context string every time. This constructor function is a performance optimization to avoid that repeated work. If you hardcode theContextKey, the derive-key mode becomes zero-overhead, like the keyed mode.Example
use Hasher; use HasherExt; let context_key = hash_derive_key_context; let mut hasher = new_from_context_key; hasher.update; let derived_key = *hasher.finalize.as_bytes; assert_eq!;fn set_input_offset(&mut self, offset: u64) -> &mut SelfConfigure the
Hasherto process a chunk or subtree starting atoffsetbytes into the whole input.You must call this function before processing any input with
updateor similar. This step isn't required for the first chunk, or for a subtree that includes the first chunk (i.e. when theoffsetis zero), but it's required for all other chunks and subtrees.The starting input offset of a subtree implies a maximum possible length for that subtree. See
max_subtree_lenand section 2.1 of the BLAKE3 paper. Note that only subtrees along the right edge of the whole tree can have a length less than their maximum possible length.See the module level examples.
Panics
This function panics if the
Hasherhas already accepted any input withupdateor similar.This should always be paired with
finalize_non_root. It's never correct to use a non-zero input offset withfinalizeorfinalize_xof. Theoffsetmust also be a multiple ofCHUNK_LEN. Violating either of these rules will currently fail an assertion and panic, but this is not guaranteed.fn finalize_non_root(&self) -> ChainingValueFinalize the non-root hash ("chaining value") of the current chunk or subtree.
Afterwards you can merge subtree chaining values into parent nodes using
merge_subtrees_non_rootand ultimately into the root node with eithermerge_subtrees_root(similar toHasher::finalize) ormerge_subtrees_root_xof(similar toHasher::finalize_xof).See the module level examples, particularly the discussion of valid tree structures.