password_policy 0.1.0

A comprehensive password generator and strength analyzer with configurable policies
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
use super::*;
use rand::prelude::*;
use std::collections::HashMap;

// Corporate password policy (common in enterprises)
#[derive(Debug, Clone)]
pub struct CorporatePolicy {
    pub min_length: usize,
    pub max_length: usize,
    pub require_uppercase: bool,
    pub require_lowercase: bool,
    pub require_numbers: bool,
    pub require_symbols: bool,
    pub min_unique_chars: usize,
    pub forbid_common_passwords: bool,
    pub forbid_keyboard_patterns: bool,
}

impl Default for CorporatePolicy {
    fn default() -> Self {
        Self {
            min_length: 12,
            max_length: 128,
            require_uppercase: true,
            require_lowercase: true,
            require_numbers: true,
            require_symbols: true,
            min_unique_chars: 8,
            forbid_common_passwords: true,
            forbid_keyboard_patterns: true,
        }
    }
}

impl PasswordPolicy for CorporatePolicy {
    fn meets_requirements(&self, password: &str) -> bool {
        let composition = self.analyze_composition(password);

        // Check length
        if password.len() < self.min_length || password.len() > self.max_length {
            return false;
        }

        // Check character requirements
        if self.require_uppercase && !composition.has_uppercase {
            return false;
        }
        if self.require_lowercase && !composition.has_lowercase {
            return false;
        }
        if self.require_numbers && !composition.has_numbers {
            return false;
        }
        if self.require_symbols && !composition.has_symbols {
            return false;
        }

        // Check unique characters
        if composition.unique_chars < self.min_unique_chars {
            return false;
        }

        // Check against common passwords
        if self.forbid_common_passwords {
            let password_lower = password.to_lowercase();
            for common in COMMON_PASSWORDS {
                if password_lower.contains(common) {
                    return false;
                }
            }
        }

        // Check for keyboard patterns
        if self.forbid_keyboard_patterns {
            let password_lower = password.to_lowercase();
            for pattern in KEYBOARD_PATTERNS {
                if password_lower.contains(pattern) {
                    return false;
                }
            }
        }

        true
    }

    fn generate(&self) -> String {
        let mut rng = thread_rng();
        let mut attempts = 0;
        const MAX_ATTEMPTS: u32 = 1000;

        while attempts < MAX_ATTEMPTS {
            let password = self.generate_candidate(&mut rng);
            if self.meets_requirements(&password) {
                return password;
            }
            attempts += 1;
        }

        // Fallback: generate a guaranteed compliant password
        self.generate_guaranteed_compliant(&mut rng)
    }

    fn get_requirements(&self) -> String {
        let mut requirements = Vec::new();

        requirements.push(format!(
            "Length: {}-{} characters",
            self.min_length, self.max_length
        ));

        if self.require_uppercase {
            requirements.push("At least one uppercase letter".to_string());
        }
        if self.require_lowercase {
            requirements.push("At least one lowercase letter".to_string());
        }
        if self.require_numbers {
            requirements.push("At least one number".to_string());
        }
        if self.require_symbols {
            requirements.push("At least one symbol".to_string());
        }

        requirements.push(format!(
            "At least {} unique characters",
            self.min_unique_chars
        ));

        if self.forbid_common_passwords {
            requirements.push("No common passwords or words".to_string());
        }
        if self.forbid_keyboard_patterns {
            requirements.push("No keyboard patterns".to_string());
        }

        requirements.join("\n")
    }

