pulith-fetch 0.2.0

HTTP downloading with streaming verification and atomic placement
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
//! Stream verification functionality.
//!
//! This module provides stream transformation for verifying
//! downloaded content integrity using various checksum algorithms.

use crate::error::{Error, Result};
use pulith_verify::{Hasher, Sha256Hasher};

/// Supported hash algorithms.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HashAlgorithm {
    /// SHA-256 algorithm
    Sha256,
    /// SHA-384 algorithm (not yet implemented)
    Sha384,
    /// SHA-512 algorithm (not yet implemented)
    Sha512,
    /// SHA-1 algorithm (not yet implemented)
    Sha1,
    /// MD5 algorithm (not yet implemented)
    Md5,
}

impl HashAlgorithm {
    /// Get the digest length in bytes for this algorithm.
    pub fn digest_length(&self) -> usize {
        match self {
            HashAlgorithm::Sha256 => 32,
            HashAlgorithm::Sha384 => 48,
            HashAlgorithm::Sha512 => 64,
            HashAlgorithm::Sha1 => 20,
            HashAlgorithm::Md5 => 16,
        }
    }

    /// Get the string representation of this algorithm.
    pub fn as_str(&self) -> &'static str {
        match self {
            HashAlgorithm::Sha256 => "sha256",
            HashAlgorithm::Sha384 => "sha384",
            HashAlgorithm::Sha512 => "sha512",
            HashAlgorithm::Sha1 => "sha1",
            HashAlgorithm::Md5 => "md5",
        }
    }
}

/// Checksum verification configuration.
#[derive(Debug, Clone)]
pub struct ChecksumConfig {
    /// The hash algorithm to use
    pub algorithm: HashAlgorithm,
    /// Expected checksum value (hex-encoded)
    pub expected: String,
}

impl ChecksumConfig {
    /// Create a new checksum configuration.
    pub fn new(algorithm: HashAlgorithm, expected: &str) -> Self {
        Self {
            algorithm,
            expected: expected.to_lowercase(),
        }
    }

    /// Parse checksum from string with algorithm prefix (e.g., "sha256:abc123").
    pub fn from_string(checksum_str: &str) -> Result<Self> {
        if let Some((algo, hash)) = checksum_str.split_once(':') {
            let algorithm = match algo.to_lowercase().as_str() {
                "sha256" => HashAlgorithm::Sha256,
                "sha384" => HashAlgorithm::Sha384,
                "sha512" => HashAlgorithm::Sha512,
                "sha1" => HashAlgorithm::Sha1,
                "md5" => HashAlgorithm::Md5,
                _ => {
                    return Err(Error::InvalidState(format!(
                        "Unsupported hash algorithm: {}",
                        algo
                    )));
                }
            };

            if hash.len() != algorithm.digest_length() * 2 {
                return Err(Error::InvalidState(format!(
                    "Invalid checksum length for {}: expected {}, got {}",
                    algo,
                    algorithm.digest_length() * 2,
                    hash.len()
                )));
            }

            Ok(Self::new(algorithm, hash))
        } else {
            // Default to SHA256 if no algorithm specified
            if checksum_str.len() != 64 {
                return Err(Error::InvalidState(
                    "Invalid checksum length for SHA256: expected 64 characters".to_string(),
                ));
            }
            Ok(Self::new(HashAlgorithm::Sha256, checksum_str))
        }
    }
}

/// Stream verifier for checksum verification.
pub struct StreamVerifier<H: Hasher> {
    hasher: Option<H>,
    config: ChecksumConfig,
    bytes_processed: usize,
    finalized: bool,
}

impl StreamVerifier<Sha256Hasher> {
    /// Create a new stream verifier with the given configuration.
    pub fn new(config: ChecksumConfig) -> Result<Self> {
        let hasher = match config.algorithm {
            HashAlgorithm::Sha256 => Some(Sha256Hasher::new()),
            _ => {
                return Err(Error::InvalidState(format!(
                    "Hash algorithm {:?} not yet implemented",
                    config.algorithm
                )));
            }
        };

        Ok(Self {
            hasher,
            config,
            bytes_processed: 0,
            finalized: false,
        })
    }
}

impl<H: Hasher> StreamVerifier<H> {
    /// Update the verifier with new data.
    pub fn update(&mut self, data: &[u8]) -> Result<()> {
        if self.finalized {
            return Err(Error::InvalidState(
                "Verifier already finalized".to_string(),
            ));
        }

        if let Some(ref mut hasher) = self.hasher {
            hasher.update(data);
        }
        self.bytes_processed += data.len();
        Ok(())
    }

    /// Finalize verification and check if the checksum matches.
    pub fn finalize(&mut self) -> Result<bool> {
        if self.finalized {
            return Err(Error::InvalidState(
                "Verifier already finalized".to_string(),
            ));
        }

        if let Some(hasher) = self.hasher.take() {
            let actual = hasher.finalize();
            let actual_hex = hex::encode(actual);
            self.finalized = true;
            Ok(actual_hex == self.config.expected)
        } else {
            Err(Error::InvalidState("No hasher available".to_string()))
        }
    }

    /// Get the number of bytes processed so far.
    pub fn bytes_processed(&self) -> usize {
        self.bytes_processed
    }

    /// Get the configured algorithm.
    pub fn algorithm(&self) -> HashAlgorithm {
        self.config.algorithm
    }

    /// Get the expected checksum.
    pub fn expected_checksum(&self) -> &str {
        &self.config.expected
    }

    /// Check if the verifier has been finalized.
    pub fn is_finalized(&self) -> bool {
        self.finalized
    }
}

/// Multiple checksum verifier for verifying against multiple algorithms.
pub struct MultiVerifier {
    verifiers: Vec<StreamVerifier<Sha256Hasher>>,
    require_all: bool,
}

impl MultiVerifier {
    /// Create a new multi-verifier.
    ///
    /// If `require_all` is true, all checksums must match.
    /// If false, at least one checksum must match.
    pub fn new(configs: Vec<ChecksumConfig>, require_all: bool) -> Result<Self> {
        let verifiers: Result<Vec<_>> = configs.into_iter().map(StreamVerifier::new).collect();

        Ok(Self {
            verifiers: verifiers?,
            require_all,
        })
    }

    /// Update all verifiers with new data.
    pub fn update(&mut self, data: &[u8]) -> Result<()> {
        for verifier in &mut self.verifiers {
            verifier.update(data)?;
        }
        Ok(())
    }

    /// Finalize verification and check if checksums match.
    pub fn finalize(&mut self) -> Result<bool> {
        let mut results = Vec::new();
        for verifier in &mut self.verifiers {
            results.push(verifier.finalize()?);
        }

        if self.require_all {
            Ok(results.iter().all(|&r| r))
        } else {
            Ok(results.iter().any(|&r| r))
        }
    }

    /// Get the number of verifiers.
    pub fn verifier_count(&self) -> usize {
        self.verifiers.len()
    }
}

/// Convenience function to verify data in one go.
pub fn verify_checksum(data: &[u8], config: &ChecksumConfig) -> Result<bool> {
    let mut verifier = StreamVerifier::new(config.clone())?;
    verifier.update(data)?;
    verifier.finalize()
}

/// Convenience function to verify data with multiple checksums.
pub fn verify_multiple_checksums(
    data: &[u8],
    configs: Vec<ChecksumConfig>,
    require_all: bool,
) -> Result<bool> {
    let mut verifier = MultiVerifier::new(configs, require_all)?;
    verifier.update(data)?;
    verifier.finalize()
}

/// Parse multiple checksums from a string.
///
/// Supports formats like:
/// - "sha256:abc123 sha512:def456"
/// - "sha256:abc123\nsha512:def456"
pub fn parse_multiple_checksums(checksums_str: &str) -> Result<Vec<ChecksumConfig>> {
    let mut configs = Vec::new();

    for line in checksums_str.lines() {
        let line = line.trim();
        if line.is_empty() || line.starts_with('#') {
            continue;
        }

        // Split by whitespace to handle multiple checksums on one line
        for checksum_str in line.split_whitespace() {
            configs.push(ChecksumConfig::from_string(checksum_str)?);
        }
    }

    Ok(configs)
}

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

    #[test]
    fn test_checksum_config_creation() {
        let config = ChecksumConfig::new(HashAlgorithm::Sha256, "abc123");
        assert_eq!(config.algorithm, HashAlgorithm::Sha256);
        assert_eq!(config.expected, "abc123");
    }

    #[test]
    fn test_checksum_config_from_string() {
        // With algorithm prefix - need 64 chars for SHA256
        let config = ChecksumConfig::from_string(
            "sha256:abc123def456abc123def456abc123def456abc123def456abc123def4567890",
        )
        .unwrap();
        assert_eq!(config.algorithm, HashAlgorithm::Sha256);
        assert_eq!(
            config.expected,
            "abc123def456abc123def456abc123def456abc123def456abc123def4567890"
        );

        // Without algorithm prefix (defaults to SHA256) - need 64 chars
        let config = ChecksumConfig::from_string(
            "abc123def456abc123def456abc123def456abc123def456abc123def4567890",
        )
        .unwrap();
        assert_eq!(config.algorithm, HashAlgorithm::Sha256);
        assert_eq!(
            config.expected,
            "abc123def456abc123def456abc123def456abc123def456abc123def4567890"
        );
    }

    #[test]
    fn test_checksum_config_invalid_algorithm() {
        let result = ChecksumConfig::from_string("invalid:abc123");
        assert!(result.is_err());
    }

    #[test]
    fn test_checksum_config_invalid_length() {
        // Invalid SHA256 length
        let result = ChecksumConfig::from_string("sha256:abc");
        assert!(result.is_err());

        // Invalid default length
        let result = ChecksumConfig::from_string("abc");
        assert!(result.is_err());
    }

    #[test]
    fn test_stream_verifier() {
        let data = b"Hello, World!";
        let config = ChecksumConfig::new(
            HashAlgorithm::Sha256,
            "dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f",
        );

        let mut verifier = StreamVerifier::new(config).unwrap();
        verifier.update(data).unwrap();
        let result = verifier.finalize().unwrap();

        assert!(result);
        assert_eq!(verifier.bytes_processed(), data.len());
    }

    #[test]
    fn test_stream_verifier_wrong_checksum() {
        let data = b"Hello, World!";
        let config = ChecksumConfig::new(HashAlgorithm::Sha256, "wrong_checksum");

        let mut verifier = StreamVerifier::new(config).unwrap();
        verifier.update(data).unwrap();
        let result = verifier.finalize().unwrap();

        assert!(!result);
    }

    #[test]
    fn test_stream_verifier_partial_updates() {
        let data = b"Hello, World!";
        let config = ChecksumConfig::new(
            HashAlgorithm::Sha256,
            "dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f",
        );

        let mut verifier = StreamVerifier::new(config).unwrap();

        // Update in chunks
        verifier.update(&data[..5]).unwrap();
        verifier.update(&data[5..]).unwrap();

        let result = verifier.finalize().unwrap();
        assert!(result);
    }

    #[test]
    fn test_multi_verifier_all_required() {
        let data = b"Hello, World!";
        let configs = vec![ChecksumConfig::new(
            HashAlgorithm::Sha256,
            "dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f",
        )];

        let mut verifier = MultiVerifier::new(configs, true).unwrap();
        verifier.update(data).unwrap();
        let result = verifier.finalize().unwrap();

        assert!(result);
    }

    #[test]
    fn test_multi_verifier_any_required() {
        let data = b"Hello, World!";
        let configs = vec![ChecksumConfig::new(
            HashAlgorithm::Sha256,
            "dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f",
        )];

        let mut verifier = MultiVerifier::new(configs, false).unwrap();
        verifier.update(data).unwrap();
        let result = verifier.finalize().unwrap();

        assert!(result);
    }

    #[test]
    fn test_convenience_functions() {
        let data = b"Hello, World!";
        let config = ChecksumConfig::new(
            HashAlgorithm::Sha256,
            "dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f",
        );

        // Test single checksum verification
        let result = verify_checksum(data, &config).unwrap();
        assert!(result);

        // Test multiple checksum verification
        let configs = vec![config.clone()];
        let result = verify_multiple_checksums(data, configs, true).unwrap();
        assert!(result);
    }

    #[test]
    fn test_parse_multiple_checksums() {
        let input = "sha256:abc123def456abc123def456abc123def456abc123def456abc123def4567890";

        let configs = parse_multiple_checksums(input).unwrap();
        assert_eq!(configs.len(), 1);
        assert_eq!(configs[0].algorithm, HashAlgorithm::Sha256);
        assert_eq!(
            configs[0].expected,
            "abc123def456abc123def456abc123def456abc123def456abc123def4567890"
        );
    }
}

#[test]
fn test_empty_data() {
    let data = b"";
    let config = ChecksumConfig::new(
        HashAlgorithm::Sha256,
        "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
    );

    let mut verifier = StreamVerifier::new(config).unwrap();
    verifier.update(data).unwrap();
    let result = verifier.finalize().unwrap();

    assert!(result);
}

#[test]
fn test_large_data() {
    let data: Vec<u8> = (0..10000).map(|i| (i % 256) as u8).collect();
    // Compute the actual SHA256 hash
    let mut hasher = Sha256Hasher::new();
    hasher.update(&data);
    let hash = hasher.finalize();
    let expected = hex::encode(hash);

    let config = ChecksumConfig::new(HashAlgorithm::Sha256, &expected);

    let mut verifier = StreamVerifier::new(config).unwrap();
    verifier.update(&data).unwrap();
    let result = verifier.finalize().unwrap();

    assert!(result);
}

#[test]
fn test_unsupported_algorithm() {
    let config = ChecksumConfig::new(HashAlgorithm::Sha512, "abc123");
    let result = StreamVerifier::new(config);
    assert!(result.is_err());
}