Created
February 19, 2018 16:21
-
-
Save suspectpart/c6eb39137e2f89941a3eedc5c37a5e66 to your computer and use it in GitHub Desktop.
Enforce nonsensical password strength rules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import unittest | |
"""Valid passwords | |
- are between 8 and 20 characters long | |
- contain at least one number | |
- have at least one uppercase and one lowercase character | |
- have a special char of !"§$%&/()=? | |
""" | |
def validate(password): | |
length_ok = len(password) in range(8, 21) | |
has_number = any(c.isdigit() for c in password) | |
has_lower = bool(set(password) & set(password.lower())) | |
has_upper = bool(set(password) & set(password.upper())) | |
has_special = bool(set(password) & set('!"§$%&/()=?')) | |
return length_ok and has_number and has_lower and has_upper and has_special | |
def validate_java(password): | |
if password == '': | |
return False | |
if len(password) < 8 or len(password) > 20: | |
return False | |
has_number = False | |
has_special = False | |
has_upper = False | |
has_lower = False | |
for char in password: | |
for special in '!"§$%&/()=?': | |
if special == char: | |
has_special = True | |
try: | |
int(char) | |
has_number = True | |
except: | |
pass | |
if ord(char) in range(65, 91): | |
has_upper = True | |
if ord(char) in range(97, 123): | |
has_lower = True | |
if has_upper == True and has_lower == True and has_special == True and has_number == True: | |
return True | |
return False | |
class ValidateTests(unittest.TestCase): | |
def test_empty(self): | |
self.assertFalse(validate('')) | |
def test_too_short(self): | |
self.assertFalse(validate('aAbB1!?')) | |
def test_too_long(self): | |
self.assertFalse(validate('aA1!?' * 4 + 'a')) | |
def test_no_number(self): | |
self.assertFalse(validate("aAbBcC!?")) | |
def test_no_upper(self): | |
self.assertFalse(validate("aabbzz()")) | |
def test_no_lower(self): | |
self.assertFalse(validate("%&AABBCC")) | |
def test_no_special(self): | |
self.assertFalse(validate("aAbmMC19")) | |
def test_valid(self): | |
self.assertTrue(validate("a1?mzuF4")) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment