ClockHasher

Struct ClockHasher 

Source
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

Source

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()
Source

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();
Source

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.

Trait Implementations§

Source§

impl Default for ClockHasher

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.