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
//! SHA-512 hasher implementation for content addressing.
//!
//! # Why this module exists
//!
//! The [`libvctrl_handler`] crate defines the [`Hasher`](libvctrl_handler::Hasher)
//! trait as the abstraction for content-addressable object hashing. This module
//! provides a concrete implementation using the SHA-512 algorithm from the
//! [`libvctrl_sha512`] crate. It bridges the raw SHA-512 digest computation to
//! the handler's [`Hash`] type, ensuring that all hashes produced by this
//! crate are compatible with the rest of the VCS ecosystem.
//!
//! # How it works
//!
//! The [`Sha512Hasher`] is a zero-sized struct. It holds no state because
//! hashing is stateless across invocations. The [`hash`](Sha512Hasher::hash)
//! method reads from a generic [`Read`](std::io::Read) stream in fixed-size
//! chunks, feeds each chunk into the underlying [`Sha512Hash`] engine, and
//! finalizes the digest into a 64-byte [`Hash`]. The result length always
//! matches [`HASH_LENGTH`](libvctrl_handler::HASH_LENGTH), so conversion
//! cannot fail.
//!
//! # Examples
//!
//! Hash a byte slice:
//!
//! ```
//! use libvctrl_core::hash::Sha512Hasher;
//! use libvctrl_handler::Hasher;
//!
//! let hasher = Sha512Hasher;
//! let hash = hasher.hash(b"hello world".as_ref()).unwrap();
//! assert_eq!(hash.as_bytes().len(), 64);
//! ```
use ;
use Hash as Sha512Hash;
/// A hasher that uses the SHA-512 algorithm.
///
/// # Design rationale
///
/// This is a zero-sized struct (ZST) because the SHA-512 algorithm does not
/// require any persistent state between calls. Each call to
/// [`hash`](Sha512Hasher::hash) creates a fresh [`Sha512Hash`] engine,
/// processes the input, and drops it. This makes the hasher trivially
/// [`Clone`], [`Default`], and [`Debug`], and allows it to be passed by value
/// without overhead.
///
/// The struct name follows the convention of naming the concrete implementation
/// after the algorithm it uses, making it obvious to users what cryptographic
/// function will be applied.
///
/// # Examples
///
/// Create a hasher instance:
///
/// ```
/// # use libvctrl_core::hash::Sha512Hasher;
/// let hasher = Sha512Hasher::default();
/// // The hasher is stateless and can be reused for multiple inputs.
/// ```
;