oxicrypto-hash 0.1.0

Pure Rust hash implementations for OxiCrypto (SHA-2, SHA-3, BLAKE3)
Documentation
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
// ── XOF / cSHAKE / TupleHash / BLAKE2b-keyed / hash_file ────────────────────
//
//! Extendable-output functions (XOFs) and related constructs per NIST SP 800-185.
//!
//! Provides:
//! - SHAKE128 / SHAKE256 XOFs (FIPS 202)
//! - cSHAKE128 / cSHAKE256 with function-name and customization strings
//! - TupleHash128 / TupleHash256 (unambiguous encoding of byte-string tuples)
//! - BLAKE2b keyed-hash (variable-length MAC mode, RFC 7693)
//! - [`hash_file`] convenience functions (behind `std` feature)

use alloc::vec::Vec;
use cshake::digest::{ExtendableOutput, Update, XofReader};
use oxicrypto_core::CryptoError;
use shake::digest::{ExtendableOutput as ShakeExtend, Update as ShakeUpdate};

// ── SHAKE128 / SHAKE256 ───────────────────────────────────────────────────────

/// SHAKE128 XOF reader: stream an arbitrary number of output bytes from a
/// finalised SHAKE128 state.
pub struct Shake128Reader(shake::Shake128Reader);

impl Shake128Reader {
    /// Fill `out` with the next output bytes.
    pub fn read(&mut self, out: &mut [u8]) {
        self.0.read(out);
    }
}

/// SHAKE256 XOF reader: stream an arbitrary number of output bytes from a
/// finalised SHAKE256 state.
pub struct Shake256Reader(shake::Shake256Reader);

impl Shake256Reader {
    /// Fill `out` with the next output bytes.
    pub fn read(&mut self, out: &mut [u8]) {
        self.0.read(out);
    }
}

/// Hash `msg` with SHAKE128 and fill `out` with extendable output.
pub fn shake128(msg: &[u8], out: &mut [u8]) {
    let mut h = shake::Shake128::default();
    ShakeUpdate::update(&mut h, msg);
    let mut reader = ShakeExtend::finalize_xof(h);
    reader.read(out);
}

/// Hash `msg` with SHAKE256 and fill `out` with extendable output.
pub fn shake256(msg: &[u8], out: &mut [u8]) {
    let mut h = shake::Shake256::default();
    ShakeUpdate::update(&mut h, msg);
    let mut reader = ShakeExtend::finalize_xof(h);
    reader.read(out);
}

/// Begin a SHAKE128 computation, returning a finalisable hasher.
///
/// The `msg` argument absorbs message data; the returned [`Shake128Reader`]
/// provides streaming output.
pub fn shake128_start(msg: &[u8]) -> Shake128Reader {
    let mut h = shake::Shake128::default();
    ShakeUpdate::update(&mut h, msg);
    Shake128Reader(ShakeExtend::finalize_xof(h))
}

/// Begin a SHAKE256 computation, returning a finalisable hasher.
pub fn shake256_start(msg: &[u8]) -> Shake256Reader {
    let mut h = shake::Shake256::default();
    ShakeUpdate::update(&mut h, msg);
    Shake256Reader(ShakeExtend::finalize_xof(h))
}

// ── cSHAKE128 / cSHAKE256 ────────────────────────────────────────────────────

/// Hash `msg` with cSHAKE128 (NIST SP 800-185 §3) and fill `out`.
///
/// When both `function_name` and `customization` are empty this degrades to
/// SHAKE128 (per spec).
pub fn cshake128(msg: &[u8], function_name: &[u8], customization: &[u8], out: &mut [u8]) {
    let mut h = cshake::CShake128::new_with_function_name(function_name, customization);
    Update::update(&mut h, msg);
    let mut reader = ExtendableOutput::finalize_xof(h);
    reader.read(out);
}

/// Hash `msg` with cSHAKE256 (NIST SP 800-185 §3) and fill `out`.
///
/// When both `function_name` and `customization` are empty this degrades to
/// SHAKE256 (per spec).
pub fn cshake256(msg: &[u8], function_name: &[u8], customization: &[u8], out: &mut [u8]) {
    let mut h = cshake::CShake256::new_with_function_name(function_name, customization);
    Update::update(&mut h, msg);
    let mut reader = ExtendableOutput::finalize_xof(h);
    reader.read(out);
}

