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.
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 or the length of key is outside
the range supported by libsodium’s generic hash function.
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<Input: Bytes + ?Sized, Key: ByteArray<KEY_LENGTH>, Output: NewByteArray<OUTPUT_LENGTH>>(
input: &Input,
key: Option<&Key>,
) -> Result<Output, Error>
pub fn hash<Input: Bytes + ?Sized, Key: ByteArray<KEY_LENGTH>, Output: NewByteArray<OUTPUT_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 or the length of key is outside
the range supported by libsodium’s generic hash function.
§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, Key: ByteArray<KEY_LENGTH>>(
input: &Input,
key: Option<&Key>,
) -> Result<Vec<u8>, Error>
pub fn hash_to_vec<Input: Bytes, Key: ByteArray<KEY_LENGTH>>( input: &Input, key: Option<&Key>, ) -> Result<Vec<u8>, Error>
Convenience wrapper for GenericHash::hash.
§Errors
Returns an error under the same conditions as GenericHash::hash.
Source§impl GenericHash<CRYPTO_GENERICHASH_KEYBYTES, CRYPTO_GENERICHASH_BYTES>
impl GenericHash<CRYPTO_GENERICHASH_KEYBYTES, CRYPTO_GENERICHASH_BYTES>
Sourcepub fn new_with_defaults<Key: ByteArray<CRYPTO_GENERICHASH_KEYBYTES>>(
key: Option<&Key>,
) -> Result<Self, Error>
pub fn new_with_defaults<Key: ByteArray<CRYPTO_GENERICHASH_KEYBYTES>>( key: Option<&Key>, ) -> Result<Self, Error>
Returns an instance of GenericHash with the default output and key
length parameters.
§Errors
The default lengths are valid, so this method does not return an error
for valid ByteArray implementations. Its return type matches the
generic initialization interface.