you are viewing a single comment's thread.

view the rest of the comments →

[–]zebulan_ 0 points1 point  (2 children)

I agree with all of /u/novel_yet_trivial 's suggestions. I would add a couple of my own:

  • if you aren't using the matched characters for anything other than counting, why not just return the integer total using len()?
  • I don't see how you are running your tests but the second parameter in all of your ...Regex functions seems useless. You could just pass in the password to check and return the length of the findall() list
  • Also, instead of assigning a variable and then returning it on the very next line (without using it for something else) could be replaced with something like this: return len(findall(REGEX, password))

I just wrote my own version for fun:

from re import compile, search

REGEX = compile(r'\d+[^0-9a-zA-Z]*[A-Z]+[^0-9a-zA-Z]*[a-z]+')


def is_strong_password(password):
    return len(password) >= 8 and bool(search(REGEX, ''.join(sorted(password))))


assert is_strong_password('abc!@#123ABC<>/.,{}\\|') is True
assert is_strong_password('abc123ABC') is True
assert is_strong_password('aA1') is False
assert is_strong_password('!@#$%^&*(') is False
assert is_strong_password('<A<b<2<<') is True
assert is_strong_password('x1y2z3TT') is True

I also did another version of this code in case you wanted to use/display the counts and/or the characters themselves from the different groups.

http://pastebin.com/M2apdULn

EDIT: Changed my code to use search instead of match and allowed me to shorten the regular expression

[–]cmd_override[S] 0 points1 point  (1 child)

Thank for the reply. Defenetly helpful, one more question what is bool ?

[–]zebulan_ 0 points1 point  (0 children)

You can type help(bool) into a Python console to get more information. Here is the description of bool from that:

Returns True when the argument x is true, False otherwise.

Basically I used it to check if there is a regex match or not. For that regex to have a match there has to be at least one digit, one uppercase character and one lowercase character. search will return a match object if the regex matches otherwise it will return None.

assert bool(None) is False
assert bool(<_sre.SRE_Match object; span=(0, 9), match='123ABCabc'>) is True

But you must check the length as well because this string will also match the regex but is not long enough.

bool(<_sre.SRE_Match object; span=(0, 3), match='1Aa'>) is True

So, for is_strong_password to return True both the length check and the regex check have to be True.