// ── TupleHash encoding helpers (SP 800-185 §2.3) ────────────────────────────

/// `left_encode(x)`: big-endian byte encoding of `x`, prepended by a 1-byte
/// length count indicating the number of non-zero bytes.
pub(crate) fn left_encode(x: u64) -> Vec<u8> {
    if x == 0 {
        return alloc::vec![1u8, 0u8];
    }
    let be = x.to_be_bytes();
    let leading_zeros = be.iter().take_while(|&&b| b == 0).count();
    let n = 8 - leading_zeros; // number of significant bytes
    let mut out = Vec::with_capacity(1 + n);
    out.push(n as u8);
    out.extend_from_slice(&be[leading_zeros..]);
    out
}

/// `right_encode(x)`: big-endian byte encoding of `x`, followed by a 1-byte
/// length count indicating the number of non-zero bytes.
pub(crate) fn right_encode(x: u64) -> Vec<u8> {
    if x == 0 {
        return alloc::vec![0u8, 1u8];
    }
    let be = x.to_be_bytes();
    let leading_zeros = be.iter().take_while(|&&b| b == 0).count();
    let n = 8 - leading_zeros;
    let mut out = Vec::with_capacity(n + 1);
    out.extend_from_slice(&be[leading_zeros..]);
    out.push(n as u8);
    out
}

/// `encode_string(s)` = `left_encode(s.len() * 8)` || `s`.
///
/// # Errors
///
/// Returns [`CryptoError::BadInput`] if `s.len() * 8` overflows a `u64`. This is
/// unreachable in practice (a slice cannot exceed `isize::MAX` bytes), but the
/// check avoids any panic per the no-unwrap policy.
pub(crate) fn encode_string(s: &[u8]) -> Result<Vec<u8>, CryptoError> {
    let bit_len = (s.len() as u64)
        .checked_mul(8)
        .ok_or(CryptoError::BadInput)?;
    let mut out = left_encode(bit_len);
    out.extend_from_slice(s);
    Ok(out)
}

// ── TupleHash128 / TupleHash256 ───────────────────────────────────────────────

/// TupleHash128 (NIST SP 800-185 §5).
///
/// Hashes the *tuple* of byte strings in `inputs` with optional `customization`
/// string; result length equals `out.len()`.
///
/// Crucially, `tuple_hash128(&[b"ab", b"c"], ...)` differs from
/// `tuple_hash128(&[b"a", b"bc"], ...)` — the encoding is unambiguous.
///
/// # Errors
///
/// Returns [`CryptoError::BadInput`] if any input length or `out.len()`,
/// multiplied by 8, overflows a `u64` (unreachable in practice).
pub fn tuple_hash128(
    inputs: &[&[u8]],
    customization: &[u8],
    out: &mut [u8],
) -> Result<(), CryptoError> {
    let mut h = cshake::CShake128::new_with_function_name(b"TupleHash", customization);

    // encode_tuple(X) = encode_string(X[0]) || ... || encode_string(X[n-1])
    for &input in inputs {
        let encoded = encode_string(input)?;
        Update::update(&mut h, &encoded);
    }

    // right_encode(L) where L = output length in bits
    let out_bits = (out.len() as u64)
        .checked_mul(8)
        .ok_or(CryptoError::BadInput)?;
    let renc = right_encode(out_bits);
    Update::update(&mut h, &renc);

    let mut reader = ExtendableOutput::finalize_xof(h);
    reader.read(out);
    Ok(())
}

/// TupleHash256 (NIST SP 800-185 §5).
///
/// Hashes the *tuple* of byte strings in `inputs` with optional `customization`
/// string; result length equals `out.len()`.
///
/// # Errors
///
/// Returns [`CryptoError::BadInput`] if any input length or `out.len()`,
/// multiplied by 8, overflows a `u64` (unreachable in practice).
pub fn tuple_hash256(
    inputs: &[&[u8]],
    customization: &[u8],
    out: &mut [u8],
) -> Result<(), CryptoError> {
    let mut h = cshake::CShake256::new_with_function_name(b"TupleHash", customization);

    for &input in inputs {
        let encoded = encode_string(input)?;
        Update::update(&mut h, &encoded);
    }

    let out_bits = (out.len() as u64)
        .checked_mul(8)
        .ok_or(CryptoError::BadInput)?;
    let renc = right_encode(out_bits);
    Update::update(&mut h, &renc);

    let mut reader = ExtendableOutput::finalize_xof(h);
    reader.read(out);
    Ok(())
}

