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
|
import os
from datetime import datetime
from . import matching, scoring, time_estimates, feedback
def zxcvbn(password, user_inputs=None, max_length=72):
# Throw error if password exceeds max length
if len(password) > max_length:
raise ValueError(f"Password exceeds max length of {max_length} characters.")
try:
# Python 2 string types
basestring = (str, unicode)
except NameError:
# Python 3 string types
basestring = (str, bytes)
if user_inputs is None:
user_inputs = []
start = datetime.now()
sanitized_inputs = []
for arg in user_inputs:
if not isinstance(arg, basestring):
arg = str(arg)
sanitized_inputs.append(arg.lower())
matches = matching.omnimatch(password, user_inputs=sanitized_inputs)
result = scoring.most_guessable_match_sequence(password, matches)
result['calc_time'] = datetime.now() - start
attack_times = time_estimates.estimate_attack_times(result['guesses'])
for prop, val in attack_times.items():
result[prop] = val
result['feedback'] = feedback.get_feedback(result['score'],
result['sequence'])
return result
|