Skip to main content

GenericHash

Struct 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>

Source

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.

Source

pub fn update<Input: Bytes + ?Sized>(&mut self, input: &Input)

Updates the hasher state from input.

Source

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.

Source

pub fn finalize_to_vec(self) -> Result<Vec<u8>, Error>

Computes and returns the final hash value as a Vec. Provided for convenience.

§Errors

Returns an error if the underlying BLAKE2b finalization rejects the output. Initialization normally guarantees a valid output length.

Source

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="
);
Source

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>

Source

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.

Source

pub fn hash_with_defaults<Input: Bytes + ?Sized, Key: ByteArray<CRYPTO_GENERICHASH_KEYBYTES>, Output: NewByteArray<CRYPTO_GENERICHASH_BYTES>>( input: &Input, key: Option<&Key>, ) -> Result<Output, Error>

Hashes input using key, with the default length parameters. Provided for convenience.

§Errors

The default lengths are valid, so this method does not return an error for valid ByteArray implementations. Its return type matches the generic hashing interface.

Source

pub fn hash_with_defaults_to_vec<Input: Bytes + ?Sized, Key: ByteArray<CRYPTO_GENERICHASH_KEYBYTES>>( input: &Input, key: Option<&Key>, ) -> Result<Vec<u8>, Error>

Hashes input using key, with the default length parameters, returning a Vec. Provided for convenience.

§Errors

The default lengths are valid, so this method does not return an error for valid ByteArray implementations. Its return type matches the generic hashing interface.

Auto Trait Implementations§

§

impl<const KEY_LENGTH: usize, const OUTPUT_LENGTH: usize> Freeze for GenericHash<KEY_LENGTH, OUTPUT_LENGTH>

§

impl<const KEY_LENGTH: usize, const OUTPUT_LENGTH: usize> RefUnwindSafe for GenericHash<KEY_LENGTH, OUTPUT_LENGTH>

§

impl<const KEY_LENGTH: usize, const OUTPUT_LENGTH: usize> Send for GenericHash<KEY_LENGTH, OUTPUT_LENGTH>

§

impl<const KEY_LENGTH: usize, const OUTPUT_LENGTH: usize> Sync for GenericHash<KEY_LENGTH, OUTPUT_LENGTH>

§

impl<const KEY_LENGTH: usize, const OUTPUT_LENGTH: usize> Unpin for GenericHash<KEY_LENGTH, OUTPUT_LENGTH>

§

impl<const KEY_LENGTH: usize, const OUTPUT_LENGTH: usize> UnsafeUnpin for GenericHash<KEY_LENGTH, OUTPUT_LENGTH>

§

impl<const KEY_LENGTH: usize, const OUTPUT_LENGTH: usize> UnwindSafe for GenericHash<KEY_LENGTH, OUTPUT_LENGTH>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.