A program needs a password strength estimator but has several implementation restrictions:
- Nothing outside of Python's standard library may be used.
- A database of common passwords may not be consulted.
- The code must be simple, self-contained, and easy to verify.
As for the interface of the password strength estimator, several requirements are provided:
- Empty passwords must have a strength of zero.
- Passwords may only use ASCII whitespace, alphanumeric, and punctuation characters.
- Passwords with other characters must have a strength less than zero.
- Otherwise, strength must be estimated as the bit-space needed to store the password.
In keeping with the caveats listed above, the following function appears to do as required:
import math import string def estimate_quality(password): if not password: return 0 types = ([False, frozenset(string.whitespace)], [False, frozenset(string.ascii_lowercase)], [False, frozenset(string.ascii_uppercase)], [False, frozenset(string.digits)], [False, frozenset(string.punctuation)]) for character in password: for index, (flag, group) in enumerate(types): if character in group: types[index] = True break else: return -1 space = sum(len(group) for flag, group in types if flag) return math.ceil(math.log2(space) * len(password))
Is it possible to refactor the code to improve upon the third implementation restriction?