aria2-core 0.3.8

aria2-rust core download engine: multi-protocol segmented downloads, rate limiting, configuration, session persistence, and BitTorrent seeding
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
//! HTTP Digest Authentication (RFC 2617) implementation
//!
//! Provides parsing of WWW-Authenticate Digest challenges and building
//! of Authorization header values for HTTP Digest authentication.
//!
//! Note: The actual hash computation (MD5/SHA-256/SHA-512-256) delegates to
//! `crate::auth::digest_auth::DigestAuthProvider`, which provides correct
//! RFC 7616-compliant hash implementations. This module focuses on the
//! challenge parsing and response formatting aspects.

use std::collections::HashMap;
use std::fmt;

use crate::auth::digest_auth::{DigestAlgorithm, DigestAuthProvider};
use crate::error::Aria2Error;

/// Parsed WWW-Authenticate header for Digest auth challenge
///
/// Represents a server's Digest authentication challenge as defined in RFC 2617.
/// Example header: `Digest realm="aria2", nonce="abc123", qop="auth", algorithm="MD5"`
#[derive(Debug, Clone)]
pub struct DigestAuthChallenge {
    /// Authentication realm (typically a human-readable string describing the protected area)
    pub realm: String,
    /// Server-provided nonce value (unique per challenge)
    pub nonce: String,
    /// Quality of protection: "auth" or "auth-int" (optional)
    pub qop: Option<String>,
    /// Hash algorithm used (default "MD5")
    pub algorithm: String,
    /// Opaque value that client must return unchanged (optional)
    pub opaque: Option<String>,
    /// If true, the previous attempt failed due to stale nonce
    pub stale: bool,
}

impl fmt::Display for DigestAuthChallenge {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Digest realm=\"{}\", nonce=\"{}\", algorithm=\"{}\"",
            self.realm, self.nonce, self.algorithm
        )?;
        if let Some(ref qop) = self.qop {
            write!(f, ", qop=\"{}\"", qop)?;
        }
        if let Some(ref opaque) = self.opaque {
            write!(f, ", opaque=\"{}\"", opaque)?;
        }
        if self.stale {
            write!(f, ", stale=true")?;
        }
        Ok(())
    }
}

impl DigestAuthChallenge {
    /// Parse a `WWW-Authenticate` header value containing a Digest challenge.
    ///
    /// # Arguments
    /// * `header_value` - The full header value, e.g.
    ///   `Digest realm="aria2", nonce="abc123", qop="auth", algorithm="MD5", opaque="xyz", stale=false`
    ///
    /// # Returns
    /// A parsed `DigestAuthChallenge` on success, or an error message on failure.
    ///
    /// # Errors
    /// Returns an error if:
    /// - The header does not start with "Digest "
    /// - The required `nonce` parameter is missing
    ///
    /// # Example
    /// ```rust,ignore
    /// let challenge = DigestAuthChallenge::parse(
    ///     r#"Digest realm="test realm", nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", qop="auth""#
    /// ).unwrap();
    /// assert_eq!(challenge.realm, "test realm");
    /// assert_eq!(challenge.nonce, "dcd98b7102dd2f0e8b11d0f600bfb0c093");
    /// ```
    pub fn parse(header_value: &str) -> Result<Self, Aria2Error> {
        let mut header_parts = header_value
            .trim()
            .splitn(2, |character: char| character.is_ascii_whitespace());
        let scheme = header_parts.next().unwrap_or_default();
        if !scheme.eq_ignore_ascii_case("Digest") {
            return Err(Aria2Error::Parse(
                "Not a Digest challenge: missing 'Digest' scheme".to_string(),
            ));
        }
        let digest_part = header_parts.next().unwrap_or_default();

        let params = parse_digest_parameters(digest_part)?;

        // Validate required fields
        let nonce = params.get("nonce").cloned().ok_or_else(|| {
            Aria2Error::Parse("Missing required 'nonce' parameter in Digest challenge".to_string())
        })?;

        Ok(DigestAuthChallenge {
            realm: params.get("realm").cloned().unwrap_or_default(),
            nonce,
            qop: select_qop(params.get("qop").map(String::as_str))?,
            algorithm: normalize_algorithm(params.get("algorithm").map(String::as_str))?,
            opaque: params.get("opaque").cloned(),
            stale: params
                .get("stale")
                .map(|s| s.eq_ignore_ascii_case("true"))
                .unwrap_or(false),
        })
    }
}

