Struct dryoc::generichash::GenericHash
source · 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 hasher instance, with key
.
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.
sourcepub fn finalize_to_vec(self) -> Result<Vec<u8>, Error>
pub fn finalize_to_vec(self) -> Result<Vec<u8>, Error>
Computes and returns the final hash value as a Vec
. Provided for
convenience.
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>
Onet-time interface for the generic hash function. Computes the hash for
input
with optional key
. The output length is determined by the type
signature of Output
.
Example
use base64::engine::general_purpose;
use base64::Engine as _;
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="
);
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.