    fn analyze_strength(&self, password: &str) -> PasswordAnalysis {
        let composition = self.analyze_composition(password);
        let mut score = 0u32;
        let mut feedback = Vec::new();

        // Length scoring
        match password.len() {
            0..=7 => {
                feedback.push("Password is too short".to_string());
            }
            8..=11 => {
                score += 10;
                feedback.push("Consider using a longer password".to_string());
            }
            12..=15 => score += 20,
            16..=20 => score += 25,
            _ => score += 30,
        }

        // Character variety scoring
        let mut char_types_used = 0;
        if composition.has_lowercase {
            score += 5;
            char_types_used += 1;
        } else {
            feedback.push("Add lowercase letters".to_string());
        }

        if composition.has_uppercase {
            score += 5;
            char_types_used += 1;
        } else {
            feedback.push("Add uppercase letters".to_string());
        }

        if composition.has_numbers {
            score += 5;
            char_types_used += 1;
        } else {
            feedback.push("Add numbers".to_string());
        }

        if composition.has_symbols {
            score += 10;
            char_types_used += 1;
        } else {
            feedback.push("Add symbols for better security".to_string());
        }

        // Bonus for using all character types
        if char_types_used == 4 {
            score += 10;
        }

        // Unique characters bonus
        let uniqueness_ratio = composition.unique_chars as f64 / password.len() as f64;
        score += (uniqueness_ratio * 20.0) as u32;

        if composition.repeated_chars > password.len() / 3 {
            score = score.saturating_sub(10);
            feedback.push("Too many repeated characters".to_string());
        }

        // Check for common passwords
        let password_lower = password.to_lowercase();
        let mut has_common_password = false;
        for common in COMMON_PASSWORDS {
            if password_lower.contains(common) {
                score = score.saturating_sub(20);
                feedback.push("Avoid common passwords and words".to_string());
                has_common_password = true;
                break;
            }
        }

        // Check for keyboard patterns
        let mut has_keyboard_pattern = false;
        for pattern in KEYBOARD_PATTERNS {
            if password_lower.contains(pattern) {
                score = score.saturating_sub(15);
                feedback.push("Avoid keyboard patterns".to_string());
                has_keyboard_pattern = true;
                break;
            }
        }

        // Bonus for long passwords without common issues
        if password.len() > 20 && !has_common_password && !has_keyboard_pattern {
            score += 15;
        }
        if password.len() > 30 && !has_common_password && !has_keyboard_pattern {
            score += 10;
        }

        // Calculate entropy
        let charset_size = self.calculate_charset_size(&composition);
        let entropy = (password.len() as f64) * (charset_size as f64).log2();

        // Determine strength level - adjusted thresholds
        let strength = match score {
            0..=25 => StrengthLevel::VeryWeak,
            26..=45 => StrengthLevel::Weak,
            46..=65 => StrengthLevel::Fair,
            66..=80 => StrengthLevel::Good,
            81..=95 => StrengthLevel::Strong,
            _ => StrengthLevel::VeryStrong,
        };

        let time_to_crack = self.estimate_crack_time(entropy);

        if feedback.is_empty() {
            feedback.push("Excellent password!".to_string());
        }

        PasswordAnalysis {
            strength,
            score,
            entropy,
            time_to_crack,
            feedback,
            character_composition: composition,
        }
    }
}

impl CorporatePolicy {
    fn analyze_composition(&self, password: &str) -> CharacterComposition {
        let mut has_lowercase = false;
        let mut has_uppercase = false;
        let mut has_numbers = false;
        let mut has_symbols = false;
        let mut char_counts = HashMap::new();

        for ch in password.chars() {
            *char_counts.entry(ch).or_insert(0) += 1;

            if ch.is_ascii_lowercase() {
                has_lowercase = true;
            } else if ch.is_ascii_uppercase() {
                has_uppercase = true;
            } else if ch.is_ascii_digit() {
                has_numbers = true;
            } else {
                has_symbols = true;
            }
        }

        let unique_chars = char_counts.len();
        let repeated_chars = char_counts.values().filter(|&&count| count > 1).count();

        CharacterComposition {
            length: password.len(),
            has_lowercase,
            has_uppercase,
            has_numbers,
            has_symbols,
            unique_chars,
            repeated_chars,
        }
    }

    fn generate_candidate(&self, rng: &mut ThreadRng) -> String {
        let mut charset = String::new();

        if self.require_lowercase {
            charset.push_str(LOWERCASE);
        }
        if self.require_uppercase {
            charset.push_str(UPPERCASE);
        }
        if self.require_numbers {
            charset.push_str(NUMBERS);
        }
        if self.require_symbols {
            charset.push_str(SAFE_SYMBOLS);
        }

        if charset.is_empty() {
            charset = format!("{LOWERCASE}{UPPERCASE}{NUMBERS}{SAFE_SYMBOLS}");
        }

        let charset_chars: Vec<char> = charset.chars().collect();
        let length = rng.gen_range(self.min_length..=self.max_length.min(32));

        (0..length)
            .map(|_| charset_chars[rng.gen_range(0..charset_chars.len())])
            .collect()
    }

