pub fn compute_to_slice<T: AsRef<[u8]>>(
digest_out: &mut [u8],
info: Option<&str>,
message: T,
)Expand description
Convenience function for “one-shot” SpongeHash-AES256 computation
The hash value (digest) of the given message is written into the slice digest_out.
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 output slice 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, i.e., digest_out.len(), in bytes, must be a positive value! 🚨
§Usage Example
The compute_to_slice() function can be used as follows:
use hex::encode_to_slice;
use sponge_hash_aes256::{DEFAULT_DIGEST_SIZE, compute_to_slice};
fn main() {
// Compute digest using the “one-shot” function
let mut digest = [0u8; DEFAULT_DIGEST_SIZE];
compute_to_slice(&mut digest, 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_to_slice};
fn main() {
// Compute digest using the “one-shot” function with additional “info”
let mut digest = [0u8; DEFAULT_DIGEST_SIZE];
compute_to_slice(
&mut digest,
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.