This is a basic Application which demonstrates how Python can be used to validate a user's attempt to set a new password with some parameters.

The Python script will:

  • Set containts the user's password must fall within,
  • log the date/time the user submitted their password,
  • calculate the length of the new password the user has submitted,
  • report back to the user if their password is not within the constraints,
  • write to a log file each failed attempt to create a user password,
  • Report to the user if their submitted password is acceptable and write this to the log file.
  • Calculate the strength of a password if is using only letters or only numbers and report this to the user.

The following Python code is used:
                    
                                        
                        def main():

                        #assign CONSTANTS
                            MIN_PASSWORD_LENGTH = 6
                            MAX_PASSWORD_LENGTH = 14

                        #import datetime
                            import datetime

                        #INPUT password
                            password = input("PasswordChecker4 program developed by Lachlan Hunt\n")


                        #CALCULATE the length of the password
                            password_length = len(password)

                        #WHILE loop keeps  password accept/rejection messages based on MIN/MAX lengths
                            while (password_length < MIN_PASSWORD_LENGTH) or (password_length > MAX_PASSWORD_LENGTH):
                                if (password_length < MIN_PASSWORD_LENGTH):
                                    print("Your password length is", (password_length),"and is less than the required length for passwords.")

                        #Writing the error to the log file (password_log_lachlan.txt)
                                    writeLog = open("password_log_lachlan_hunt.txt", "a+")
                                    current_date_time = datetime.datetime.today()
                                    writeLog.write(str(current_date_time))
                                    writeLog.write(", password < 6")
                                    writeLog.write("\n")
                                    writeLog.close()

                                    password = input("Please enter a different password: ")
                                    password_length = len(password)

                        # ENDIF

                        #OUTPUT password accept/rejection messages based on MIN/MAX lengths
                                if (password_length > MAX_PASSWORD_LENGTH):
                                    print("Your password length is", (password_length),"and is greater than the required length for passwords.")

                        #Writing the error to the log file (password_log_lachlan.txt)
                                    writeLog = open("password_log_lachlan_hunt.txt", "a+")
                                    current_date_time = datetime.datetime.today()
                                    writeLog.write(str(current_date_time))
                                    writeLog.write(", password > 14")
                                    writeLog.write("\n")
                                    writeLog.close()

                                    password = input("Please enter a different password: ")
                                    password_length = len(password)

                        # ENDIF

                        #assign password strength values - only possible once password is set by user
                            onlyNumbers = password.isnumeric()
                            onlyLetters = password.isalpha()


                        #Display successful password assessment of strength and log file
                            if (onlyNumbers) or (onlyLetters):
                                message = "Your password length is {} and is acceptable, however it is weak (contains only numbers or only letters).\nPlease consider a combination of letters, numbers and other characters."
                                print(message.format(password_length))
                                #open and read log file
                                writeLog = open("password_log_lachlan_hunt.txt","r")
                                for line in writeLog:
                                    print(line)
                                writeLog.close()

                                list_data_1 = []

                                input_file = open("password_log_lachlan_hunt.txt","r")

                                for line in input_file:
                                    list_data_1.append(line)

                                input_file.close()

                                no_of_pw_small = 0 #create count for keywords starting at zero
                                for line in list_data_1:
                                    if "password < 6" in line: #define keyword
                                        no_of_pw_small = no_of_pw_small + 1 #add 1 to the count for every keyword found
                                #Display count of password attempts that are too small
                                print("Number of MIN_PASSWORD_LENGTH errors:" , (no_of_pw_small))

                                no_of_pw_large = 0 #create count for keywords starting at zero
                                for line in list_data_1:
                                    if "password > 14" in line: #define keyword
                                        no_of_pw_large = no_of_pw_large + 1 #add 1 to the count for every keyword found
                                # Display count of password attempts that are too long
                                print("Number of MAX_PASSWORD_LENGTH errors: ", (no_of_pw_large))

                            #If the password does not contain only numbers or only letters, run the following
                            else:
                                message = "Your password length is {}, making it acceptable and it is strong (contains a combination of letters, numbers and other characters)."
                                print(message.format(password_length))
                                writeLog = open("password_log_lachlan_hunt.txt","r")
                                for line in writeLog:
                                    print(line)
                                writeLog.close()

                                list_data_2 = []

                                input_file = open("password_log_lachlan_hunt.txt","r")

                                for line in input_file:
                                    list_data_2.append(line)

                                input_file.close()

                                no_of_pw_small = 0 #create count for keywords starting at zero
                                for line in list_data_2:
                                    if "password < 6" in line: #define keyword
                                        no_of_pw_small = no_of_pw_small + 1 #add 1 to the count for every keyword found
                                # Display count of password attempts that are too small
                                print("Number of MIN_PASSWORD_LENGTH errors:" , (no_of_pw_small))

                                no_of_pw_large = 0 #create count for keywords starting at zero
                                for line in list_data_2:
                                    if "password > 14" in line: #define keyword
                                        no_of_pw_large = no_of_pw_large + 1 #add 1 to the count for every keyword found
                                # Display count of password attempts that are too long
                                print("Number of MAX_PASSWORD_LENGTH errors:", (no_of_pw_large))

                        main()