    fn generate_guaranteed_compliant(&self, rng: &mut ThreadRng) -> String {
        let mut password = String::new();

        // Ensure at least one of each required character type
        if self.require_lowercase {
            password.push(LOWERCASE.chars().choose(rng).unwrap());
        }
        if self.require_uppercase {
            password.push(UPPERCASE.chars().choose(rng).unwrap());
        }
        if self.require_numbers {
            password.push(NUMBERS.chars().choose(rng).unwrap());
        }
        if self.require_symbols {
            password.push(SAFE_SYMBOLS.chars().choose(rng).unwrap());
        }

        // Fill the rest with random characters
        let all_chars = format!("{LOWERCASE}{UPPERCASE}{NUMBERS}{SAFE_SYMBOLS}");
        let all_chars: Vec<char> = all_chars.chars().collect();

        while password.len() < self.min_length {
            password.push(all_chars[rng.gen_range(0..all_chars.len())]);
        }

        // Shuffle the password to avoid predictable patterns
        let mut chars: Vec<char> = password.chars().collect();
        chars.shuffle(rng);
        chars.into_iter().collect()
    }

    fn calculate_charset_size(&self, composition: &CharacterComposition) -> usize {
        let mut size = 0;

        if composition.has_lowercase {
            size += 26;
        }
        if composition.has_uppercase {
            size += 26;
        }
        if composition.has_numbers {
            size += 10;
        }
        if composition.has_symbols {
            size += 32; // Approximate number of common symbols
        }

        size.max(1)
    }

    fn estimate_crack_time(&self, entropy: f64) -> String {
        // Assume 1 billion guesses per second (modern hardware)
        let guesses_per_second = 1_000_000_000.0;
        let total_combinations = 2_f64.powf(entropy);
        let seconds_to_crack = total_combinations / (2.0 * guesses_per_second);

        if seconds_to_crack < 1.0 {
            "Instantly".to_string()
        } else if seconds_to_crack < 60.0 {
            format!("{seconds_to_crack:.0} seconds")
        } else if seconds_to_crack < 3600.0 {
            format!("{:.0} minutes", seconds_to_crack / 60.0)
        } else if seconds_to_crack < 86400.0 {
            format!("{:.0} hours", seconds_to_crack / 3600.0)
        } else if seconds_to_crack < 31536000.0 {
            format!("{:.0} days", seconds_to_crack / 86400.0)
        } else if seconds_to_crack < 31536000000.0 {
            format!("{:.0} years", seconds_to_crack / 31536000.0)
        } else {
            "Centuries".to_string()
        }
    }
}

// High-security policy for sensitive systems
#[derive(Debug, Clone)]
pub struct HighSecurityPolicy {
    pub min_length: usize,
    pub require_all_char_types: bool,
    pub min_entropy: f64,
}

impl Default for HighSecurityPolicy {
    fn default() -> Self {
        Self {
            min_length: 16,
            require_all_char_types: true,
            min_entropy: 60.0,
        }
    }
}

impl PasswordPolicy for HighSecurityPolicy {
    fn meets_requirements(&self, password: &str) -> bool {
        if password.len() < self.min_length {
            return false;
        }

        if self.require_all_char_types {
            let has_lower = password.chars().any(|c| c.is_ascii_lowercase());
            let has_upper = password.chars().any(|c| c.is_ascii_uppercase());
            let has_digit = password.chars().any(|c| c.is_ascii_digit());
            let has_symbol = password.chars().any(|c| !c.is_ascii_alphanumeric());

            if !(has_lower && has_upper && has_digit && has_symbol) {
                return false;
            }
        }

        // Calculate entropy
        let charset_size = self.estimate_charset_size(password);
        let entropy = (password.len() as f64) * (charset_size as f64).log2();

        entropy >= self.min_entropy
    }

