Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """An underscore at the end of a variable denotes a Boolean data type"""
- import random, sys, os, time
- def menu():
- password=""
- points=0
- #Valid characters for the password
- allowed="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!$%^&*()-_=+"
- try:
- choice = int(input("Welcome to my password checker/generator. Press 1 to check password, 2 to generate password, or 3 to quit. \n"))
- if choice == 1:
- password = input("Please enter a password. \n")
- if len(password) < 8 or len(password) > 24:
- print("Invalid password length.")
- menu()
- points = checkPassword(password,points,allowed)
- printStrength(points)
- elif choice == 2:
- generatePassword(password,points,allowed)
- elif choice == 3:
- print("Goodbye")
- time.sleep(2)
- sys.exit()
- else:
- raise ValueError
- except ValueError:
- print("Invalid option.")
- menu()
- def checkPassword(password,points,allowed):
- top = "qwertyuiop" #Top row of keyboard
- middle = "asdfghjkl" #Middle row of keyboard
- bottom = "zxcvbnm" #Bottom row of keyboard
- upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- lowerCase = "abcdefghijklmnopqrstuvwxyz"
- digits = "0123456789"
- symbols = "!$%^&*()-_=+"
- for letter in password:
- if letter not in allowed:
- print("Invalid character:", letter + ".")
- time.sleep(2)
- menu()
- points = score(password,top,middle,bottom,upperCase,lowerCase,digits,symbols)
- return points
- def score(password,top,middle,bottom,upperCase,lowerCase,digits,symbols):
- #This section awards points for each type of character used, or removes points for duplicates of each character type.
- upper_ = False
- lowe_ = False
- digits_ = False
- symbols_ = False
- points = len(password)
- # Checks to see if the password contains uppercase or lowercase letters, symbols or digits.
- for character in password:
- if character in upperCase:
- upper_ = True
- elif character in lowerCase:
- lower_ = True
- elif character in digits:
- digits_ = True
- elif character in symbols:
- symbols_ = True
- if upper_ == True:
- points += 5
- if lower_ == True:
- points += 5
- if digits_ == True:
- points += 5
- if symbols_ == True:
- points += 5
- if upper_ and lowe_ == True and digits_ == False and symbols_ == False:
- points -= 5
- elif digits_ == True and symbols_ == False and lower_ == False and upper_ == False:
- points -= 5
- elif symbols_ == True and digits_ == False and lower_ == False and upper_ == False:
- points -= 5
- elif upper_ and lower_ and symbols_ and digits_ == True:
- points += 10
- password1 = password.lower()
- for ch in password:
- try:
- for key in top:
- if ch == key and password1[password1.index(ch)+1] == top[top.index(key)+1] and password1[password1.index(ch)+2] == top[top.index(key)+2]:
- points -= 5
- for key in middle:
- if ch == key and password1[password1.index(ch)+1] == middle[middle.index(key)+1] and password1[password1.index(ch)+2] == middle[middle.index(key)+2]:
- points -= 5
- for key in bottom:
- if ch == key and password1[password1.index(ch)+1] == bottom[bottom.index(key)+1] and password1[password1.index(ch)+2] == bottom[bottom.index(key)+2]:
- points -= 5
- except IndexError:
- pass
- return points
- def printStrength(points):
- if points < 0:
- print("Your password is weak, with a score of", str(points) + ".")
- elif points > 20:
- print("Your password is strong, with a score of", str(points) + ".")
- else:
- print("Your password is of medium strength, with a score of", str(points) + ".")
- time.sleep(2)
- menu()
- def generatePassword(password,points,allowed):
- length = random.randint(8, 12)
- password = ""
- for x in range(length):
- character = random.randint(33, 127)
- while chr(character) not in allowed:
- character = random.randint(33, 127)
- password += chr(character)
- points = checkPassword(password,points,allowed)
- while points < 20:
- checkPassword(password,points,allowed)
- print("The password generated is", password + ". It has a strength of", str(points) + ".")
- time.sleep(2)
- os.system('cls')
- menu()
- menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement