pub struct GenericHash<const KEY_LENGTH: usize, const OUTPUT_LENGTH: usize> { /* private fields */ }Expand description
Provides a generic hash function implementation based on Blake2b. Compatible with libsodium’s generic hash.
Like libsodium, OUTPUT_LENGTH may be 1 to 64 bytes and KEY_LENGTH 0 to
64 bytes. CRYPTO_GENERICHASH_BYTES_MIN and
CRYPTO_GENERICHASH_KEYBYTES_MIN (16 bytes) are recommended minimums,
not enforced ones, and an empty key is the same as no key.
Cloning a hasher copies its in-progress state, so a common prefix can be hashed once and finished with different suffixes. Each copy is wiped when dropped.
Implementations§
Source§impl<const KEY_LENGTH: usize, const OUTPUT_LENGTH: usize> GenericHash<KEY_LENGTH, OUTPUT_LENGTH>
impl<const KEY_LENGTH: usize, const OUTPUT_LENGTH: usize> GenericHash<KEY_LENGTH, OUTPUT_LENGTH>
Sourcepub fn new<Key: ByteArray<KEY_LENGTH>>(key: Option<&Key>) -> Result<Self, Error>
pub fn new<Key: ByteArray<KEY_LENGTH>>(key: Option<&Key>) -> Result<Self, Error>
Returns a new incremental hasher with an optional secret key.
§Errors
Returns an error if OUTPUT_LENGTH is not 1 to 64 bytes or key is
longer than 64 bytes.
Sourcepub fn update<Input: Bytes + ?Sized>(&mut self, input: &Input)
pub fn update<Input: Bytes + ?Sized>(&mut self, input: &Input)
Updates the hasher state from input.
Sourcepub fn finalize<Output: NewByteArray<OUTPUT_LENGTH>>(
self,
) -> Result<Output, Error>
pub fn finalize<Output: NewByteArray<OUTPUT_LENGTH>>( self, ) -> Result<Output, Error>
Computes and returns the final hash value.
§Errors
Returns an error if the underlying BLAKE2b finalization rejects the output. Initialization normally guarantees a valid output length.
Sourcepub fn hash<Output: NewByteArray<OUTPUT_LENGTH>, Input: Bytes + ?Sized, Key: ByteArray<KEY_LENGTH>>(
input: &Input,
key: Option<&Key>,
) -> Result<Output, Error>
pub fn hash<Output: NewByteArray<OUTPUT_LENGTH>, Input: Bytes + ?Sized, Key: ByteArray<KEY_LENGTH>>( input: &Input, key: Option<&Key>, ) -> Result<Output, Error>
Computes the hash of input with an optional secret key.
The output length is determined by Output. Providing a key selects
keyed BLAKE2b, which can be used as a MAC or PRF.
§Errors
Returns an error if OUTPUT_LENGTH is not 1 to 64 bytes or key is
longer than 64 bytes.
§Example
use base64::Engine as _;
use base64::engine::general_purpose;
use dryoc::generichash::{GenericHash, Hash};
let output: Hash =
GenericHash::hash(b"hello", Some(b"a very secret key")).expect("hash failed");
assert_eq!(
general_purpose::STANDARD.encode(&output),
"AECDe+XJsB6nOkbCsbS/OPXdzpcRm3AolW/Bg1LFY9A="
);Sourcepub fn hash_to_vec<Input: Bytes + ?Sized, Key: ByteArray<KEY_LENGTH>>(
input: &Input,
key: Option<&Key>,
) -> Result<Vec<u8>, Error>
Available on crate feature alloc only.
pub fn hash_to_vec<Input: Bytes + ?Sized, Key: ByteArray<KEY_LENGTH>>( input: &Input, key: Option<&Key>, ) -> Result<Vec<u8>, Error>
alloc only.Convenience wrapper for GenericHash::hash.
§Errors
Returns an error under the same conditions as GenericHash::hash.