// ── BLAKE2b keyed-hash mode ───────────────────────────────────────────────────

/// BLAKE2b keyed-hash (MAC mode), with variable output size 1–64 bytes.
///
/// BLAKE2b in keyed mode is a one-pass MAC: the key is absorbed as the first
/// block. Keys must be between 1 and 64 bytes inclusive.
pub struct Blake2bKeyed {
    key: Vec<u8>,
}

impl core::fmt::Debug for Blake2bKeyed {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "Blake2bKeyed(***)")
    }
}

impl Blake2bKeyed {
    /// Create a keyed BLAKE2b hasher.
    ///
    /// # Errors
    ///
    /// Returns [`CryptoError::InvalidKey`] if `key` is empty or longer than 64 bytes.
    pub fn new(key: &[u8]) -> Result<Self, CryptoError> {
        if key.is_empty() || key.len() > 64 {
            return Err(CryptoError::InvalidKey);
        }
        Ok(Self { key: key.to_vec() })
    }

    /// Hash `msg` under this key; output is written to `out`.
    ///
    /// `out.len()` must be between 1 and 64 bytes inclusive.
    ///
    /// # Errors
    ///
    /// Returns [`CryptoError::BadInput`] if `out` is empty or longer than 64 bytes.
    /// Returns [`CryptoError::InvalidKey`] if the key is invalid (should not
    /// happen after successful [`new`](Self::new)).
    pub fn hash(&self, msg: &[u8], out: &mut [u8]) -> Result<(), CryptoError> {
        blake2b_keyed(&self.key, msg, out)
    }
}

/// Hash `msg` under `key` with BLAKE2b in keyed mode; output is written to `out`.
///
/// Both `key.len()` (1–64 bytes) and `out.len()` (1–64 bytes) are validated.
///
/// # Errors
///
/// Returns [`CryptoError::InvalidKey`] if `key` is empty or longer than 64 bytes.
/// Returns [`CryptoError::BadInput`] if `out` is empty or longer than 64 bytes.
pub fn blake2b_keyed(key: &[u8], msg: &[u8], out: &mut [u8]) -> Result<(), CryptoError> {
    use blake2::digest::{FixedOutput, KeyInit, Update as MacUpdate};

    if key.is_empty() || key.len() > 64 {
        return Err(CryptoError::InvalidKey);
    }
    if out.is_empty() || out.len() > 64 {
        return Err(CryptoError::BadInput);
    }

    // Blake2bMac512 gives a full 64-byte MAC; we then truncate to out.len()
    let mut mac =
        blake2::Blake2bMac512::new_from_slice(key).map_err(|_| CryptoError::InvalidKey)?;
    MacUpdate::update(&mut mac, msg);
    let full = mac.finalize_fixed();
    out.copy_from_slice(&full[..out.len()]);
    Ok(())
}

// ── hash_file (std feature) ───────────────────────────────────────────────────

/// Hash a file at `path` with SHA-256, returning a 32-byte digest.
///
/// Reads the file in 64 KB chunks. Maps I/O errors to [`CryptoError::Internal`].
///
/// # Errors
///
/// Returns [`CryptoError::Internal`] if the file cannot be read.
#[cfg(feature = "std")]
pub fn hash_file_sha256(path: &std::path::Path) -> Result<[u8; 32], CryptoError> {
    use sha2::Digest;
    use std::io::Read;

    let file = std::fs::File::open(path).map_err(|_| CryptoError::Internal("cannot open file"))?;
    let mut reader = std::io::BufReader::new(file);
    let mut hasher = sha2::Sha256::new();
    let mut buf = alloc::vec![0u8; 65536];

    loop {
        let n = reader
            .read(&mut buf)
            .map_err(|_| CryptoError::Internal("file read error"))?;
        if n == 0 {
            break;
        }
        Digest::update(&mut hasher, &buf[..n]);
    }

    let result = Digest::finalize(hasher);
    let mut out = [0u8; 32];
    out.copy_from_slice(&result);
    Ok(out)
}