    fn generate(&self) -> String {
        let mut rng = thread_rng();

        // Generate a high-entropy password
        loop {
            let mut password = String::new();

            // Ensure all character types
            password.push(LOWERCASE.chars().choose(&mut rng).unwrap());
            password.push(UPPERCASE.chars().choose(&mut rng).unwrap());
            password.push(NUMBERS.chars().choose(&mut rng).unwrap());
            password.push(SYMBOLS.chars().choose(&mut rng).unwrap());

            // Fill with random characters
            let all_chars = format!("{LOWERCASE}{UPPERCASE}{NUMBERS}{SAFE_SYMBOLS}");
            let all_chars: Vec<char> = all_chars.chars().collect();

            while password.len() < self.min_length {
                password.push(all_chars[rng.gen_range(0..all_chars.len())]);
            }

            // Shuffle
            let mut chars: Vec<char> = password.chars().collect();
            chars.shuffle(&mut rng);
            let password: String = chars.into_iter().collect();

            if self.meets_requirements(&password) {
                return password;
            }
        }
    }

    fn get_requirements(&self) -> String {
        format!(
            "• Minimum length: {} characters\n\
             • Must contain uppercase, lowercase, numbers, and symbols\n\
             • Minimum entropy: {:.1} bits",
            self.min_length, self.min_entropy
        )
    }

    fn analyze_strength(&self, password: &str) -> PasswordAnalysis {
        // Reuse corporate policy analysis but with stricter scoring
        let corporate = CorporatePolicy::default();
        let mut analysis = corporate.analyze_strength(password);

        // Adjust score based on high-security requirements
        if password.len() >= self.min_length {
            analysis.score += 10;
        } else {
            analysis.score = analysis.score.saturating_sub(20);
            analysis
                .feedback
                .push("Password too short for high security".to_string());
        }

        if analysis.entropy >= self.min_entropy {
            analysis.score += 15;
        } else {
            analysis.score = analysis.score.saturating_sub(15);
            analysis
                .feedback
                .push("Insufficient entropy for high security".to_string());
        }

        // Recalculate strength with adjusted score
        analysis.strength = match analysis.score {
            0..=30 => StrengthLevel::VeryWeak,
            31..=50 => StrengthLevel::Weak,
            51..=70 => StrengthLevel::Fair,
            71..=85 => StrengthLevel::Good,
            86..=95 => StrengthLevel::Strong,
            _ => StrengthLevel::VeryStrong,
        };

        analysis
    }
}

impl HighSecurityPolicy {
    fn estimate_charset_size(&self, password: &str) -> usize {
        let mut size = 0;

        if password.chars().any(|c| c.is_ascii_lowercase()) {
            size += 26;
        }
        if password.chars().any(|c| c.is_ascii_uppercase()) {
            size += 26;
        }
        if password.chars().any(|c| c.is_ascii_digit()) {
            size += 10;
        }
        if password.chars().any(|c| !c.is_ascii_alphanumeric()) {
            size += 32;
        }

        size.max(1)
    }
}

// Using enum instead of trait objects to avoid object safety issues
#[derive(Debug, Clone)]
pub enum PolicyType {
    Corporate(CorporatePolicy),
    HighSecurity(HighSecurityPolicy),
}

impl PolicyType {
    pub fn meets_requirements(&self, password: &str) -> bool {
        match self {
            PolicyType::Corporate(policy) => policy.meets_requirements(password),
            PolicyType::HighSecurity(policy) => policy.meets_requirements(password),
        }
    }

    pub fn generate(&self) -> String {
        match self {
            PolicyType::Corporate(policy) => policy.generate(),
            PolicyType::HighSecurity(policy) => policy.generate(),
        }
    }

    pub fn get_requirements(&self) -> String {
        match self {
            PolicyType::Corporate(policy) => policy.get_requirements(),
            PolicyType::HighSecurity(policy) => policy.get_requirements(),
        }
    }

    pub fn analyze_strength(&self, password: &str) -> PasswordAnalysis {
        match self {
            PolicyType::Corporate(policy) => policy.analyze_strength(password),
            PolicyType::HighSecurity(policy) => policy.analyze_strength(password),
        }
    }
}