Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Type annotations #57

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions tests/matching_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,9 @@ def test_l33t_matching():
['', {}],
['abcdefgo123578!#$&*)]}>', {}],
['a', {}],
['4', {'a': ['4']}],
['4@', {'a': ['4', '@']}],
['4({60', {'a': ['4'], 'c': ['(', '{'], 'g': ['6'], 'o': ['0']}],
['4', {'a': {'4'}}],
['4@', {'a': {'4', '@'}}],
['4({60', {'a': {'4'}, 'c': {'(', '{'}, 'g': {'6'}, 'o': {'0'}}],
]:
msg = "reduces l33t table to only the substitutions that a password might be employing"
assert matching.relevant_l33t_subtable(pw, test_table) == expected, msg
Expand Down
28 changes: 11 additions & 17 deletions zxcvbn/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
from datetime import datetime
from datetime import datetime, timedelta
from decimal import Decimal
from typing import Any, Dict, Iterable, List, TypedDict

from . import matching, scoring, time_estimates, feedback
from . import feedback, matching, scoring, time_estimates, types

def zxcvbn(password, user_inputs=None):
try:
# Python 2 string types
basestring = (str, unicode)
except NameError:
# Python 3 string types
basestring = (str, bytes)

def zxcvbn(password: str, user_inputs: Iterable[Any] = None) -> types.Feedback:

if user_inputs is None:
user_inputs = []
Expand All @@ -17,22 +14,19 @@ def zxcvbn(password, user_inputs=None):

sanitized_inputs = []
for arg in user_inputs:
if not isinstance(arg, basestring):
arg = str(arg)
sanitized_inputs.append(arg.lower())
sanitized_inputs.append(str(arg).lower())

ranked_dictionaries = matching.RANKED_DICTIONARIES
ranked_dictionaries['user_inputs'] = matching.build_ranked_dict(sanitized_inputs)
ranked_dictionaries["user_inputs"] = matching.build_ranked_dict(sanitized_inputs)

matches = matching.omnimatch(password, ranked_dictionaries)
result = scoring.most_guessable_match_sequence(password, matches)
result['calc_time'] = datetime.now() - start
result["calc_time"] = datetime.now() - start

attack_times = time_estimates.estimate_attack_times(result['guesses'])
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'])
result["feedback"] = feedback.get_feedback(result["score"], result["sequence"])

return result
6 changes: 5 additions & 1 deletion zxcvbn/feedback.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from zxcvbn.types import Feedback, PasswordMatch
from zxcvbn.scoring import START_UPPER, ALL_UPPER
from gettext import gettext as _
from decimal import Decimal
from typing import Dict, List, Union

from .types import PasswordMatch, Feedback

def get_feedback(score, sequence):
def get_feedback(score: int, sequence: List[PasswordMatch]) -> Feedback:
if len(sequence) == 0:
return {
'warning': '',
Expand Down
Loading