rustack-s3-core 0.9.0

S3 service implementation for Rustack
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
//! Checksum computation for S3 objects.
//!
//! Provides functions to compute MD5, SHA-1, SHA-256, CRC32, and CRC32C
//! checksums used by S3 for ETags and the additional checksum algorithms
//! supported by the `x-amz-checksum-*` headers.
//!
//! # Streaming Hashing
//!
//! For large objects that cannot be buffered entirely in memory, use
//! [`StreamingHasher`] to incrementally feed data and obtain the final
//! results via [`HasherResult`].

use std::{fmt, str::FromStr};

use base64::{Engine, engine::general_purpose::STANDARD as BASE64_STANDARD};
use digest::Digest;

// ---------------------------------------------------------------------------
// ChecksumAlgorithm
// ---------------------------------------------------------------------------

/// S3-supported checksum algorithms (excluding MD5 which is always computed
/// for the ETag).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ChecksumAlgorithm {
    /// CRC-32 (IEEE 802.3).
    Crc32,
    /// CRC-32C (Castagnoli).
    Crc32c,
    /// CRC-64/NVME (NVMe polynomial, hardware-accelerated).
    Crc64Nvme,
    /// SHA-1.
    Sha1,
    /// SHA-256.
    Sha256,
}

impl ChecksumAlgorithm {
    /// Return the canonical string representation used in S3 headers.
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Crc32 => "CRC32",
            Self::Crc32c => "CRC32C",
            Self::Crc64Nvme => "CRC64NVME",
            Self::Sha1 => "SHA1",
            Self::Sha256 => "SHA256",
        }
    }
}

impl fmt::Display for ChecksumAlgorithm {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Error returned when parsing a [`ChecksumAlgorithm`] from a string fails.
#[derive(Debug, Clone, thiserror::Error)]
#[error("unknown checksum algorithm: {0}")]
pub struct ParseChecksumAlgorithmError(String);

impl FromStr for ChecksumAlgorithm {
    type Err = ParseChecksumAlgorithmError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_ascii_uppercase().as_str() {
            "CRC32" => Ok(Self::Crc32),
            "CRC32C" => Ok(Self::Crc32c),
            "CRC64NVME" => Ok(Self::Crc64Nvme),
            "SHA1" => Ok(Self::Sha1),
            "SHA256" => Ok(Self::Sha256),
            _ => Err(ParseChecksumAlgorithmError(s.to_owned())),
        }
    }
}

// ---------------------------------------------------------------------------
// ChecksumValue
// ---------------------------------------------------------------------------

/// A base64-encoded checksum value paired with its algorithm.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ChecksumValue {
    /// The algorithm used to compute this checksum.
    pub algorithm: ChecksumAlgorithm,
    /// The base64-encoded checksum.
    pub value: String,
}

// ---------------------------------------------------------------------------
// Standalone checksum functions
// ---------------------------------------------------------------------------

/// Compute the hex-encoded MD5 digest of `data`.
///
/// This is the raw hex digest used internally. For an S3-formatted ETag (quoted),
/// use [`compute_etag`].
///
/// # Examples
///
/// ```
/// use rustack_s3_core::checksums::compute_md5;
///
/// let digest = compute_md5(b"hello");
/// assert_eq!(digest, "5d41402abc4b2a76b9719d911017c592");
/// ```
#[must_use]
pub fn compute_md5(data: &[u8]) -> String {
    let hash = md5::Md5::digest(data);
    hex::encode(hash)
}

/// Compute the quoted hex-encoded MD5 digest of `data`, suitable for use as
/// an S3 ETag.
///
/// The returned string is surrounded by double quotes, e.g.
/// `"5d41402abc4b2a76b9719d911017c592"`.
///
/// # Examples
///
/// ```
/// use rustack_s3_core::checksums::compute_etag;
///
/// let etag = compute_etag(b"");
/// assert_eq!(etag, "\"d41d8cd98f00b204e9800998ecf8427e\"");
/// ```
#[must_use]
pub fn compute_etag(data: &[u8]) -> String {
    let md5_hex = compute_md5(data);
    format!("\"{md5_hex}\"")
}

