HashFunction

Trait HashFunction 

Source
pub trait HashFunction {
    // Required methods
    fn new() -> Self;
    fn update(&mut self, data: &[u8]) -> Result<(), HashError>;
    fn finalize(self) -> Result<Digest, HashError>;
    fn hash(data: &[u8]) -> Result<Digest, HashError>;
}
Expand description

Cryptographic hash function trait.

§Examples

use qudag_crypto::hash::{HashFunction, HashError};

// Implement a simple hash function for demonstration
struct SimpleHash {
    state: Vec<u8>,
}

impl HashFunction for SimpleHash {
    fn new() -> Self {
        Self { state: Vec::new() }
    }
     
    fn update(&mut self, data: &[u8]) -> Result<(), HashError> {
        self.state.extend_from_slice(data);
        Ok(())
    }
     
    fn finalize(self) -> Result<qudag_crypto::hash::Digest, HashError> {
        // Simple checksum for example
        let sum = self.state.iter().fold(0u8, |acc, &x| acc.wrapping_add(x));
        Ok(qudag_crypto::hash::Digest(vec![sum]))
    }
     
    fn hash(data: &[u8]) -> Result<qudag_crypto::hash::Digest, HashError> {
        let mut hasher = Self::new();
        hasher.update(data)?;
        hasher.finalize()
    }
}

// Use the hash function
let data = b"hello world";
let digest = SimpleHash::hash(data).unwrap();

Required Methods§

Source

fn new() -> Self

Create a new hash instance.

§Examples
let mut hasher = Blake3Hash::new();
Source

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

Update the hash state with input data.

§Examples
let mut hasher = Blake3Hash::new();
hasher.update(b"hello").unwrap();
hasher.update(b" world").unwrap();
Source

fn finalize(self) -> Result<Digest, HashError>

Finalize the hash computation and return the digest.

§Examples
let mut hasher = Blake3Hash::new();
hasher.update(b"hello world").unwrap();
let digest = hasher.finalize().unwrap();
Source

fn hash(data: &[u8]) -> Result<Digest, HashError>

Compute hash of input data in one step.

§Examples
let digest = Blake3Hash::hash(b"hello world").unwrap();

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§