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::engine::general_purpose;
use base64::Engine as _;
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!(
    general_purpose::STANDARD.encode(output),
    "GdztjR9nU/rLh8VJt8e74+/seKTUnHgBexhGSpxLau0="
);

Classic API example, incremental interface

use base64::engine::general_purpose;
use base64::Engine as _;
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!(
    general_purpose::STANDARD.encode(output),
    "GdztjR9nU/rLh8VJt8e74+/seKTUnHgBexhGSpxLau0="
);

Structs

Functions