/// Compute a composite ETag for a multipart upload.
///
/// The composite ETag is the MD5 of the concatenated binary MD5 digests of
/// each part, formatted as `"<hex>-<part_count>"`.
///
/// Each entry in `part_md5_hexes` should be the *unquoted* hex MD5 of a part.
///
/// # Examples
///
/// ```
/// use rustack_s3_core::checksums::compute_multipart_etag;
///
/// let part_hexes = ["5d41402abc4b2a76b9719d911017c592"];
/// let etag = compute_multipart_etag(&part_hexes, 1);
/// assert!(etag.ends_with("-1\""));
/// ```
#[must_use]
pub fn compute_multipart_etag(part_md5_hexes: &[impl AsRef<str>], part_count: usize) -> String {
    let mut combined = Vec::with_capacity(part_md5_hexes.len() * 16);
    for hex_str in part_md5_hexes {
        let hex_str = hex_str.as_ref().trim_matches('"');
        if let Ok(bytes) = hex::decode(hex_str) {
            combined.extend_from_slice(&bytes);
        }
    }
    let final_md5 = hex::encode(md5::Md5::digest(&combined));
    format!("\"{final_md5}-{part_count}\"")
}

/// Compute a base64-encoded checksum for the given algorithm.
///
/// # Examples
///
/// ```
/// use rustack_s3_core::checksums::{ChecksumAlgorithm, compute_checksum};
///
/// let b64 = compute_checksum(ChecksumAlgorithm::Crc32, b"hello");
/// assert!(!b64.is_empty());
/// ```
#[must_use]
pub fn compute_checksum(algorithm: ChecksumAlgorithm, data: &[u8]) -> String {
    match algorithm {
        ChecksumAlgorithm::Crc32 => {
            let mut hasher = crc32fast::Hasher::new();
            hasher.update(data);
            let value = hasher.finalize();
            BASE64_STANDARD.encode(value.to_be_bytes())
        }
        ChecksumAlgorithm::Crc32c => {
            let value = crc32c::crc32c(data);
            BASE64_STANDARD.encode(value.to_be_bytes())
        }
        ChecksumAlgorithm::Crc64Nvme => {
            let mut hasher = crc64fast_nvme::Digest::new();
            hasher.write(data);
            let value = hasher.sum64();
            BASE64_STANDARD.encode(value.to_be_bytes())
        }
        ChecksumAlgorithm::Sha1 => {
            let hash = sha1::Sha1::digest(data);
            BASE64_STANDARD.encode(hash)
        }
        ChecksumAlgorithm::Sha256 => {
            let hash = sha2::Sha256::digest(data);
            BASE64_STANDARD.encode(hash)
        }
    }
}

/// Compute a composite checksum for a multipart upload.
///
/// The composite checksum is computed by concatenating the raw (decoded)
/// checksums of each part and then computing the checksum of that
/// concatenation. The result is base64-encoded with a `-<part_count>` suffix.
///
/// Each entry in `part_checksums_b64` should be the base64-encoded checksum
/// of a single part (without a part-count suffix).
///
/// # Examples
///
/// ```
/// use rustack_s3_core::checksums::{ChecksumAlgorithm, compute_checksum, compute_composite_checksum};
///
/// let part1 = compute_checksum(ChecksumAlgorithm::Crc32, b"hello");
/// let composite = compute_composite_checksum(ChecksumAlgorithm::Crc32, &[part1]);
/// assert!(composite.contains("-1"));
/// ```
#[must_use]
pub fn compute_composite_checksum(
    algorithm: ChecksumAlgorithm,
    part_checksums_b64: &[impl AsRef<str>],
) -> String {
    let mut combined = Vec::new();
    for b64 in part_checksums_b64 {
        if let Ok(bytes) = BASE64_STANDARD.decode(b64.as_ref()) {
            combined.extend_from_slice(&bytes);
        }
    }
    let checksum_b64 = compute_checksum(algorithm, &combined);
    format!("{checksum_b64}-{}", part_checksums_b64.len())
}

// ---------------------------------------------------------------------------
// StreamingHasher
// ---------------------------------------------------------------------------

/// Result produced by [`StreamingHasher::finish`].
#[derive(Debug, Clone)]
pub struct HasherResult {
    /// Hex-encoded MD5 digest.
    pub md5_hex: String,
    /// Per-algorithm base64-encoded checksums (only for algorithms that were
    /// requested when the hasher was created).
    pub checksums: Vec<ChecksumValue>,
}

