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
//! SHA-512 hasher implementation for `libvctrl_core`.
//!
//! # Purpose
//!
//! This module provides the [`Sha512Hasher`], a concrete implementation of the
//! [`Hasher`](libvctrl_handler::Hasher) trait. It bridges the pure-Rust,
//! `#![no_std]`-compatible [`libvctrl_sha512`] crate with the core version
//! control contracts defined in [`libvctrl_handler`].
//!
//! # Design Rationale
//!
//! - **Stateless and zero-cost**: The [`Sha512Hasher`] is a unit struct
//! (zero-sized type). It requires no heap allocations or internal state to
//! be instantiated, making it extremely cheap to pass around or instantiate
//! repeatedly.
//! - **Audited cryptography**: By delegating the actual hashing to
//! [`libvctrl_sha512`], this module ensures that the version control system
//! relies on a carefully reviewed implementation of the SHA-512 algorithm,
//! minimizing the attack surface for collision or preimage attacks.
//! - **Deterministic addressing**: SHA-512 produces a 64-byte (512-bit)
//! digest, which perfectly matches the
//! [`HASH_LENGTH`](libvctrl_handler::HASH_LENGTH) constant defined in the
//! contracts. This provides a massive keyspace, making accidental hash
//! collisions practically impossible.
//!
//! # Why SHA-512?
//!
//! SHA-512 was chosen for its wide digest size and widespread availability in
//! cryptographic libraries. The 64-byte output aligns with the contract's
//! [`HASH_LENGTH`](libvctrl_handler::HASH_LENGTH), avoiding truncation or
//! padding. In a content-addressable system, a larger hash reduces collision
//! probability and increases resistance against birthday attacks, making it
//! suitable for repositories that may contain millions of objects.
//!
//! # Internal Mechanism
//!
//! The hasher calls [`Sha512Hash::hash`](libvctrl_sha512::Hash::hash) on the
//! input data, which returns a statically sized `[u8; 64]` array. This array
//! is then converted into the canonical
//! [`Hash`](libvctrl_handler::Hash) type using
//! [`Hash::from_bytes`](libvctrl_handler::Hash::from_bytes). The conversion
//! uses `.expect()` safely because the output length of SHA-512 is statically
//! guaranteed to be exactly 64 bytes by the algorithm's specification.
//!
//! # Error Handling
//!
//! Although the [`Hasher::hash`] method returns
//! [`Result<Hash, VctrlError>`](libvctrl_handler::VctrlError), this
//! particular implementation is infallible. The result is always `Ok` because
//! SHA-512 is a deterministic algorithm that cannot fail for arbitrary byte
//! slices. The `Result` is part of the trait contract, allowing other hashing
//! algorithms that may have failure modes (e.g., keyed hashing with missing
//! keys) to use the same interface.
//!
//! # Security Considerations
//!
//! - **No unsafe code**: This module contains no `unsafe` blocks and relies
//! only on safe Rust and the audited `libvctrl_sha512` crate.
//! - **Deterministic output**: The same input always produces the same hash,
//! which is essential for reproducible content addressing.
//! - **Resistance to length extension**: SHA-512's internal structure and wide
//! digest provide strong cryptographic properties suitable for version
//! control integrity checking.
//!
//! # Examples
//!
//! Hashing data and verifying the result length:
//!
//! ```
//! use libvctrl_handler::Hasher;
//! use libvctrl_core::hash::Sha512Hasher;
//!
//! let hasher = Sha512Hasher;
//! let hash = hasher.hash(b"hello world").unwrap();
//! assert_eq!(hash.as_bytes().len(), 64);
//! ```
use VctrlError;
use ;
use Hash as Sha512Hash;
/// A cryptographic hasher that implements the SHA-512 algorithm.
///
/// # Purpose
///
/// This struct adapts the [`libvctrl_sha512`] crate to the
/// [`Hasher`](libvctrl_handler::Hasher) trait, allowing it to be used
/// transparently by the version control system to generate content-addressable
/// object identifiers.
///
/// # Design Rationale
///
/// Because the underlying [`Sha512Hash`] uses static one-shot functions, this
/// adapter does not need to hold any state. It is a zero-sized type (ZST),
/// meaning it consumes no memory and can be copied freely.
///
/// The struct derives [`Clone`], [`Debug`], and [`Default`] to provide common
/// conveniences without adding any runtime overhead. The [`Default`] instance
/// is particularly useful when the hasher is used as a field in a larger
/// struct or as a default parameter in generic code.
///
/// # Thread Safety
///
/// `Sha512Hasher` is both [`Send`] and [`Sync`] because it contains no data.
/// It can be shared across threads without synchronization, making it ideal
/// for use in concurrent indexing or hashing tasks.
///
/// # Examples
///
/// Hashing a simple byte string:
///
/// ```
/// use libvctrl_handler::Hasher;
/// use libvctrl_core::hash::Sha512Hasher;
///
/// let hasher = Sha512Hasher;
/// let hash = hasher.hash(b"hello world").unwrap();
/// assert_eq!(hash.as_bytes().len(), 64);
/// ```
;