Module 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::Engine as _;
use base64::engine::general_purpose;
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 as _;
use base64::engine::general_purpose;
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§

GenericHashState
State struct for the generic hash algorithm, based on BLAKE2B.

Functions§

crypto_generichash
Computes a hash from input and key, copying the result into output.
crypto_generichash_final
Finalizes the hash computation, copying the result into output. The length of output should match outlen from the call to crypto_generichash_init.
crypto_generichash_init
Initializes the state for the generic hash function using outlen for the expected hash output length, and optional key, returning it upon success.
crypto_generichash_keygen
Generates a random hash key using the OS’s random number source.
crypto_generichash_update
Updates the internal hash state with input.