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.
# -*- encoding: UTF-8 -*- import re import getpass from random import randint class PasswordValidator(object): """ A password validator, used to validate a password for specific characters, numbers, and special characters """ validated = False def __init__(self, password): self.password = password def validate_length(self): # Check the length, gotta be over 8 if len(self.password) > 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 return "Password validated successfully." def encrypt(password): """ Encrypt the password simply example: bÃFuo <- says Test!23456 :type password: String """ arr = list(password) new_arr =  for i in arr: num = ord(i) + randint(0, 100) new_arr.append(chr(num)) return ''.join(new_arr) def obtain_password(): """ Get the password, simple and easy with getpass library """ return getpass.getpass('Change your password: ') def second_authentication(password_to_verify): """ Verify that the password is what the user wants it to be, by asking for it a second time. :type password_to_verify: String """ password = getpass.getpass('Enter password a second time: ') if password == password_to_verify: with open('passwords.txt', 'w') as f: f.write(encrypt(password_to_verify)) return "Password changed." else: return "Password not changed." if __name__ == '__main__': pass_verify = obtain_password() val = PasswordValidator(pass_verify) print val.validate_all() if val.validated is False: print "Password not validated." else: print second_authentication(pass_verify)
My question is, what can I do better to make this more efficient and more accurate?