Module dryoc::classic::crypto_generichash
source · Expand description
Generic hashing
Implements libsodium’s generic hashing functions, based on blake2b. Can also be used as an HMAC function, if a key is provided.
For details, refer to libsodium docs.
Classic API example, one-time interface
use base64::encode;
use dryoc::classic::crypto_generichash::*;
use dryoc::constants::CRYPTO_GENERICHASH_BYTES;
// Use the default hash length
let mut output = [0u8; CRYPTO_GENERICHASH_BYTES];
// Compute the hash using the one-time interface
crypto_generichash(&mut output, b"a string of bytes", None).ok();
assert_eq!(
encode(output),
"GdztjR9nU/rLh8VJt8e74+/seKTUnHgBexhGSpxLau0="
);
Classic API example, incremental interface
use base64::encode;
use dryoc::classic::crypto_generichash::*;
use dryoc::constants::CRYPTO_GENERICHASH_BYTES;
// Use the default hash length
let mut output = [0u8; CRYPTO_GENERICHASH_BYTES];
// Initialize the state for the incremental interface
let mut state = crypto_generichash_init(None, CRYPTO_GENERICHASH_BYTES).expect("state");
// Update the hash
crypto_generichash_update(&mut state, b"a string of bytes");
// Finalize, compute the hash and copy it into `output`
crypto_generichash_final(state, &mut output).expect("final failed");
assert_eq!(
encode(output),
"GdztjR9nU/rLh8VJt8e74+/seKTUnHgBexhGSpxLau0="
);
Structs
- State struct for the generic hash algorithm, based on BLAKE2B.
Functions
- Computes a hash from
input
andkey
, copying the result intooutput
. - Finalizes the hash computation, copying the result into
output
. The length ofoutput
should matchoutlen
from the call tocrypto_generichash_init
. - Initializes the state for the generic hash function using
outlen
for the expected hash output length, and optionalkey
, returning it upon success. - Generates a random hash key using the OS’s random number source.
- Updates the internal hash state with
input
.