[−][src]Module orion::hazardous::hash::blake2b
BLAKE2b as specified in the RFC 7693.
Parameters:
secret_key
: An optional secret key.size
: The desired output length for the digest.data
: The data to be hashed.expected
: The expected digest when verifying.
Errors:
An error will be returned if:
size
is 0 or greater than 64.finalize()
is called twice without areset()
in between.update()
is called afterfinalize()
without areset()
in between.reset()
is called withSome(secret_key)
but the struct was initialized withNone
.reset()
is called withNone
assecret_key
but the struct was initialized withSome(secret_key)
.
Panics:
A panic will occur if:
- More than 2*(2^64-1) bytes of data are hashed.
Security:
- The secret key should always be generated using a CSPRNG.
SecretKey::generate()
can be used for this. It generates a secret key of 32 bytes. - The minimum recommended size for a secret key is 32 bytes.
- When using Blake2b with a secret key, then the output can be used as a
MAC. If this is the intention, avoid using
as_ref()
to compare such MACs and use insteadverify()
, which will compare the MAC in constant time. - The recommended minimum output size is 32.
Example:
use orion::hazardous::hash::blake2b::{Blake2b, Hasher, SecretKey}; // Using the streaming interface without a key. let mut state = Blake2b::new(None, 64)?; state.update(b"Some data")?; let digest = state.finalize()?; // Using the streaming interface with a key. let secret_key = SecretKey::generate(); let mut state_keyed = Blake2b::new(Some(&secret_key), 64)?; state_keyed.update(b"Some data")?; let mac = state_keyed.finalize()?; assert!(Blake2b::verify(&mac, &secret_key, 64, b"Some data").is_ok()); // Using the `Hasher` for convenience functions. let digest = Hasher::Blake2b512.digest(b"Some data")?;
Structs
Blake2b | BLAKE2b streaming state. |
Digest | A type to represent the |
SecretKey | A type to represent the secret key that BLAKE2b uses for keyed mode. |
Enums
Hasher | Convenience functions for common BLAKE2b operations. |