/// Incremental hasher that computes MD5 and optionally additional checksums
/// over a stream of data chunks.
///
/// # Examples
///
/// ```
/// use rustack_s3_core::checksums::{ChecksumAlgorithm, StreamingHasher};
///
/// let mut hasher = StreamingHasher::new(&[ChecksumAlgorithm::Sha256]);
/// hasher.update(b"hello ");
/// hasher.update(b"world");
/// let result = hasher.finish();
/// assert!(!result.md5_hex.is_empty());
/// assert_eq!(result.checksums.len(), 1);
/// ```
pub struct StreamingHasher {
    md5: md5::Md5,
    sha1: Option<sha1::Sha1>,
    sha256: Option<sha2::Sha256>,
    crc32: Option<crc32fast::Hasher>,
    crc32c: Option<u32>,
    crc64nvme: Option<crc64fast_nvme::Digest>,
    algorithms: Vec<ChecksumAlgorithm>,
}

impl fmt::Debug for StreamingHasher {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("StreamingHasher")
            .field("algorithms", &self.algorithms)
            .finish_non_exhaustive()
    }
}

impl StreamingHasher {
    /// Create a new streaming hasher.
    ///
    /// MD5 is always computed. Provide additional algorithms in `algorithms`
    /// to compute extra checksums.
    #[must_use]
    pub fn new(algorithms: &[ChecksumAlgorithm]) -> Self {
        let mut sha1 = None;
        let mut sha256 = None;
        let mut crc32 = None;
        let mut crc32c = None;
        let mut crc64nvme = None;

        for &algo in algorithms {
            match algo {
                ChecksumAlgorithm::Sha1 => {
                    sha1 = Some(<sha1::Sha1 as Digest>::new());
                }
                ChecksumAlgorithm::Sha256 => {
                    sha256 = Some(<sha2::Sha256 as Digest>::new());
                }
                ChecksumAlgorithm::Crc32 => {
                    crc32 = Some(crc32fast::Hasher::new());
                }
                ChecksumAlgorithm::Crc32c => {
                    crc32c = Some(0);
                }
                ChecksumAlgorithm::Crc64Nvme => {
                    crc64nvme = Some(crc64fast_nvme::Digest::new());
                }
            }
        }

        Self {
            md5: <md5::Md5 as Digest>::new(),
            sha1,
            sha256,
            crc32,
            crc32c,
            crc64nvme,
            algorithms: algorithms.to_vec(),
        }
    }

    /// Feed more data into the hasher.
    pub fn update(&mut self, data: &[u8]) {
        Digest::update(&mut self.md5, data);

        if let Some(ref mut h) = self.sha1 {
            Digest::update(h, data);
        }
        if let Some(ref mut h) = self.sha256 {
            Digest::update(h, data);
        }
        if let Some(ref mut h) = self.crc32 {
            h.update(data);
        }
        if let Some(ref mut val) = self.crc32c {
            *val = crc32c::crc32c_append(*val, data);
        }
        if let Some(ref mut h) = self.crc64nvme {
            h.write(data);
        }
    }

    /// Finalize the hasher and return the results.
    ///
    /// This consumes the hasher.
    #[must_use]
    pub fn finish(self) -> HasherResult {
        let md5_hex = hex::encode(Digest::finalize(self.md5));

        let mut checksums = Vec::with_capacity(self.algorithms.len());
        for algo in &self.algorithms {
            let value = match algo {
                ChecksumAlgorithm::Sha1 => {
                    let hash = Digest::finalize(self.sha1.clone().unwrap_or_default());
                    BASE64_STANDARD.encode(hash)
                }
                ChecksumAlgorithm::Sha256 => {
                    let hash = Digest::finalize(self.sha256.clone().unwrap_or_default());
                    BASE64_STANDARD.encode(hash)
                }
                ChecksumAlgorithm::Crc32 => {
                    let val = self
                        .crc32
                        .as_ref()
                        .map_or(0, crc32fast::Hasher::clone_finalize);
                    BASE64_STANDARD.encode(val.to_be_bytes())
                }
                ChecksumAlgorithm::Crc32c => {
                    let val = self.crc32c.unwrap_or(0);
                    BASE64_STANDARD.encode(val.to_be_bytes())
                }
                ChecksumAlgorithm::Crc64Nvme => {
                    let val = self
                        .crc64nvme
                        .as_ref()
                        .map_or(0, crc64fast_nvme::Digest::sum64);
                    BASE64_STANDARD.encode(val.to_be_bytes())
                }
            };
            checksums.push(ChecksumValue {
                algorithm: *algo,
                value,
            });
        }

        HasherResult { md5_hex, checksums }
    }
}

