Skip to main content

Module hash

Module hash 

Source
Expand description

Hash utilities for Git objects with selectable algorithms (SHA-1, SHA-256 and BLAKE3-256).

§Algorithms

  • Sha1 (20 bytes / 40 hex) and Sha256 (32 bytes / 64 hex) follow the standard Git object formats.
  • Blake3 (32 bytes / 64 hex) is a git-internal / Libra extension: a repository created with it is a separate object namespace that standard Git does not understand. Because SHA-256 and BLAKE3 IDs have the same width, a 64-hex or 32-byte value can only be interpreted inside a known repository HashKind context (see the explicit-kind API below) or when it carries its algorithm tag (blake3:HEX); nothing in this module guesses BLAKE3 from a length.
  • BLAKE3 here is the repository object ID algorithm only. It does not change crate::internal::object::integrity::IntegrityHash (always SHA-256) or any application-level digest (Buck manifests, policy digests, HMACs).

§Thread-local default versus explicit HashKind

Two families of API live here:

Raw hex IDs (Display, FromStr) stay Git-like: no algorithm prefix. The legacy FromStr implementation infers SHA-1 for 40 hex characters and SHA-256 for 64 hex characters purely from the length; it does not consult the thread-local kind and must not be used where the repository format is known. When an ID has to travel across repositories, APIs, logs or indexes together with its algorithm, use the tagged representation (sha1:HEX / sha256:HEX / blake3:HEX) via ObjectHash::to_tagged_string and ObjectHash::from_tagged_str.

Every explicit-kind parser fails closed: a length, hex or kind mismatch is reported through HashError (carrying the operation, the expected/actual lengths and — whenever it can be determined — the requested kind) and never silently falls back to another algorithm.

Hash kind is stored thread-locally; set once at startup to match your repository format. Defaults to SHA-1.

Structs§

HashKindGuard
A guard to reset the hash kind after the test

Enums§

ArchivedHashKind
An archived HashKind
ArchivedObjectHash
An archived ObjectHash
HashError
Errors produced by the explicit-kind ObjectHash constructors and parsers.
HashKind
Supported hash algorithms for object IDs (selector only, no data attached). Used to configure which hash algorithm to use globally (thread-local). Defaults to SHA-1.
HashKindResolver
The resolver for an archived HashKind
ObjectHash
Concrete object ID value carrying the bytes for the selected algorithm (SHA-1, SHA-256 or BLAKE3-256). Used for Git object hashes. Supports conversion to/from hex strings, byte slices, and stream reading.
ObjectHashResolver
The resolver for an archived ObjectHash

Functions§

get_hash_kind
Retrieves the hash kind for the current thread.
set_hash_kind
Set the thread-local hash kind (configure once at startup to match repo format).
set_hash_kind_for_test
Sets the hash kind for the current thread and returns a guard to reset it later.