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§
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.