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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//! Cryptographic hash functions (SHA-3 and SHA-2 families).
//!
//! These are thin, one-shot wrappers over the already-audited [`sha3`] and
//! [`sha2`] RustCrypto crates. They expose no new or novel cryptography — they
//! simply make the digest primitives the rest of the crate already depends on
//! available as public API.
//!
//! ## Which one should I use?
//!
//! [`sha3_512`] is the recommended default for new code. It is the highest-assurance
//! option here (~256-bit collision resistance, NIST Category 5) and keeps callers
//! consistent with this crate's Keccak-based hybrid combiner. The other functions
//! are provided so integrators can pick a level that matches an existing format or
//! interoperability requirement.
//!
//! | Function | Algorithm | Output | Notes |
//! |--------------|------------|--------|------------------------------------|
//! | [`sha3_512`] | SHA3-512 | 64 B | Recommended default (Cat-5). |
//! | [`sha3_256`] | SHA3-256 | 32 B | Same family as the hybrid combiner.|
//! | [`sha256`] | SHA-256 | 32 B | SHA-2; for SHA-2 interop. |
//! | [`sha512`] | SHA-512 | 64 B | SHA-2; for SHA-2 interop. |
//!
//! ## Encoding
//!
//! The native functions take raw bytes (`&[u8]`) and return fixed-size byte
//! arrays. Encode the result yourself when needed, e.g. with [`crate::b64::encode`]
//! for base64. The WASM bindings (see [`crate::wasm`]) operate on base64 strings to
//! match the rest of the WASM API.
//!
//! ## Security note
//!
//! These digests are intended for **public** data only — specifically key
//! fingerprints / safety numbers and key-transparency-log entries, where both
//! the input (e.g. a public key) and the output digest are meant to be public.
//!
//! Because the input is public, the zeroize-on-drop and constant-time concerns
//! that apply to the encryption APIs in this crate do not apply here, and no
//! such ceremony is added. This is deliberate, not an oversight: a bare hash
//! makes **no** guarantees about its inputs, and wiping a transient copy of
//! already-public data would add cost without adding protection (the underlying
//! RustCrypto `sha2`/`sha3` primitives do not zeroize their internal state
//! either, and callers routinely hold the input elsewhere).
//!
//! **Do not feed secret material (passwords, private keys, shared secrets) to a
//! bare hash here.** If you need to process secrets, use the right construction
//! instead — this crate's Argon2id [`crate::kdf::derive_session_key`] for
//! password-based derivation, or a dedicated KDF/MAC. The encryption APIs that
//! *do* handle secrets already zeroize that material on drop.
use ;
use ;
/// Compute the SHA3-512 digest of `data`, returning 64 bytes.
///
/// This is the recommended default hash for new code (NIST Category 5,
/// ~256-bit collision resistance).
///
/// ```
/// use metamorphic_crypto::hash::sha3_512;
///
/// let digest = sha3_512(b"hello, metamorphic!");
/// assert_eq!(digest.len(), 64);
/// // The empty-input digest is a well-known SHA3-512 test vector.
/// assert_eq!(
/// &sha3_512(b"")[..4],
/// &[0xa6, 0x9f, 0x73, 0xcc]
/// );
/// ```
/// Compute a **domain-separated** SHA3-512 digest of `data` under `context`.
///
/// This binds the digest to a caller-chosen context label, so the same `data`
/// hashed under two different contexts yields unrelated digests. Prefer this
/// over bare [`sha3_512`] for fingerprints, safety numbers, and
/// key-transparency-log entries: it guarantees a digest computed for one
/// purpose can never be reinterpreted as a digest for another (cross-protocol /
/// cross-context collision resistance), and it makes the intent explicit at the
/// call site.
///
/// It is exactly as collision- and preimage-resistant as [`sha3_512`] — it *is*
/// SHA3-512, just over an unambiguously framed message.
///
/// ## Encoding (stable wire format — reproduce exactly for cross-language parity)
///
/// ```text
/// digest = SHA3-512( I2OSP(len(context), 8) || context_utf8 || data )
/// ```
///
/// where `I2OSP(len, 8)` is the byte length of `context` (its UTF-8 encoding)
/// as a **big-endian unsigned 64-bit integer**. The length prefix makes the
/// `(context, data)` boundary unambiguous, so distinct `(context, data)` pairs
/// cannot collide by boundary confusion. Any client (native Rust, WASM, the
/// Elixir NIF, or hand-rolled JS) that reproduces this framing computes an
/// identical digest.
///
/// `context` is a UTF-8 label, conventionally a versioned namespace such as
/// `"mosslet/key-fingerprint/v1"`. An empty context is permitted (it still
/// differs from `sha3_512(data)` because of the length prefix) but is
/// discouraged — pick an explicit label.
///
/// ```
/// use metamorphic_crypto::hash::sha3_512_with_context;
///
/// let fp = sha3_512_with_context("mosslet/key-fingerprint/v1", b"public key bytes");
/// assert_eq!(fp.len(), 64);
///
/// // Same bytes under a different context produce an unrelated digest.
/// let log = sha3_512_with_context("mosslet/log-entry/v1", b"public key bytes");
/// assert_ne!(fp, log);
/// ```
/// Compute the SHA3-256 digest of `data`, returning 32 bytes.
///
/// This is the same primitive used by the crate's hybrid KEM combiner.
///
/// ```
/// use metamorphic_crypto::hash::sha3_256;
///
/// let digest = sha3_256(b"hello, metamorphic!");
/// assert_eq!(digest.len(), 32);
/// ```
/// Compute the SHA-256 (SHA-2 family) digest of `data`, returning 32 bytes.
///
/// Provided for interoperability with systems standardized on SHA-2. Prefer
/// [`sha3_512`] for new code.
///
/// ```
/// use metamorphic_crypto::hash::sha256;
///
/// let digest = sha256(b"hello, metamorphic!");
/// assert_eq!(digest.len(), 32);
/// ```
/// Compute the SHA-512 (SHA-2 family) digest of `data`, returning 64 bytes.
///
/// Provided for interoperability with systems standardized on SHA-2. Prefer
/// [`sha3_512`] for new code.
///
/// ```
/// use metamorphic_crypto::hash::sha512;
///
/// let digest = sha512(b"hello, metamorphic!");
/// assert_eq!(digest.len(), 64);
/// ```