import string # # module passwords # # Functions for testing the security/validity of a password. # # # isValid1: A function that checks whether a password is secure by # testing each character individually. # # Parameters: # password: string representing a password # # Returns: true if the following conditions are satisfied # at least one uppercase char # at least one lowercase char # at least one numeric digit # length >= 7 # def isValid1(password): # # Read each character, test whether current char satisfies one of the properties # Required properties: # at least one uppercase char # at least one lowercase char # at least one numeric digit # uppercase = False lowercase = False digit = False for ch in password: if (ch >= 'A') and (ch <= 'Z'): uppercase = True if (ch >= 'a') and (ch <= 'z'): lowercase = True if (ch >= '0') and (ch <= '9'): digit = True # # password is "secure" if all 3 properties are satisfied and has at least 7 chars # if (uppercase == True) and (lowercase == True) and (digit == True) and (len(password) >= 7): return True else: return False # # isValid2: A function that checks whether a password is secure by # testing each required property individually # # Parameters: # password: string representing a password # # Returns: true if the following conditions are satisfied # at least one uppercase char # at least one lowercase char # at least one numeric digit # length >= 7 # def isValid2(password): # # Search for upper case letter # uppercase = False for ch in "ABCDEFGHIJKLMNOPQRSTUVWXYZ": if (password.find(ch) >= 0): uppercase = True # # Search for lower case letter # lowercase = False for ch in string.lowercase: # string.lowercase is a string with all the if (password.find(ch) >= 0): # lowercase letters in it lowercase = True # # Search for a digit # digit = False for ch in string.digits: if (password.find(ch) >= 0): digit = True # # Password is "secure" if the following properties are satisfied: # at least one uppercase char # at least one lowercase char # at least one numeric digit # at least 7 characters # if (uppercase == True) and (lowercase == True) and (digit == True) and (len(password) >= 7): return True else: return False