fn parse_digest_parameters(input: &str) -> Result<HashMap<String, String>, Aria2Error> {
    let bytes = input.as_bytes();
    let mut index = 0;
    let mut params = HashMap::new();

    while index < bytes.len() {
        while index < bytes.len() && (bytes[index].is_ascii_whitespace() || bytes[index] == b',') {
            index += 1;
        }
        if index == bytes.len() {
            break;
        }

        let key_start = index;
        while index < bytes.len() && bytes[index] != b'=' && bytes[index] != b',' {
            index += 1;
        }
        let key = input[key_start..index].trim();
        if key.is_empty() || index == bytes.len() || bytes[index] != b'=' {
            return Err(Aria2Error::Parse(
                "Invalid Digest challenge parameter".to_string(),
            ));
        }
        index += 1;
        while index < bytes.len() && bytes[index].is_ascii_whitespace() {
            index += 1;
        }

        let value = if bytes.get(index) == Some(&b'"') {
            index += 1;
            let mut value = String::new();
            let mut closed = false;
            while index < bytes.len() {
                match bytes[index] {
                    b'\\' if index + 1 < bytes.len() => {
                        value.push(bytes[index + 1] as char);
                        index += 2;
                    }
                    b'"' => {
                        index += 1;
                        closed = true;
                        break;
                    }
                    byte => {
                        value.push(byte as char);
                        index += 1;
                    }
                }
            }
            if !closed {
                return Err(Aria2Error::Parse(
                    "Unterminated quoted Digest challenge parameter".to_string(),
                ));
            }
            value
        } else {
            let value_start = index;
            while index < bytes.len() && bytes[index] != b',' {
                index += 1;
            }
            input[value_start..index].trim().to_string()
        };

        params.insert(key.to_ascii_lowercase(), value);
        while index < bytes.len() && bytes[index].is_ascii_whitespace() {
            index += 1;
        }
        if index < bytes.len() && bytes[index] != b',' {
            return Err(Aria2Error::Parse(
                "Invalid Digest challenge parameter separator".to_string(),
            ));
        }
    }

    Ok(params)
}

fn select_qop(value: Option<&str>) -> Result<Option<String>, Aria2Error> {
    let Some(value) = value else {
        return Ok(None);
    };

    value
        .split(',')
        .map(str::trim)
        .find(|qop| qop.eq_ignore_ascii_case("auth"))
        .or_else(|| {
            value
                .split(',')
                .map(str::trim)
                .find(|qop| qop.eq_ignore_ascii_case("auth-int"))
        })
        .map(|qop| qop.to_ascii_lowercase())
        .ok_or_else(|| Aria2Error::Parse(format!("Unsupported Digest qop: {value}")))
        .map(Some)
}

fn normalize_algorithm(value: Option<&str>) -> Result<String, Aria2Error> {
    let algorithm = value.unwrap_or("MD5");
    let canonical = match algorithm.to_ascii_uppercase().as_str() {
        "MD5" => "MD5",
        "MD5-SESS" => "MD5-sess",
        "SHA-256" => "SHA-256",
        "SHA-256-SESS" => "SHA-256-sess",
        "SHA-512-256" => "SHA-512-256",
        "SHA-512-256-SESS" => "SHA-512-256-sess",
        _ => {
            return Err(Aria2Error::Parse(format!(
                "Unsupported Digest algorithm: {algorithm}"
            )));
        }
    };
    Ok(canonical.to_string())
}

