compute

Function compute 

Source
pub fn compute<const N: usize, T: AsRef<[u8]>>(
    info: Option<&str>,
    message: T,
) -> [u8; N]
Expand description

Convenience function for “one-shot” SpongeHash-AES256 computation

The hash value (digest) of the given message is returned as an new array of type [u8; N].

A message can be of any type that implements the AsRef<[u8]> trait, e.g., &[u8], &str or String.

Optionally, an additional info string may be specified.

The returned array is filled completely, generating a hash value (digest) of the appropriate size.

This function uses the default number of permutation rounds, as is given by DEFAULT_PERMUTE_ROUNDS.

Note: The digest output size N, in bytes, must be a positive value! 🚨

§Usage Example

The compute() function can be used as follows:

use hex::encode_to_slice;
use sponge_hash_aes256::{DEFAULT_DIGEST_SIZE, compute};

fn main() {
    // Compute the digest using the “one-shot” function
    let digest: [u8; DEFAULT_DIGEST_SIZE] = compute(
        None,
        b"The quick brown fox jumps over the lazy dog");

    // Encode to hex
    let mut hex_buffer = [0u8; 2usize * DEFAULT_DIGEST_SIZE];
    encode_to_slice(&digest, &mut hex_buffer).unwrap();

    // Print the digest (hex format)
    println!("0x{}", core::str::from_utf8(&hex_buffer).unwrap());
}

§Context information

Optionally, additional “context” information may be provided via the info parameter:

use sponge_hash_aes256::{DEFAULT_DIGEST_SIZE, compute};

fn main() {
    // Compute the digest using the “one-shot” function with additional “info”
    let digest: [u8; DEFAULT_DIGEST_SIZE] = compute(
        Some("my_application"),
        b"The quick brown fox jumps over the lazy dog");
    /* ... */
}

§Important note

Applications that need to process large messages are recommended to use the streaming API, which does not require all message data to be held in memory at once and which allows for an incremental hash computation.