Guest User

Untitled

a guest
Sep 14th, 2018
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.12 KB | None | 0 0
  1. #Password Checker and Generator
  2.  
  3. import random
  4.  
  5. def validate_password(password):
  6.     valid_characters = "abcdefghijklmnopqrstuvwxyz0123456789!£$%^&*()-_=+"
  7.     print("Validate password", password)
  8.  
  9.     if len(password) >= 8 and len(password) <=24:
  10.         for char in password:
  11.             if char.lower() not in valid_characters:
  12.                 return False
  13.     else:
  14.         return False
  15.  
  16.  
  17. def generate_password():
  18.     gen_password = ""
  19.     i = 0
  20.     gen_characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!$%^&*()-_=+"
  21.     password_length = random.randrange(8, 13)
  22.     while i < password_length:
  23.         random_value = random.choice(gen_characters)
  24.         gen_password += random_value
  25.         i += 1
  26.  
  27.         return gen_password
  28.  
  29. def check_password(password):
  30.     score = len(password)
  31.     rulematch = 0
  32.  
  33.     seqQwerty1 = "qwertyuiop"
  34.     seqQwerty2 = "asdfghjkl"
  35.     seqQwerty3 = "zxcvbnm"
  36.     allsymbols = "!£$%^&*()-_=+"
  37.  
  38.     countUpper = 0
  39.     countLower = 0
  40.     symbolsfound = False
  41.     countNumbers = 0
  42.  
  43.     for char in password:
  44.         if str(char).isupper(): countUpper += 1
  45.         if str(char).islower(): countLower += 1
  46.         if str(char).isdigit(): countNumbers += 1
  47.         if str(char) in allsymbols: symbolsfound = True
  48.  
  49.         if countUpper > 0:
  50.             score += 5
  51.             rulematch += 1
  52.         if countLower > 0:
  53.             score += 5
  54.             rulematch += 1
  55.         if countNumbers > 0:
  56.             score += 5
  57.             rulematch += 1
  58.         if symbolsfound == True:
  59.             score += 5
  60.             rulematch += 1
  61.         if rulematch == 4:
  62.             score += 10
  63.  
  64.  
  65.         if (countUpper > 0 or countLower > 0) and countNumbers == 0 and symbolsfound == False:
  66.             score = score - 5
  67.  
  68.         if countNumbers > 0 and countUpper == 0 and countLower == 0 and symbolsfound == False:
  69.             score = score + 5
  70.  
  71.         if countNumbers == 0 and countUpper == 0 and countLower == 0 and symbolsfound == True:
  72.             score = score - 5
  73.  
  74.  
  75.         for i in range(0, len(password) - 2):
  76.             testSeg = password[i:i+3]
  77.  
  78.             if testSeg.lower() in seqQwerty1:
  79.                 score -= 5
  80.             if testSeg.lower() in seqQwerty2:
  81.                 score -= 5
  82.             if testSeg.lower() in seqQwerty3:
  83.                 score -= 5
  84.  
  85.         return score
  86.  
  87. #Menu starts here
  88.  
  89. print("*****************************************************")
  90. print("* Welcome to Rowan's Password Generator and Checker *")
  91. print("* Pick one of the following:                        *")
  92. print("* 1) Check Password                                 *")
  93. print("* 2) Generate Password                              *")
  94. print("* 3) Quit the program                               *")
  95. print("*****************************************************")
  96. print("")
  97.  
  98. user_input = ""
  99.  
  100. while user_input != "3":
  101.     user_input = input("Enter a number: ")
  102.  
  103.     if user_input == "1":
  104.         print("Enter a password you want to check")
  105.         print("Your password must be between 8 and 24 characters in length")
  106.         print("It must also contain only alphabetic characters, digits or allowed symbols")
  107.         print("Allowed symbols are: !£$%^&*()-_=+")
  108.         password = input("Password: ")
  109.  
  110.         if validate_password(password) == False:
  111.             print("Your password is invalid. Try again.")
  112.         else:
  113.             score = check_password(password)
  114.             if score <= 0:
  115.                 print("Your password score is", score, "and is weak.")
  116.             elif score <= 20:
  117.                 print("Your password score is", score, "and is medium.")
  118.             else:
  119.                 print("Your password score is", score, "and is strong.")
  120.  
  121.     elif user_input == "2":
  122.  
  123.         score = 0
  124.         while score <= 20:
  125.             password = generate_password()
  126.             score = check_password(password)
  127.         print("The strong password generated is: ", password)
  128.         print("The score is: ", score)
  129.  
  130.     elif user_input == "3":
  131.         break
  132.  
  133.     else:
  134.         print("Error! Enter 1, 2 or 3!")
  135.  
  136. print("Goodbye!")
Add Comment
Please, Sign In to add comment