Skip to content

merkle

merkle #

A tree hashes as the merkle of its entries; every kind of node hashes in its own domain.

call_key(identity, inputs) #

The key of identity applied to arguments hashing to inputs, in order.

Source code in capturegraph-lib/capturegraph/recipes/scratch/hashing/merkle.py
def call_key(identity: str, inputs: list[ContentHash]) -> CallKey:
    """The key of ``identity`` applied to arguments hashing to ``inputs``, in order."""
    hasher = blake3.blake3(derive_key_context=CALL_CONTEXT)
    name = identity.encode()
    hasher.update(_length(len(name)))
    hasher.update(name)
    hasher.update(_length(len(inputs)))
    for content in inputs:
        hasher.update(content.digest)
    return CallKey(ContentHash(hasher.digest()))

hash_bytes(data, context=LEAF_CONTEXT) #

The hash of data in context's domain.

Source code in capturegraph-lib/capturegraph/recipes/scratch/hashing/merkle.py
def hash_bytes(data: bytes, context: str = LEAF_CONTEXT) -> ContentHash:
    """The hash of ``data`` in ``context``'s domain."""
    return ContentHash(blake3.blake3(data, derive_key_context=context).digest())

hash_tree(path, cache=NO_CACHE) #

The merkle hash of the file or directory at path.

A file hashes its bytes; a directory hashes its entries in name order, each as its name, whether it is a directory, and its own hash. Hidden entries count like any other.

Raises:

Type Description
FileNotFoundError

If path does not exist.

Source code in capturegraph-lib/capturegraph/recipes/scratch/hashing/merkle.py
def hash_tree(path: Path | str, cache: HashCache = NO_CACHE) -> ContentHash:
    """The merkle hash of the file or directory at ``path``.

    A file hashes its bytes; a directory hashes its entries in name order, each
    as its name, whether it is a directory, and its own hash. Hidden entries
    count like any other.

    Raises:
        FileNotFoundError: If ``path`` does not exist.
    """
    path = Path(path)
    stat = Stat.of(path)
    return _hash_directory(path, cache) if stat.is_dir else _hash_file(path, stat, cache)