macula_content_hasher (macula v0.20.5)

View Source

Content hashing module for Macula content-addressed storage.

Provides cryptographic hashing using BLAKE3 (primary) and SHA256 (fallback). BLAKE3 uses a pure Erlang implementation that can be optimized with NIFs from macula-nifs/ for production use.

Example Usage

   %% Hash binary data
   Hash = macula_content_hasher:hash(blake3, Data).
  
   %% Verify hash
   true = macula_content_hasher:verify(blake3, Data, Hash).
  
   %% Stream hash multiple chunks
   Hash = macula_content_hasher:hash_streaming(sha256, [Chunk1, Chunk2]).

Summary

Functions

Hash binary data using the specified algorithm.

Return hash output size in bytes for an algorithm.

Hash a list of binary chunks using streaming (memory efficient).

Decode hex string to binary.

Encode binary to lowercase hex string.

Check if an algorithm is supported.

Return list of supported hash algorithms.

Verify that data matches the expected hash.

Types

algorithm/0

-type algorithm() :: blake3 | sha256.

hash/0

-type hash() :: <<_:256>>.

32 bytes

Functions

hash(_, Data)

-spec hash(algorithm(), binary()) -> hash().

Hash binary data using the specified algorithm.

hash_size(_)

-spec hash_size(algorithm()) -> pos_integer().

Return hash output size in bytes for an algorithm.

hash_streaming(_, Chunks)

-spec hash_streaming(algorithm(), [binary()]) -> hash().

Hash a list of binary chunks using streaming (memory efficient).

hex_decode(Hex)

-spec hex_decode(binary()) -> {ok, binary()} | {error, invalid_hex}.

Decode hex string to binary.

hex_encode(Bin)

-spec hex_encode(binary()) -> binary().

Encode binary to lowercase hex string.

is_supported(_)

-spec is_supported(atom()) -> boolean().

Check if an algorithm is supported.

supported_algorithms()

-spec supported_algorithms() -> [algorithm()].

Return list of supported hash algorithms.

verify(Algorithm, Data, ExpectedHash)

-spec verify(algorithm(), binary(), hash()) -> boolean().

Verify that data matches the expected hash.