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.

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>

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 is not 1 to 64 bytes or key is longer than 64 bytes.

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>

Available on crate feature alloc only.

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

pub 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.

Convenience wrapper for GenericHash::hash.

§Errors

Returns an error under the same conditions as GenericHash::hash.

Trait Implementations§

Source§

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

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.