# Change password after being validated

I'm doing Python programming challenges and have come across one where the object of the challenge was to create a script to change passwords. The script must validate that the password contains, one upper, one lowercase, and one number. After everything has been validated the script should save the password to a text file. I added special characters and some basic encryption to the program to make it a little more interesting.

Source:

# -*- encoding: UTF-8 -*-

import re
import getpass
from random import randint

specific characters, numbers, and special characters """

validated = False

def validate_length(self):  # Check the length, gotta be over 8
return True
else:
return False

def validate_upper_case(self):  # Check for upper case characters
return re.search("[A-Z]", self.password) is not None

def validate_lower_case(self):  # Check for lower case characters
return re.search("[a-z]", self.password) is not None

def validate_integers(self):  # Check for integers
return re.search("[0-9]", self.password) is not None

def validate_spec_chars(self):  # Check for special characters
return re.search("[!$&*]", self.password) is not None def validate_all(self): """ Validate everything. """ length = self.validate_length() upper = self.validate_upper_case() lower = self.validate_lower_case() digits = self.validate_integers() specs = self.validate_spec_chars() if specs is False: return "Failed special characters validation. Allowed special chars: (!$ & *)"
elif digits is False:
return "Failed integer validation. Allowed integers: (0 - 9)"
elif lower is False:
return "Failed lower case validation. (a - z)"
elif upper is False:
return "Failed uppercase validation. (A - Z)"
elif length is False:
return "Failed length validation. Allowed length: (> 8)"
else:
self.validated = True

""" Encrypt the password simply example: bÃ­Fuo <- says Test!23456
new_arr = []

for i in arr:
num = ord(i) + randint(0, 100)
new_arr.append(chr(num))

return ''.join(new_arr)

""" Get the password, simple and easy with getpass library """

""" Verify that the password is what the user wants it to be,
by asking for it a second time.
else:

if __name__ == '__main__':
print val.validate_all()
if val.validated is False:
else:
print second_authentication(pass_verify)

My question is, what can I do better to make this more efficient and more accurate?

• How are you going to read the password back when you need it if you encrypt it with random numbers you do not store? How can a test be meaningful without RNG seeding if randomness if used within the function? – Caridorc Aug 30 '16 at 21:30
• @Caridorc that's a valid point, but the object was to just encrypt it, I hadn't gone through with reading it back and storing it and everything yet. My goal was just to get the program running. – YoYoYo I'm Awesome Aug 31 '16 at 14:44
• Encryption does not make sense without decription, if something you "encrypt" is not recoverable anymore, than "encryption" becomes synonim with "deletion" – Caridorc Aug 31 '16 at 14:46
• @Caridorc That's a valid point, and I see what you're saying. I'm going to rewrite this and add some more features into it such as, username, password has to be changed after so many attempts, decryption, etc. Thank you – YoYoYo I'm Awesome Aug 31 '16 at 14:48
• You are welcome, thank to you for using this site, I will look forward to the improved version :) – Caridorc Aug 31 '16 at 14:50

It would be more intuitive if your validate_all method was just called validate.

In quite a few places you are doing something like this in validate_length:

def validate_length(self):  # Check the length, gotta be over 8
return True
else:
return False

Just return the right value right away:

def validate_length(self):  # Check the length, gotta be over 8

It would also make more sense if the Validator.validate method just returned True or False instead of setting a class variable and returning a string detailing the status:

return all(validator(password) for validator in [self.validate_length, self.validate_upper_case, ...])

I would also make failing a testcase raise an exception and catch it in validate:

class ValidatorError(Exception):
pass
...

def __init__(self):
self.validators = [self.validate_length, self.validate_upper_case, ...]

...

"""Check the length, gotta be over 8"""
return True
raise ValidatorError("Failed length validation. Allowed length: (> 8)")

try:
return all(validator(password) for validator in self.validators)
except ValidatorError as e:
print e
return False

...
if __name__ == '__main__':