/// Built Digest authentication response ready for inclusion in an Authorization header
///
/// Contains all computed values needed to construct the Authorization header value.
#[derive(Debug, Clone)]
pub struct DigestAuthResponse {
    /// Username being authenticated
    pub username: String,
    /// Realm from the server's challenge
    pub realm: String,
    /// Nonce from the server's challenge
    pub nonce: String,
    /// Request URI (path portion)
    pub uri: String,
    /// Quality of protection (from challenge)
    pub qop: Option<String>,
    /// Nonce count (hex, 8 digits)
    pub nc: u32,
    /// Client-generated nonce (hex string)
    pub cnonce: String,
    /// Computed response hash
    pub response: String,
    /// Algorithm used (from challenge)
    pub algorithm: String,
    /// Opaque value from challenge (must be returned unchanged)
    pub opaque: Option<String>,
}

impl DigestAuthResponse {
    /// Build the complete `Authorization` header value string.
    ///
    /// Returns the full header value in the format:
    /// ```text
    /// Digest username="...", realm="...", nonce="...", uri="...",
    ///        nc=XXXXXXXX, cnonce="...", qop="...", response="...",
    ///        algorithm="...", opaque="..."
    /// ```
    pub fn to_header_value(&self) -> String {
        let mut fields = vec![
            format!(r#"username="{}""#, escape_quoted(&self.username)),
            format!(r#"realm="{}""#, escape_quoted(&self.realm)),
            format!(r#"nonce="{}""#, escape_quoted(&self.nonce)),
            format!(r#"uri="{}""#, escape_quoted(&self.uri)),
            format!(r#"response="{}""#, self.response),
            format!("algorithm={}", self.algorithm),
        ];
        if let Some(qop) = self.qop.as_deref() {
            fields.push(format!("qop={qop}"));
            fields.push(format!("nc={:08x}", self.nc));
            fields.push(format!(r#"cnonce="{}""#, escape_quoted(&self.cnonce)));
        } else if self.algorithm.eq_ignore_ascii_case("MD5-sess")
            || self.algorithm.eq_ignore_ascii_case("SHA-256-sess")
            || self.algorithm.eq_ignore_ascii_case("SHA-512-256-sess")
        {
            fields.push(format!(r#"cnonce="{}""#, escape_quoted(&self.cnonce)));
        }
        if let Some(opaque) = self.opaque.as_deref() {
            fields.push(format!(r#"opaque="{}""#, escape_quoted(opaque)));
        }
        format!("Digest {}", fields.join(", "))
    }

    /// Compute a Digest authentication response per RFC 2617 section 3.2.2.1.
    ///
    /// This method computes all necessary hashes and builds a complete response
    /// that can be serialized via [`to_header_value`](Self::to_header_value).
    ///
    /// # Algorithm (RFC 2617):
    /// ```text
    /// HA1 = MD5(username:realm:password)
    /// HA2 = MD5(method:uri)
    /// if qop is set:
    ///     response = MD5(HA1:nonce:nc:cnonce:qop:HA2)
    /// else:
    ///     response = MD5(HA1:nonce:HA2)
    /// ```
    ///
    /// # Arguments
    /// * `username` - The username for authentication
    /// * `password` - The user's password (plaintext)
    /// * `method` - HTTP method (GET, POST, etc.)
    /// * `uri` - The request URI path
    /// * `challenge` - The parsed server challenge
    /// * `nc` - Nonce count (incremented per request with same nonce)
    ///
    /// # Returns
    /// A fully constructed `DigestAuthResponse`.
    pub fn compute(
        username: &str,
        password: &str,
        method: &str,
        uri: &str,
        challenge: &DigestAuthChallenge,
        nc: u32,
    ) -> Self {
        // Determine the hash algorithm from the challenge
        let algorithm_name = challenge.algorithm.to_ascii_uppercase();
        let algorithm = match algorithm_name.as_str() {
            "SHA-256" | "SHA-256-SESS" => DigestAlgorithm::Sha256,
            "SHA-512-256" | "SHA-512-256-SESS" => DigestAlgorithm::Sha512_256,
            _ => DigestAlgorithm::Md5,
        };
        let is_session_algorithm = algorithm_name.ends_with("-SESS");

        // Create a DigestAuthProvider to leverage its correct hash implementations
        let provider =
            DigestAuthProvider::new(username.to_string(), password.to_string(), Some(algorithm));

        // Compute HA2 = H(method:uri)
        // Generate random cnonce (client nonce) using crypto-quality randomness
        let cnonce = format!("{:016x}", rand::random::<u64>());
        let qop = select_qop(challenge.qop.as_deref()).unwrap_or(None);

        // RFC 7616 uses a second HA1 derivation for -sess algorithms.
        let initial_ha1 = provider.compute_ha1(&challenge.realm);
        let ha1 = if is_session_algorithm {
            provider.hash_kd(&initial_ha1, &format!("{}:{}", challenge.nonce, cnonce))
        } else {
            initial_ha1
        };

        let ha2 = provider.compute_ha2(method, uri, qop.as_deref(), None);

        let nc_str = format!("{:08x}", nc);

        // Compute final response = KD(HA1, nonce:nc:cnonce:qop:HA2) or KD(HA1, nonce:HA2)
        let response = provider.compute_response(
            &ha1,
            &challenge.nonce,
            &nc_str,
            &cnonce,
            qop.as_deref(),
            &ha2,
        );

        DigestAuthResponse {
            username: username.to_string(),
            realm: challenge.realm.clone(),
            nonce: challenge.nonce.clone(),
            uri: uri.to_string(),
            qop,
            nc,
            cnonce,
            response,
            algorithm: challenge.algorithm.clone(),
            opaque: challenge.opaque.clone(),
        }
    }
}

fn escape_quoted(value: &str) -> String {
    value.replace('\\', "\\\\").replace('"', "\\\"")
}

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

    // --- DigestAuthChallenge tests ---

    #[test]
    fn test_digest_challenge_parse_basic() {
        let header = r#"Digest realm="test realm", nonce="abc123def456""#;
        let challenge = DigestAuthChallenge::parse(header).unwrap();

        assert_eq!(challenge.realm, "test realm");
        assert_eq!(challenge.nonce, "abc123def456");
        assert_eq!(challenge.algorithm, "MD5"); // default
        assert!(challenge.qop.is_none());
        assert!(challenge.opaque.is_none());
        assert!(!challenge.stale);
    }

    #[test]
    fn test_digest_challenge_parse_all_fields() {
        let header = r#"Digest realm="aria2 download", nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", qop="auth", algorithm="MD5", opaque="5ccc069c403ebaf9f0171e9517bf40c9", stale=true"#;
        let challenge = DigestAuthChallenge::parse(header).unwrap();

        assert_eq!(challenge.realm, "aria2 download");
        assert_eq!(challenge.nonce, "dcd98b7102dd2f0e8b11d0f600bfb0c093");
        assert_eq!(challenge.qop.as_deref(), Some("auth"));
        assert_eq!(challenge.algorithm, "MD5");
        assert_eq!(
            challenge.opaque.as_deref(),
            Some("5ccc069c403ebaf9f0171e9517bf40c9")
        );
        assert!(challenge.stale);
    }

    #[test]
    fn test_digest_challenge_missing_nonce_returns_error() {
        let header = r#"Digest realm="only realm""#;
        let result = DigestAuthChallenge::parse(header);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("nonce"));
    }

    #[test]
    fn test_digest_challenge_not_digest_prefix_returns_error() {
        let header = r#"Basic realm="test""#;
        let result = DigestAuthChallenge::parse(header);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Digest "));
    }

    #[test]
    fn test_digest_challenge_case_insensitive_keys() {
        // Keys should be case-insensitive per RFC
        let header = r#"Digest REALM="TestRealm", NONCE="myNonce", ALGORITHM="SHA-256""#;
        let challenge = DigestAuthChallenge::parse(header).unwrap();
        assert_eq!(challenge.realm, "TestRealm");
        assert_eq!(challenge.nonce, "myNonce");
        assert_eq!(challenge.algorithm, "SHA-256");
    }

    #[test]
    fn test_digest_challenge_parse_case_insensitive_scheme_and_quoted_commas() {
        let challenge = DigestAuthChallenge::parse(
            r#"dIgEsT realm="download, private", nonce="n\"1", qop="auth-int, auth", algorithm=MD5"#,
        )
        .unwrap();

        assert_eq!(challenge.realm, "download, private");
        assert_eq!(challenge.nonce, "n\"1");
        assert_eq!(challenge.qop.as_deref(), Some("auth"));
    }

    #[test]
    fn test_digest_challenge_rejects_unknown_algorithm() {
        let result =
            DigestAuthChallenge::parse(r#"Digest realm="test", nonce="nonce", algorithm=SHA-1"#);
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Unsupported Digest algorithm")
        );
    }

    // --- DigestAuthResponse tests ---

    #[test]
    fn test_digest_response_compute_and_format() {
        let challenge = DigestAuthChallenge::parse(
            r#"Digest realm="test@host.com", nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", qop="auth", opaque="someopaque""#
        ).unwrap();

        let response = DigestAuthResponse::compute(
            "Mufasa",
            "Circle Of Life",
            "GET",
            "/dir/index.html",
            &challenge,
            1,
        );

        assert_eq!(response.username, "Mufasa");
        assert_eq!(response.realm, "test@host.com");
        assert_eq!(response.uri, "/dir/index.html");
        assert_eq!(response.nc, 1);
        assert_eq!(response.qop.as_deref(), Some("auth"));
        assert_eq!(response.algorithm, "MD5");
        assert_eq!(response.opaque.as_deref(), Some("someopaque"));

        // Verify the header value contains expected components
        let header_val = response.to_header_value();
        assert!(header_val.starts_with("Digest "));
        assert!(header_val.contains(r#"username="Mufasa""#));
        assert!(header_val.contains(r#"realm="test@host.com""#));
        assert!(header_val.contains("nc=00000001"));
        assert!(header_val.contains(r#"response=""#));
        assert!(!response.cnonce.is_empty());
    }

    #[test]
    fn test_digest_response_without_qop() {
        let challenge =
            DigestAuthChallenge::parse(r#"Digest realm="simple", nonce="simpleNonce123""#).unwrap();

        let response =
            DigestAuthResponse::compute("user", "pass", "POST", "/api/data", &challenge, 1);

        assert!(response.qop.is_none());
        let header_val = response.to_header_value();
        assert!(!header_val.contains("qop="));
        assert!(header_val.contains("algorithm=MD5"));
        assert!(!header_val.contains("cnonce="));
    }

    #[test]
    fn test_digest_full_flow_roundtrip() {
        // Simulate full flow: receive challenge -> build response -> verify format

        // Step 1: Server sends WWW-Authenticate header
        let www_authenticate = r#"Digest realm="WallyWorld", nonce="OA=MPOPQKX/RI=SOXPVDFKB,URI=/download/file.torrent", qop="auth", algorithm="MD5", opaque="FQwERTYuiop123""#;

        // Step 2: Client parses the challenge
        let challenge = DigestAuthChallenge::parse(www_authenticate).unwrap();
        assert_eq!(challenge.realm, "WallyWorld");

        // Step 3: Client computes response
        let auth_response = DigestAuthResponse::compute(
            "admin",
            "secret123",
            "GET",
            "/download/file.torrent",
            &challenge,
            1,
        );

        // Step 4: Build Authorization header value
        let authorization = auth_response.to_header_value();

        // Verify roundtrip integrity
        assert!(authorization.starts_with("Digest "));
        assert!(authorization.contains(r#"username="admin""#));
        assert!(authorization.contains(r#"realm="WallyWorld""#));
        assert!(authorization.contains(&format!("nonce=\"{}\"", challenge.nonce)));
        assert!(authorization.contains(r#"uri="/download/file.torrent""#));
        assert!(authorization.contains("nc=00000001"));
        assert!(authorization.contains("qop=auth"));
        assert!(authorization.contains(r#"opaque="FQwERTYuiop123""#));

        // Verify we can re-parse the generated header structure (sanity check)
        assert!(authorization.len() > 50); // Should be substantial
        assert!(!authorization.contains("\n")); // Single line header
    }

    #[test]
    fn test_digest_challenge_display_format() {
        let challenge = DigestAuthChallenge {
            realm: "MyRealm".into(),
            nonce: "abc123".into(),
            qop: Some("auth".into()),
            algorithm: "MD5".into(),
            opaque: Some("opaqueVal".into()),
            stale: false,
        };

        let display = format!("{}", challenge);
        assert!(display.contains("Digest "));
        assert!(display.contains(r#"realm="MyRealm""#));
        assert!(display.contains(r#"nonce="abc123""#));
        assert!(display.contains(r#"qop="auth""#));
        assert!(display.contains(r#"opaque="opaqueVal""#));
    }

    #[test]
    fn test_digest_nonce_count_increments_correctly() {
        let challenge =
            DigestAuthChallenge::parse(r#"Digest realm="test", nonce="nonce123", qop="auth""#)
                .unwrap();

        let resp1 = DigestAuthResponse::compute("u", "p", "GET", "/", &challenge, 1);
        let resp2 = DigestAuthResponse::compute("u", "p", "GET", "/", &challenge, 2);

        let h1 = resp1.to_header_value();
        let h2 = resp2.to_header_value();

        assert!(h1.contains("nc=00000001"));
        assert!(h2.contains("nc=00000002"));

        // Each request should have different cnonce and response
        assert_ne!(resp1.cnonce, resp2.cnonce);
        assert_ne!(resp1.response, resp2.response);
    }

    /// Verify RFC 2617 known test vector.
    ///
    /// Reference values computed from:
    ///   HA1 = MD5("Mufasa:testrealm@host.com:Circle Of Life") = "939e7578ed9e3c518a452acee763bce9"
    ///   HA2 = MD5("GET:/dir/index.html") = "39aff3a2bab6126f332b942af96d3366"
    ///   response = MD5(HA1 ":" nonce ":" nc ":" cnonce ":" qop ":" HA2)
    ///            = MD5("939e7578ed9e3c518a452acee763bce9:dcd98b7102dd2f0e8b11d0f600bfb0c093:00000001:0a4f113b:auth:39aff3a2bab6126f332b942af96d3366")
    ///            = "6629fae49393a05397450978507c4ef1"
    #[test]
    fn test_rfc2617_known_vector_md5() {
        // Verify HA1 computation
        let provider = DigestAuthProvider::new(
            "Mufasa".to_string(),
            "Circle Of Life".to_string(),
            Some(DigestAlgorithm::Md5),
        );
        let ha1 = provider.compute_ha1("testrealm@host.com");
        assert_eq!(
            ha1, "939e7578ed9e3c518a452acee763bce9",
            "HA1 should match RFC 2617 test vector"
        );

        // Verify HA2 computation
        let ha2 = provider.compute_ha2("GET", "/dir/index.html", None, None);
        assert_eq!(
            ha2, "39aff3a2bab6126f332b942af96d3366",
            "HA2 should match RFC 2617 test vector"
        );

        // Verify response computation with known cnonce
        let response = provider.compute_response(
            &ha1,
            "dcd98b7102dd2f0e8b11d0f600bfb0c093",
            "00000001",
            "0a4f113b",
            Some("auth"),
            &ha2,
        );
        assert_eq!(
            response, "6629fae49393a05397450978507c4ef1",
            "Response should match RFC 2617 test vector"
        );
    }

    /// Verify SHA-256 variant produces correct length hash (64 hex chars).
    #[test]
    fn test_sha256_digest_produces_64_char_hex() {
        let provider = DigestAuthProvider::new(
            "user".to_string(),
            "pass".to_string(),
            Some(DigestAlgorithm::Sha256),
        );

        let ha1 = provider.compute_ha1("testrealm");
        assert_eq!(ha1.len(), 64, "SHA-256 should produce 64 hex chars");

        let ha2 = provider.compute_ha2("GET", "/test", None, None);
        assert_eq!(ha2.len(), 64, "SHA-256 should produce 64 hex chars");
    }
}