pub struct ClockHasher { /* private fields */ }Expand description
Incremental hasher for ClockHash-256
ClockHasher provides a streaming interface for computing ClockHash-256 hashes
incrementally. This is useful when the input data is too large to fit in memory
at once, or when data arrives in chunks (e.g., network streams, file reading).
The hasher maintains internal state and buffers partial blocks until enough data is available for processing. It automatically handles padding and finalization.
§Examples
Basic incremental hashing:
use clock_hash::ClockHasher;
let mut hasher = ClockHasher::new();
hasher.update(b"Hello");
hasher.update(b", ");
hasher.update(b"World!");
let hash = hasher.finalize();
assert_eq!(hash.len(), 32);Incremental hashing produces the same result as one-shot hashing:
let data = b"The quick brown fox jumps over the lazy dog";
// Incremental
let mut hasher = ClockHasher::new();
hasher.update(data);
let incremental_hash = hasher.finalize();
// One-shot
let oneshot_hash = clockhash256(data);
assert_eq!(incremental_hash, oneshot_hash);Hashing large files or streams:
let mut hasher = ClockHasher::new();
let mut file = File::open("large_file.dat")?;
let mut buffer = [0u8; 8192]; // 8KB chunks
while let Ok(n) = file.read(&mut buffer) {
if n == 0 { break; }
hasher.update(&buffer[..n]);
}
let file_hash = hasher.finalize();Implementations§
Source§impl ClockHasher
impl ClockHasher
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new ClockHasher with initial state from IV.
The hasher is initialized with the ClockHash-256 initialization vector
and is ready to accept data via update().
§Examples
use clock_hash::ClockHasher;
let hasher = ClockHasher::new();
// Now you can call hasher.update(data) and hasher.finalize()Sourcepub fn update(&mut self, data: &[u8])
pub fn update(&mut self, data: &[u8])
Update the hasher with new data.
This method can be called multiple times to feed data incrementally. The hasher will buffer partial blocks and process complete 128-byte blocks as they become available.
§Arguments
data- The data to hash (can be any length, including empty slices)
§Examples
Feeding data in chunks:
let mut hasher = ClockHasher::new();
// Feed data in multiple calls
hasher.update(b"chunk 1");
hasher.update(b"chunk 2");
hasher.update(b"chunk 3");
let hash = hasher.finalize();Empty updates are allowed:
let mut hasher = ClockHasher::new();
hasher.update(b""); // Empty update - no effect
hasher.update(b"data");
let hash = hasher.finalize();Sourcepub fn finalize(self) -> [u8; 32]
pub fn finalize(self) -> [u8; 32]
Finalize the hash and return the result.
This method applies the final padding scheme, processes any remaining
buffered data, and returns the 32-byte ClockHash-256 hash. After calling
finalize(), the hasher is consumed and cannot be used again.
The padding scheme used is: message || 0x80 || zeros || length_bits
where length_bits is the total message length in bits as a 64-bit
little-endian value.
§Returns
A 32-byte array containing the final ClockHash-256 hash
§Examples
Basic finalization:
let mut hasher = ClockHasher::new();
hasher.update(b"test data");
let hash = hasher.finalize();
assert_eq!(hash.len(), 32);Empty hash:
let hasher = ClockHasher::new();
let empty_hash = hasher.finalize(); // No data added
// Still produces a valid hash
assert_eq!(empty_hash.len(), 32);§Note
After calling finalize(), the hasher cannot be reused.
Create a new ClockHasher instance for additional hashes.