Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. """An underscore at the end of a variable denotes a Boolean data type"""
  2.  
  3. import random, sys, os, time
  4.  
  5. def menu():
  6. password=""
  7. points=0
  8. #Valid characters for the password
  9. allowed="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!$%^&*()-_=+"
  10. try:
  11. choice = int(input("Welcome to my password checker/generator. Press 1 to check password, 2 to generate password, or 3 to quit. \n"))
  12. if choice == 1:
  13. password = input("Please enter a password. \n")
  14. if len(password) < 8 or len(password) > 24:
  15. print("Invalid password length.")
  16. menu()
  17. points = checkPassword(password,points,allowed)
  18. printStrength(points)
  19. elif choice == 2:
  20. generatePassword(password,points,allowed)
  21. elif choice == 3:
  22. print("Goodbye")
  23. time.sleep(2)
  24. sys.exit()
  25. else:
  26. raise ValueError
  27. except ValueError:
  28. print("Invalid option.")
  29. menu()
  30.  
  31.  
  32. def checkPassword(password,points,allowed):
  33. top = "qwertyuiop" #Top row of keyboard
  34. middle = "asdfghjkl" #Middle row of keyboard
  35. bottom = "zxcvbnm" #Bottom row of keyboard
  36. upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  37. lowerCase = "abcdefghijklmnopqrstuvwxyz"
  38. digits = "0123456789"
  39. symbols = "!$%^&*()-_=+"
  40. for letter in password:
  41. if letter not in allowed:
  42. print("Invalid character:", letter + ".")
  43. time.sleep(2)
  44. menu()
  45. points = score(password,top,middle,bottom,upperCase,lowerCase,digits,symbols)
  46. return points
  47.  
  48.  
  49. def score(password,top,middle,bottom,upperCase,lowerCase,digits,symbols):
  50. #This section awards points for each type of character used, or removes points for duplicates of each character type.
  51. upper_ = False
  52. lowe_ = False
  53. digits_ = False
  54. symbols_ = False
  55. points = len(password)
  56. # Checks to see if the password contains uppercase or lowercase letters, symbols or digits.
  57. for character in password:
  58. if character in upperCase:
  59. upper_ = True
  60. elif character in lowerCase:
  61. lower_ = True
  62. elif character in digits:
  63. digits_ = True
  64. elif character in symbols:
  65. symbols_ = True
  66. if upper_ == True:
  67. points += 5
  68. if lower_ == True:
  69. points += 5
  70. if digits_ == True:
  71. points += 5
  72. if symbols_ == True:
  73. points += 5
  74. if upper_ and lowe_ == True and digits_ == False and symbols_ == False:
  75. points -= 5
  76. elif digits_ == True and symbols_ == False and lower_ == False and upper_ == False:
  77. points -= 5
  78. elif symbols_ == True and digits_ == False and lower_ == False and upper_ == False:
  79. points -= 5
  80. elif upper_ and lower_ and symbols_ and digits_ == True:
  81. points += 10
  82. password1 = password.lower()
  83. for ch in password:
  84. try:
  85. for key in top:
  86. 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]:
  87. points -= 5
  88. for key in middle:
  89. 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]:
  90. points -= 5
  91. for key in bottom:
  92. 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]:
  93. points -= 5
  94. except IndexError:
  95. pass
  96. return points
  97.  
  98.  
  99. def printStrength(points):
  100. if points < 0:
  101. print("Your password is weak, with a score of", str(points) + ".")
  102. elif points > 20:
  103. print("Your password is strong, with a score of", str(points) + ".")
  104. else:
  105. print("Your password is of medium strength, with a score of", str(points) + ".")
  106. time.sleep(2)
  107. menu()
  108.  
  109.  
  110. def generatePassword(password,points,allowed):
  111. length = random.randint(8, 12)
  112. password = ""
  113. for x in range(length):
  114. character = random.randint(33, 127)
  115. while chr(character) not in allowed:
  116. character = random.randint(33, 127)
  117. password += chr(character)
  118. points = checkPassword(password,points,allowed)
  119. while points < 20:
  120. checkPassword(password,points,allowed)
  121. print("The password generated is", password + ". It has a strength of", str(points) + ".")
  122. time.sleep(2)
  123. os.system('cls')
  124. menu()
  125.  
  126.  
  127. menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement