Skip to main content

HasherService

HasherService defines the API to calculate hashes of files, chunks, streams, and buffers. It is abstract; real implementations are in the infrastructure layer (e.g., Blake3HasherService in Rust).

This page documents the public API available in the Rust SDK.

pub trait HasherService: Send + Sync {
...
}

Methods

MethodReturnsDescription
hash_file(file_path, root_dir, chunk_size)Result<FileEntry, HasherError>Returns a FileEntry calculating the file hash and chunk hashes.
hash_stream(stream, on_chunk)Result<Vec<Chunk>, HasherError>Processes a stream of chunks and returns an array of hashed chunks.
hash_buffer(data)Result<String, HasherError>Returns the hash of a buffer (hex string).
verify_chunk(data, expected_hash)Result<bool, HasherError>Verifies that a chunk has the expected hash.
verify_file(path, expected_hash)Result<bool, HasherError>Verifies that a file has the expected hash.
create_streaming_hasher()Box<dyn StreamingHasher + Send>Creates a StreamingHasher object for incremental hashing.

HasherError

Custom error enum for results of HasherService. (Uses thiserror)

pub enum HasherError {
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),

#[error("Invalid argument: {0}")]
InvalidArgument(String),

#[error("Hashing failed: {0}")]
Hash(String),

#[error("Unexpected error: {0}")]
Other(String),
}

Method Details

hash_file(file_path, root_dir, chunk_size)

Returns a FileEntry of the given file, calculating its hash and chunk hashes.

IMPORTANT NOTE: selected chunk_size must be the same in all operations of rac-delta

Parameters:

NameTypeDescription
file_path&strRelative path of the file (dir/file.txt)
root_dir&strRoot directory of the index (dir)
chunk_sizeu64Size in bytes of each chunk (recommended 1 MB)

Returns: Result<FileEntry, HasherError>


hash_stream(stream, on_chunk)

Processes a stream of chunks, optionally calling on_chunk for each processed chunk.

Parameters:

NameTypeDescription
stream&mut (dyn AsyncChunkStream + Send)The input chunk stream.
on_chunkOption<Box<dyn Fn(Vec<u8>) + Send + Sync>>Optional callback for each hashed chunk.
pub trait AsyncChunkStream: Send + Sync {
async fn next_chunk(&mut self) -> Option<Vec<u8>>;
async fn reset(&mut self) {}
async fn close(&mut self) {}
}

Returns: Result<Vec<Chunk>, HasherError>


hash_buffer(data)

Hashes a buffer.

ParameterTypeDescription
data&[u8]The buffer to hash.

Returns: Result<String, HasherError> (hex string)


verify_chunk(data, expected_hash)

Checks if a chunk has the expected hash.

ParameterTypeDescription
data&[u8]Chunk data buffer.
expected_hash&strExpected hash.

Returns: Result<bool, HasherError>


verify_file(path, expected_hash)

Checks if a file has the expected hash.

ParameterTypeDescription
path&strFile path.
expected_hash&strExpected hash.

Returns: Result<bool, HasherError>


create_streaming_hasher()

Creates a StreamingHasher instance:

pub trait StreamingHasher: Send {
fn update(&mut self, data: &[u8]);
fn digest(&self) -> String;
}

Returns: Box<dyn StreamingHasher + Send>