// ---------------------------------------------------------------------------
// crc32fast Hasher clone_finalize helper
// ---------------------------------------------------------------------------

/// Extension trait to finalize a cloned `crc32fast::Hasher` without
/// consuming the original (used by `StreamingHasher::finish`).
trait CloneFinalize {
    /// Clone and finalize, returning the CRC-32 value.
    fn clone_finalize(&self) -> u32;
}

impl CloneFinalize for crc32fast::Hasher {
    fn clone_finalize(&self) -> u32 {
        self.clone().finalize()
    }
}

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

    // -----------------------------------------------------------------------
    // ChecksumAlgorithm
    // -----------------------------------------------------------------------

    #[test]
    fn test_should_display_checksum_algorithm() {
        assert_eq!(ChecksumAlgorithm::Crc32.to_string(), "CRC32");
        assert_eq!(ChecksumAlgorithm::Crc32c.to_string(), "CRC32C");
        assert_eq!(ChecksumAlgorithm::Crc64Nvme.to_string(), "CRC64NVME");
        assert_eq!(ChecksumAlgorithm::Sha1.to_string(), "SHA1");
        assert_eq!(ChecksumAlgorithm::Sha256.to_string(), "SHA256");
    }

    #[test]
    fn test_should_parse_checksum_algorithm() {
        assert_eq!(
            "CRC32".parse::<ChecksumAlgorithm>().ok(),
            Some(ChecksumAlgorithm::Crc32)
        );
        assert_eq!(
            "crc32c".parse::<ChecksumAlgorithm>().ok(),
            Some(ChecksumAlgorithm::Crc32c)
        );
        assert_eq!(
            "CRC64NVME".parse::<ChecksumAlgorithm>().ok(),
            Some(ChecksumAlgorithm::Crc64Nvme)
        );
        assert_eq!(
            "sha1".parse::<ChecksumAlgorithm>().ok(),
            Some(ChecksumAlgorithm::Sha1)
        );
        assert_eq!(
            "SHA256".parse::<ChecksumAlgorithm>().ok(),
            Some(ChecksumAlgorithm::Sha256)
        );
        assert!("unknown".parse::<ChecksumAlgorithm>().is_err());
    }

    // -----------------------------------------------------------------------
    // MD5 / ETag
    // -----------------------------------------------------------------------

    #[test]
    fn test_should_compute_md5_empty() {
        assert_eq!(compute_md5(b""), "d41d8cd98f00b204e9800998ecf8427e");
    }

    #[test]
    fn test_should_compute_md5_hello() {
        assert_eq!(compute_md5(b"hello"), "5d41402abc4b2a76b9719d911017c592");
    }

    #[test]
    fn test_should_compute_etag_empty() {
        assert_eq!(compute_etag(b""), "\"d41d8cd98f00b204e9800998ecf8427e\"");
    }

    #[test]
    fn test_should_compute_etag_with_data() {
        let etag = compute_etag(b"hello");
        assert!(etag.starts_with('"'));
        assert!(etag.ends_with('"'));
        assert_eq!(etag.len(), 34); // 32 hex + 2 quotes
    }

    // -----------------------------------------------------------------------
    // Multipart ETag
    // -----------------------------------------------------------------------

    #[test]
    fn test_should_compute_multipart_etag() {
        let part1_hex = compute_md5(b"hello");
        let part2_hex = compute_md5(b"world");
        let etag = compute_multipart_etag(&[part1_hex, part2_hex], 2);
        assert!(etag.starts_with('"'));
        assert!(etag.ends_with("-2\""));
    }

    #[test]
    fn test_should_compute_multipart_etag_single_part() {
        let part_hex = compute_md5(b"data");
        let etag = compute_multipart_etag(&[part_hex], 1);
        assert!(etag.ends_with("-1\""));
    }

    // -----------------------------------------------------------------------
    // Algorithm-specific checksums
    // -----------------------------------------------------------------------

    #[test]
    fn test_should_compute_crc32_checksum() {
        let b64 = compute_checksum(ChecksumAlgorithm::Crc32, b"hello");
        assert!(!b64.is_empty());
        // Verify round-trip decode
        let decoded = BASE64_STANDARD.decode(&b64);
        assert!(decoded.is_ok());
        assert_eq!(decoded.expect("test decode").len(), 4);
    }

    #[test]
    fn test_should_compute_crc32c_checksum() {
        let b64 = compute_checksum(ChecksumAlgorithm::Crc32c, b"hello");
        assert!(!b64.is_empty());
    }

    #[test]
    fn test_should_compute_crc64nvme_checksum() {
        let b64 = compute_checksum(ChecksumAlgorithm::Crc64Nvme, b"hello");
        assert!(!b64.is_empty());
        let decoded = BASE64_STANDARD.decode(&b64);
        assert!(decoded.is_ok());
        assert_eq!(decoded.expect("test decode").len(), 8);
    }

    #[test]
    fn test_should_compute_sha1_checksum() {
        let b64 = compute_checksum(ChecksumAlgorithm::Sha1, b"hello");
        let decoded = BASE64_STANDARD.decode(&b64);
        assert!(decoded.is_ok());
        assert_eq!(decoded.expect("test decode").len(), 20);
    }

    #[test]
    fn test_should_compute_sha256_checksum() {
        let b64 = compute_checksum(ChecksumAlgorithm::Sha256, b"hello");
        let decoded = BASE64_STANDARD.decode(&b64);
        assert!(decoded.is_ok());
        assert_eq!(decoded.expect("test decode").len(), 32);
    }

    // -----------------------------------------------------------------------
    // Composite checksums
    // -----------------------------------------------------------------------

    #[test]
    fn test_should_compute_composite_checksum() {
        let p1 = compute_checksum(ChecksumAlgorithm::Sha256, b"part1");
        let p2 = compute_checksum(ChecksumAlgorithm::Sha256, b"part2");
        let composite = compute_composite_checksum(ChecksumAlgorithm::Sha256, &[p1, p2]);
        assert!(composite.contains("-2"));
    }

    // -----------------------------------------------------------------------
    // StreamingHasher
    // -----------------------------------------------------------------------

    #[test]
    fn test_should_stream_md5_only() {
        let mut hasher = StreamingHasher::new(&[]);
        hasher.update(b"hello");
        let result = hasher.finish();
        assert_eq!(result.md5_hex, "5d41402abc4b2a76b9719d911017c592");
        assert!(result.checksums.is_empty());
    }

    #[test]
    fn test_should_stream_with_sha256() {
        let mut hasher = StreamingHasher::new(&[ChecksumAlgorithm::Sha256]);
        hasher.update(b"hello ");
        hasher.update(b"world");
        let result = hasher.finish();

        // MD5 of "hello world"
        assert_eq!(result.md5_hex, compute_md5(b"hello world"));

        // SHA-256 of "hello world"
        assert_eq!(result.checksums.len(), 1);
        assert_eq!(result.checksums[0].algorithm, ChecksumAlgorithm::Sha256);
        assert_eq!(
            result.checksums[0].value,
            compute_checksum(ChecksumAlgorithm::Sha256, b"hello world"),
        );
    }

    #[test]
    fn test_should_stream_multiple_algorithms() {
        let algos = [
            ChecksumAlgorithm::Crc32,
            ChecksumAlgorithm::Crc32c,
            ChecksumAlgorithm::Crc64Nvme,
            ChecksumAlgorithm::Sha1,
            ChecksumAlgorithm::Sha256,
        ];
        let mut hasher = StreamingHasher::new(&algos);
        hasher.update(b"test data");
        let result = hasher.finish();

        assert_eq!(result.checksums.len(), 5);
        for (i, algo) in algos.iter().enumerate() {
            assert_eq!(result.checksums[i].algorithm, *algo);
            assert_eq!(
                result.checksums[i].value,
                compute_checksum(*algo, b"test data"),
            );
        }
    }

    #[test]
    fn test_should_match_single_shot_and_streaming_results() {
        let data = b"The quick brown fox jumps over the lazy dog";

        let single_md5 = compute_md5(data);
        let single_sha256 = compute_checksum(ChecksumAlgorithm::Sha256, data);

        let mut hasher = StreamingHasher::new(&[ChecksumAlgorithm::Sha256]);
        // Feed in chunks
        hasher.update(&data[..10]);
        hasher.update(&data[10..30]);
        hasher.update(&data[30..]);
        let result = hasher.finish();

        assert_eq!(result.md5_hex, single_md5);
        assert_eq!(result.checksums[0].value, single_sha256);
    }
}