Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Password Checker and Generator
- import random
- def validate_password(password):
- valid_characters = "abcdefghijklmnopqrstuvwxyz0123456789!£$%^&*()-_=+"
- print("Validate password", password)
- if len(password) >= 8 and len(password) <=24:
- for char in password:
- if char.lower() not in valid_characters:
- return False
- else:
- return False
- def generate_password():
- gen_password = ""
- i = 0
- gen_characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!$%^&*()-_=+"
- password_length = random.randrange(8, 13)
- while i < password_length:
- random_value = random.choice(gen_characters)
- gen_password += random_value
- i += 1
- return gen_password
- def check_password(password):
- score = len(password)
- rulematch = 0
- seqQwerty1 = "qwertyuiop"
- seqQwerty2 = "asdfghjkl"
- seqQwerty3 = "zxcvbnm"
- allsymbols = "!£$%^&*()-_=+"
- countUpper = 0
- countLower = 0
- symbolsfound = False
- countNumbers = 0
- for char in password:
- if str(char).isupper(): countUpper += 1
- if str(char).islower(): countLower += 1
- if str(char).isdigit(): countNumbers += 1
- if str(char) in allsymbols: symbolsfound = True
- if countUpper > 0:
- score += 5
- rulematch += 1
- if countLower > 0:
- score += 5
- rulematch += 1
- if countNumbers > 0:
- score += 5
- rulematch += 1
- if symbolsfound == True:
- score += 5
- rulematch += 1
- if rulematch == 4:
- score += 10
- if (countUpper > 0 or countLower > 0) and countNumbers == 0 and symbolsfound == False:
- score = score - 5
- if countNumbers > 0 and countUpper == 0 and countLower == 0 and symbolsfound == False:
- score = score + 5
- if countNumbers == 0 and countUpper == 0 and countLower == 0 and symbolsfound == True:
- score = score - 5
- for i in range(0, len(password) - 2):
- testSeg = password[i:i+3]
- if testSeg.lower() in seqQwerty1:
- score -= 5
- if testSeg.lower() in seqQwerty2:
- score -= 5
- if testSeg.lower() in seqQwerty3:
- score -= 5
- return score
- #Menu starts here
- print("*****************************************************")
- print("* Welcome to Rowan's Password Generator and Checker *")
- print("* Pick one of the following: *")
- print("* 1) Check Password *")
- print("* 2) Generate Password *")
- print("* 3) Quit the program *")
- print("*****************************************************")
- print("")
- user_input = ""
- while user_input != "3":
- user_input = input("Enter a number: ")
- if user_input == "1":
- print("Enter a password you want to check")
- print("Your password must be between 8 and 24 characters in length")
- print("It must also contain only alphabetic characters, digits or allowed symbols")
- print("Allowed symbols are: !£$%^&*()-_=+")
- password = input("Password: ")
- if validate_password(password) == False:
- print("Your password is invalid. Try again.")
- else:
- score = check_password(password)
- if score <= 0:
- print("Your password score is", score, "and is weak.")
- elif score <= 20:
- print("Your password score is", score, "and is medium.")
- else:
- print("Your password score is", score, "and is strong.")
- elif user_input == "2":
- score = 0
- while score <= 20:
- password = generate_password()
- score = check_password(password)
- print("The strong password generated is: ", password)
- print("The score is: ", score)
- elif user_input == "3":
- break
- else:
- print("Error! Enter 1, 2 or 3!")
- print("Goodbye!")
Add Comment
Please, Sign In to add comment