/// Hash a file at `path` with SHA-512, returning a 64-byte digest.
///
/// Reads the file in 64 KB chunks. Maps I/O errors to [`CryptoError::Internal`].
///
/// # Errors
///
/// Returns [`CryptoError::Internal`] if the file cannot be read.
#[cfg(feature = "std")]
pub fn hash_file_sha512(path: &std::path::Path) -> Result<[u8; 64], CryptoError> {
    use sha2::Digest;
    use std::io::Read;

    let file = std::fs::File::open(path).map_err(|_| CryptoError::Internal("cannot open file"))?;
    let mut reader = std::io::BufReader::new(file);
    let mut hasher = sha2::Sha512::new();
    let mut buf = alloc::vec![0u8; 65536];

    loop {
        let n = reader
            .read(&mut buf)
            .map_err(|_| CryptoError::Internal("file read error"))?;
        if n == 0 {
            break;
        }
        Digest::update(&mut hasher, &buf[..n]);
    }

    let result = Digest::finalize(hasher);
    let mut out = [0u8; 64];
    out.copy_from_slice(&result);
    Ok(out)
}

/// Hash a file at `path` with BLAKE3, returning a 32-byte digest.
///
/// Reads the file in 64 KB chunks. Maps I/O errors to [`CryptoError::Internal`].
///
/// # Errors
///
/// Returns [`CryptoError::Internal`] if the file cannot be read.
#[cfg(feature = "std")]
pub fn hash_file_blake3(path: &std::path::Path) -> Result<[u8; 32], CryptoError> {
    use std::io::Read;

    let file = std::fs::File::open(path).map_err(|_| CryptoError::Internal("cannot open file"))?;
    let mut reader = std::io::BufReader::new(file);
    let mut hasher = blake3::Hasher::new();
    let mut buf = alloc::vec![0u8; 65536];

    loop {
        let n = reader
            .read(&mut buf)
            .map_err(|_| CryptoError::Internal("file read error"))?;
        if n == 0 {
            break;
        }
        hasher.update(&buf[..n]);
    }

    Ok(*hasher.finalize().as_bytes())
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    // ── SHAKE128 ──────────────────────────────────────────────────────────────

    #[test]
    fn shake128_nonzero_output() {
        let mut out = [0u8; 32];
        shake128(b"abc", &mut out);
        assert!(
            out.iter().any(|&b| b != 0),
            "SHAKE128 output must be non-zero"
        );
    }

    #[test]
    fn shake128_64_prefix_matches_32() {
        let mut out32 = [0u8; 32];
        let mut out64 = [0u8; 64];
        shake128(b"abc", &mut out32);
        shake128(b"abc", &mut out64);
        assert_eq!(
            out32,
            out64[..32],
            "64-byte SHAKE128 must be prefixed by 32-byte output"
        );
    }

    #[test]
    fn shake256_nonzero_output() {
        let mut out = [0u8; 32];
        shake256(b"abc", &mut out);
        assert!(out.iter().any(|&b| b != 0));
    }

    #[test]
    fn shake128_reader_matches_one_shot() {
        let mut expected = [0u8; 48];
        shake128(b"hello", &mut expected);

        let mut reader = shake128_start(b"hello");
        let mut actual = [0u8; 48];
        reader.read(&mut actual);
        assert_eq!(expected, actual);
    }

    // ── cSHAKE128 ─────────────────────────────────────────────────────────────

    #[test]
    fn cshake128_empty_equals_shake128() {
        let mut shake_out = [0u8; 32];
        let mut cshake_out = [0u8; 32];
        shake128(b"abc", &mut shake_out);
        cshake128(b"abc", b"", b"", &mut cshake_out);
        assert_eq!(
            shake_out, cshake_out,
            "cSHAKE128 with empty N and S must equal SHAKE128"
        );
    }

    #[test]
    fn cshake128_custom_differs_from_shake128() {
        let mut shake_out = [0u8; 32];
        let mut cshake_out = [0u8; 32];
        shake128(b"abc", &mut shake_out);
        cshake128(b"abc", b"Email Signature", b"", &mut cshake_out);
        assert_ne!(
            shake_out, cshake_out,
            "cSHAKE128 with non-empty N must differ from SHAKE128"
        );
    }

    #[test]
    fn cshake256_empty_equals_shake256() {
        let mut shake_out = [0u8; 64];
        let mut cshake_out = [0u8; 64];
        shake256(b"abc", &mut shake_out);
        cshake256(b"abc", b"", b"", &mut cshake_out);
        assert_eq!(
            shake_out, cshake_out,
            "cSHAKE256 with empty N and S must equal SHAKE256"
        );
    }

    #[test]
    fn cshake128_customization_matters() {
        let mut out1 = [0u8; 32];
        let mut out2 = [0u8; 32];
        cshake128(b"abc", b"", b"customA", &mut out1);
        cshake128(b"abc", b"", b"customB", &mut out2);
        assert_ne!(
            out1, out2,
            "Different customization strings must produce different outputs"
        );
    }

    // ── TupleHash ──────────────────────────────────────────────────────────────

    #[test]
    fn tuple_hash128_unambiguous() {
        let mut out1 = [0u8; 32];
        let mut out2 = [0u8; 32];
        tuple_hash128(&[b"ab", b"c"], b"", &mut out1).unwrap();
        tuple_hash128(&[b"a", b"bc"], b"", &mut out2).unwrap();
        assert_ne!(
            out1, out2,
            "TupleHash128 must disambiguate ('ab','c') from ('a','bc')"
        );
    }

    #[test]
    fn tuple_hash256_unambiguous() {
        let mut out1 = [0u8; 32];
        let mut out2 = [0u8; 32];
        tuple_hash256(&[b"ab", b"c"], b"", &mut out1).unwrap();
        tuple_hash256(&[b"a", b"bc"], b"", &mut out2).unwrap();
        assert_ne!(
            out1, out2,
            "TupleHash256 must disambiguate ('ab','c') from ('a','bc')"
        );
    }

    #[test]
    fn tuple_hash128_deterministic() {
        let mut out1 = [0u8; 32];
        let mut out2 = [0u8; 32];
        tuple_hash128(&[b"hello", b"world"], b"custom", &mut out1).unwrap();
        tuple_hash128(&[b"hello", b"world"], b"custom", &mut out2).unwrap();
        assert_eq!(out1, out2, "TupleHash128 must be deterministic");
    }

    #[test]
    fn tuple_hash128_customization_matters() {
        let mut out1 = [0u8; 32];
        let mut out2 = [0u8; 32];
        tuple_hash128(&[b"data"], b"custA", &mut out1).unwrap();
        tuple_hash128(&[b"data"], b"custB", &mut out2).unwrap();
        assert_ne!(
            out1, out2,
            "Different customizations must produce different TupleHash128 outputs"
        );
    }

    // ── Encoding helpers ────────────────────────────────────────────────────────

    #[test]
    fn left_encode_zero() {
        assert_eq!(left_encode(0), alloc::vec![1u8, 0u8]);
    }

    #[test]
    fn left_encode_one() {
        // 1 → 1 byte significant; value = 0x01
        assert_eq!(left_encode(1), alloc::vec![1u8, 1u8]);
    }

    #[test]
    fn right_encode_zero() {
        assert_eq!(right_encode(0), alloc::vec![0u8, 1u8]);
    }

    #[test]
    fn right_encode_one() {
        assert_eq!(right_encode(1), alloc::vec![1u8, 1u8]);
    }

    // ── BLAKE2b keyed ────────────────────────────────────────────────────────────

    #[test]
    fn blake2b_keyed_different_keys() {
        let mut out1 = [0u8; 32];
        let mut out2 = [0u8; 32];
        blake2b_keyed(b"key1", b"message", &mut out1).unwrap();
        blake2b_keyed(b"key2", b"message", &mut out2).unwrap();
        assert_ne!(
            out1, out2,
            "Different keys must produce different BLAKE2b-keyed outputs"
        );
    }

    #[test]
    fn blake2b_keyed_different_messages() {
        let mut out1 = [0u8; 32];
        let mut out2 = [0u8; 32];
        blake2b_keyed(b"key", b"message1", &mut out1).unwrap();
        blake2b_keyed(b"key", b"message2", &mut out2).unwrap();
        assert_ne!(out1, out2);
    }

    #[test]
    fn blake2b_keyed_empty_key_rejected() {
        let mut out = [0u8; 32];
        assert_eq!(
            blake2b_keyed(b"", b"msg", &mut out).unwrap_err(),
            CryptoError::InvalidKey
        );
    }

    #[test]
    fn blake2b_keyed_too_long_key_rejected() {
        let mut out = [0u8; 32];
        assert_eq!(
            blake2b_keyed(&[0u8; 65], b"msg", &mut out).unwrap_err(),
            CryptoError::InvalidKey
        );
    }

    #[test]
    fn blake2b_keyed_empty_output_rejected() {
        assert_eq!(
            blake2b_keyed(b"key", b"msg", &mut []).unwrap_err(),
            CryptoError::BadInput
        );
    }

    #[test]
    fn blake2b_keyed_64byte_key_ok() {
        let mut out = [0u8; 64];
        blake2b_keyed(&[0x42u8; 64], b"hello", &mut out).unwrap();
        assert!(out.iter().any(|&b| b != 0));
    }

    #[test]
    fn blake2b_keyed_struct_api() {
        let key = b"my secret key";
        let msg = b"hello world";
        let mut out_fn = [0u8; 32];
        let mut out_struct = [0u8; 32];

        blake2b_keyed(key, msg, &mut out_fn).unwrap();
        Blake2bKeyed::new(key)
            .unwrap()
            .hash(msg, &mut out_struct)
            .unwrap();

        assert_eq!(
            out_fn, out_struct,
            "Free function and struct API must agree"
        );
    }

    #[test]
    fn blake2b_keyed_struct_empty_key_rejected() {
        assert_eq!(Blake2bKeyed::new(b"").unwrap_err(), CryptoError::InvalidKey);
    }

    // ── hash_file ─────────────────────────────────────────────────────────────

    #[cfg(feature = "std")]
    #[test]
    fn hash_file_sha256_matches_in_memory() {
        use sha2::Digest;
        use std::io::Write;

        let content = b"Hello, hash_file test!";

        let mut path = std::env::temp_dir();
        path.push("oxicrypto_hash_file_test.bin");

        {
            let mut f = std::fs::File::create(&path).unwrap();
            f.write_all(content).unwrap();
        }

        let expected = sha2::Sha256::digest(content);
        let actual = hash_file_sha256(&path).unwrap();

        assert_eq!(actual.as_slice(), expected.as_slice());

        let _ = std::fs::remove_file(&path);
    }

    #[cfg(feature = "std")]
    #[test]
    fn hash_file_sha512_matches_in_memory() {
        use sha2::Digest;
        use std::io::Write;

        let content = b"SHA-512 file hash test";

        let mut path = std::env::temp_dir();
        path.push("oxicrypto_hash_file_sha512_test.bin");

        {
            let mut f = std::fs::File::create(&path).unwrap();
            f.write_all(content).unwrap();
        }

        let expected = sha2::Sha512::digest(content);
        let actual = hash_file_sha512(&path).unwrap();

        assert_eq!(actual.as_slice(), expected.as_slice());

        let _ = std::fs::remove_file(&path);
    }

    #[cfg(feature = "std")]
    #[test]
    fn hash_file_blake3_matches_in_memory() {
        use std::io::Write;

        let content = b"BLAKE3 file hash test";

        let mut path = std::env::temp_dir();
        path.push("oxicrypto_hash_file_blake3_test.bin");

        {
            let mut f = std::fs::File::create(&path).unwrap();
            f.write_all(content).unwrap();
        }

        let expected = *blake3::hash(content).as_bytes();
        let actual = hash_file_blake3(&path).unwrap();

        assert_eq!(actual, expected);

        let _ = std::fs::remove_file(&path);
    }

    #[cfg(feature = "std")]
    #[test]
    fn hash_file_sha256_not_found() {
        let path = std::env::temp_dir().join("oxicrypto_nonexistent_file_12345678.bin");
        let err = hash_file_sha256(&path).unwrap_err();
        assert!(matches!(err, CryptoError::Internal(_)));
    }
}