In [4]:
#!/usr/bin/python3
In [68]:
def increment_password(password):
# Incrementing is just like counting with numbers: xx, xy, xz, ya, yb, and so on.
# Increase the rightmost letter one step; if it was z, it wraps around to a, and
# repeat with the next letter to the left until one doesn't wrap around.
if password[-1] == 'z':
password = increment_password(password[:-1]) + "a"
else:
password = password[:-1] + increase_char(password[-1])
return password
def increase_char(char):
if char == "z":
return "a"
return chr(ord(char)+1)
In [106]:
def valid_password(password):
if not pw_policy_1(password):
# print ("pw1 False")
return False
if not pw_policy_2(password):
# print ("pw2 False")
return False
if not pw_policy_3(password):
# print ("pw3 False")
return False
if not pw_policy_4(password):
# print ("pw4 False")
return False
if not pw_policy_5(password):
# print ("pw5 False")
return False
return True
def pw_policy_1(password):
# exactly eight letters
if (len(password) == 8):
return True
return False
def pw_policy_2(password):
# only lowercase letters
lowercase_letters = set('abcdefghijklmnopqrstuvwxyz')
for char in password:
if char not in lowercase_letters:
return False
return True
def pw_policy_3(password):
# one increasing straight of at least three letters
for i in range(6):
if (ord(password[i+2]) - ord(password[i]) == 2) and (ord(password[i+1]) - ord(password[i]) == 1):
return True
return False
def pw_policy_4(password):
# Passwords may not contain the letters i, o, or l
forbidden_letters = set('iol')
for char in password:
if char in forbidden_letters:
return False
return True
def pw_policy_5(password):
# must contain at least two different, non-overlapping pairs of letters
found = set()
for i in range(7):
if (ord(password[i]) == ord(password[i+1])):
found.add(password[i])
return (len(found) > 1)
In [117]:
input_pw = "cqjxjnds"
while not valid_password( input_pw ):
input_pw = increment_password(input_pw)
print (input_pw)
In [118]:
input_pw = increment_password(input_pw)
In [119]:
while not valid_password( input_pw ):
input_pw = increment_password(input_pw)
print (input_pw)
In [ ]: