HashFunction

Trait HashFunction 

Source
pub trait HashFunction: Sized {
    type Algorithm: HashAlgorithm;
    type Output: AsRef<[u8]> + AsMut<[u8]> + Clone;

    // Required methods
    fn new() -> Self;
    fn update(&mut self, data: &[u8]) -> Result<&mut Self>;
    fn finalize(&mut self) -> Result<Self::Output>;

    // Provided methods
    fn finalize_reset(&mut self) -> Result<Self::Output> { ... }
    fn output_size() -> usize { ... }
    fn block_size() -> usize { ... }
    fn digest(data: &[u8]) -> Result<Self::Output> { ... }
    fn name() -> String { ... }
    fn verify(data: &[u8], expected: &Self::Output) -> Result<bool>
       where Self::Output: PartialEq { ... }
}
Expand description

Trait for cryptographic hash functions with improved type safety.

Example usage of the enhanced hash functions:

use dcrypt_algorithms::hash::{EnhancedSha256, HashFunction};

// One-shot API
let digest = EnhancedSha256::digest(b"hello world").unwrap();

// Incremental API with method chaining
let digest = EnhancedSha256::new()
    .update(b"hello ").unwrap()
    .update(b"world").unwrap()
    .finalize().unwrap();

// Verification
assert!(EnhancedSha256::verify(b"hello world", &digest).unwrap());

Required Associated Types§

Source

type Algorithm: HashAlgorithm

The algorithm type that defines constants and properties

Source

type Output: AsRef<[u8]> + AsMut<[u8]> + Clone

The output digest type with size guarantees

Required Methods§

Source

fn new() -> Self

Creates a new instance of the hash function.

Source

fn update(&mut self, data: &[u8]) -> Result<&mut Self>

Updates the hash state with data, returning self for chaining.

Source

fn finalize(&mut self) -> Result<Self::Output>

Finalizes and returns the digest.

Provided Methods§

Source

fn finalize_reset(&mut self) -> Result<Self::Output>

Finalizes, returns the digest, and resets state.

Source

fn output_size() -> usize

The output size in bytes.

Source

fn block_size() -> usize

The internal block size in bytes.

Source

fn digest(data: &[u8]) -> Result<Self::Output>

Convenience: one-shot digest computation with fluent interface.

Source

fn name() -> String

Human-readable name.

Source

fn verify(data: &[u8], expected: &Self::Output) -> Result<bool>
where Self::Output: PartialEq,

Convenience method to verify a hash against input data

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl HashFunction for Blake2b

Source§

type Algorithm = Blake2bAlgorithm

Source§

type Output = Digest<BLAKE2B_MAX_OUTPUT_SIZE>

Source§

impl HashFunction for Blake2s

Source§

type Algorithm = Blake2sAlgorithm

Source§

type Output = Digest<BLAKE2S_MAX_OUTPUT_SIZE>

Source§

impl HashFunction for Sha1

Source§

type Algorithm = Sha1Algorithm

Source§

type Output = Digest<SHA1_OUTPUT_SIZE>

Source§

impl HashFunction for Sha224

Source§

type Algorithm = Sha224Algorithm

Source§

type Output = Digest<SHA224_OUTPUT_SIZE>

Source§

impl HashFunction for Sha256

Source§

type Algorithm = Sha256Algorithm

Source§

type Output = Digest<SHA256_OUTPUT_SIZE>

Source§

impl HashFunction for Sha384

Source§

type Algorithm = Sha384Algorithm

Source§

type Output = Digest<SHA384_OUTPUT_SIZE>

Source§

impl HashFunction for Sha512

Source§

type Algorithm = Sha512Algorithm

Source§

type Output = Digest<SHA512_OUTPUT_SIZE>

Source§

impl HashFunction for Sha512_224

Source§

impl HashFunction for Sha512_256

Source§

impl HashFunction for Sha3_224

Source§

type Algorithm = Sha3_224Algorithm

Source§

type Output = Digest<SHA3_224_OUTPUT_SIZE>

Source§

impl HashFunction for Sha3_256

Source§

type Algorithm = Sha3_256Algorithm

Source§

type Output = Digest<SHA3_256_OUTPUT_SIZE>

Source§

impl HashFunction for Sha3_384

Source§

type Algorithm = Sha3_384Algorithm

Source§

type Output = Digest<SHA3_384_OUTPUT_SIZE>

Source§

impl HashFunction for Sha3_512

Source§

type Algorithm = Sha3_512Algorithm

Source§

type Output = Digest<SHA3_512_OUTPUT_SIZE>

Source§

impl HashFunction for Shake128

Source§

type Algorithm = Shake128Algorithm

Source§

type Output = Digest<SHAKE128_OUTPUT_SIZE>

Source§

impl HashFunction for Shake256

Source§

type Algorithm = Shake256Algorithm

Source§

type Output = Digest<SHAKE256_OUTPUT_SIZE>

Source§

impl HashFunction for EnhancedSha256