~/ learn/ comp-400/ cards/ Password selection strategies and proactive checking
1 of 8

Build a generator over a cryptographically secure source and a meets_policy() checker, then judge four candidate passwords against it. The generated password is genuinely unpredictable, so the exercise prints its VERDICT rather than the password — which is what makes the output deterministic without weakening the generator.

Build a generator over a cryptographically secure source and a meets_policy() checker, then judge four candidate passwords against it. The generated password is genuinely unpredictable, so the exercise prints its VERDICT rather than the password — which is what makes the output deterministic without weakening the generator.

Answer

import secrets, string CLASSES = [string.ascii_lowercase, string.ascii_uppercase, string.digits, '!#$%&*+-=?@^_~'] BANNED = ('password', 'letmein', 'qwerty', 'admin', 'welcome') def meets_policy(pw, length=16): if len(pw) < length: return False if any(word in pw.lower() for word in BANNED): return False return all(any(c in klass for c in pw) for klass in CLASSES) def generate(length=16): alphabet = ''.join(CLASSES) while True: pw = ''.join(secrets.choice(alphabet) for _ in range(length)) if meets_policy(pw, length): return pw pw = generate() print('generated length :', len(pw)) print('generated meets policy:', meets_policy(pw)) for candidate in ('Passw0rd!', 'MyPassword2026!!', 'correcthorsebattery', 'Tr0ub4dor&3xKq7Lm'): print(f'{candidate:<20}', meets_policy(candidate))

Stallings & Brown, Computer Security 5e, ch3 §3.2; NIST SP 800-63B (current guidance, EXT)

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/6675bc49-b05d-40ef-b086-e491f5e5dd66/flashcard utf-8 LF