1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//! Cryptographic hashing to produce content addresses.
//!
//! # Purpose
//!
//! This module defines the [`Hasher`] trait, which abstracts the
//! content-addressing algorithm used to identify version control objects.
//! Content addressing is the cornerstone of the object model: every object
//! is identified by the cryptographic hash of its serialized bytes, ensuring
//! integrity and deduplication.
//!
//! # Design Rationale
//!
//! Hashing is separated into a trait for several reasons:
//!
//! - **Algorithm agility**: Different deployments may prefer SHA-256,
//! SHA-512, BLAKE3, or other digest functions. The trait allows swapping
//! the algorithm without touching storage or object logic.
//! - **Testability**: Dummy or deterministic hashers can be injected in unit
//! tests, avoiding the need for actual cryptographic operations.
//! - **Decoupling**: The core data types remain independent of any specific
//! hash implementation. Only the trait contract matters.
//!
//! # Why `Result`?
//!
//! Although most cryptographic hash functions are infallible for arbitrary
//! byte slices, the `hash` method returns [`Result<Hash, VctrlError>`].
//! This design accounts for:
//!
//! - Hardware or library failures in exotic backends.
//! - Keyed hashing algorithms that may fail if no key is configured.
//! - Future extensions where hashing may involve fallible resources.
//!
//! The error type is [`VctrlError`](crate::VctrlError), preserving a unified
//! error surface across the crate.
//!
//! # Internal Mechanism
//!
//! A typical implementation receives a byte slice, feeds it to the selected
//! hash function, and then wraps the resulting fixed-size digest in a
//! [`Hash`]. The [`Hash`] type enforces a constant length via
//! [`HASH_LENGTH`](crate::constants::HASH_LENGTH); the hasher is responsible
//! for producing exactly that many bytes. If the underlying algorithm
//! produces a digest of a different length, the implementation must either
//! truncate, extend, or return
//! [`VctrlError::InvalidHashLength`](crate::VctrlError::InvalidHashLength).
//!
//! # Examples
//!
//! A simple deterministic hasher that returns a constant hash:
//!
//! ```
//! use libvctrl_handler::{Hash, Hasher, VctrlError};
//!
//! struct ConstantHasher;
//!
//! impl Hasher for ConstantHasher {
//! fn hash(&self, _data: &[u8]) -> Result<Hash, VctrlError> {
//! Ok(Hash::from_bytes(&[0xAB; 64]).unwrap())
//! }
//! }
//!
//! let hasher = ConstantHasher;
//! let hash = hasher.hash(b"anything").unwrap();
//! assert_eq!(hash.as_bytes(), &[0xAB; 64]);
//! ```
use crateVctrlError;
use crateHash;
/// Defines the interface for hashing raw data into a [`Hash`].
///
/// # Purpose
///
/// A `Hasher` implements the specific content-addressing algorithm (e.g.,
/// SHA-256, BLAKE3) used to identify objects in the system. The output is
/// always a [`Hash`], which is a fixed-size byte array of
/// [`HASH_LENGTH`](crate::constants::HASH_LENGTH) bytes.
///
/// # Design Rationale
///
/// - The method takes `&self` rather than consuming the hasher, allowing a
/// single instance to be reused for multiple hashing operations.
/// - The method takes `&[u8]` rather than `Vec<u8>` to avoid unnecessary
/// allocation and to accept any byte source (files, network buffers,
/// already-serialized objects).
/// - The return type is [`Result<Hash, VctrlError>`] to accommodate
/// fallible hashing backends while maintaining a unified error surface.
///
/// # Why Not `Hash::from_bytes` Directly?
///
/// The [`Hash`] constructor validates length, but it does not compute a
/// digest. The `Hasher` trait is responsible for the actual cryptographic
/// operation. This separation allows the rest of the crate to depend only on
/// the contract, not on a concrete algorithm.
///
/// # How It Works Internally
///
/// An implementation receives raw bytes and returns a [`Hash`]. It must
/// guarantee that the produced hash has exactly
/// [`HASH_LENGTH`](crate::constants::HASH_LENGTH) bytes. Most implementations
/// will call [`Hash::from_bytes`] on the digest produced by the underlying
/// hash function. If the digest length is not exactly 64 bytes, the
/// implementation must handle the mismatch, typically by returning
/// [`VctrlError::InvalidHashLength`](crate::VctrlError::InvalidHashLength).
///
/// # Examples
///
/// A complete dummy hasher:
///
/// ```
/// use libvctrl_handler::{Hash, Hasher, VctrlError};
///
/// struct DummyHasher;
///
/// impl Hasher for DummyHasher {
/// fn hash(&self, _data: &[u8]) -> Result<Hash, VctrlError> {
/// Ok(Hash::from_bytes(&[0u8; 64]).unwrap())
/// }
/// }
///
/// let hasher = DummyHasher;
/// let hash = hasher.hash(b"hello").unwrap();
/// assert_eq!(hash.as_bytes().len(), 64);
/// ```