Guest User

Untitled

a guest
Nov 30th, 2017
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. # Password generator and strength Checker
  2. from random import choice, randint
  3. from string import ascii_letters as letters
  4. programmRunning = True
  5.  
  6. symbols = '!$%^&*()_-=+'
  7. points = 0
  8. # 2 Strength checker
  9. def strengthCheck(x):
  10. global points
  11. points = 0
  12. hasall = 0
  13. # Points for length of password
  14. points = points + len(x)
  15.  
  16. # Upper case letters
  17. if (any(i.isupper() for i in x)) == True:
  18. points += 5
  19. hasall+= 1
  20.  
  21. # Lower case letters
  22. if (any(i.islower() for i in x)) == True:
  23. points += 5
  24. hasall+= 1
  25.  
  26. # Checking for symbols
  27. if any((c in symbols) for c in x):
  28. points += 5
  29. hasall += 1
  30.  
  31. # Checking for numbers
  32. if (any(i.isdigit() for i in x)) == True:
  33. points += 5
  34. hasall+= 1
  35.  
  36. if hasall == 4:
  37. points += 10
  38.  
  39. # Checking if only contains symbols
  40. if (any(i.islower() for i in x)) == False and (any(i.isupper() for i in x)) == False :
  41. points -= 5
  42.  
  43. # Checking if only contains numbers
  44. if (any(i.isalpha() for i in x)) == False:
  45. points -= 5
  46.  
  47. # Checking if only contains uppercase and lowercase
  48. if (any(i.isdigit() for i in x)) == False:
  49. points -= 5
  50.  
  51. # Check for any consecutive letters
  52. lines = ["qwertyuiop", "asdfghjkl", "zxcvbnm"]
  53. triples = []
  54. for line in lines:
  55. for i in range(len(line)-2):
  56. triples.append(line[i:i+3])
  57. # This lowers all string characters to lowercase
  58. loweredx = []
  59. for i in x:
  60. if i.isupper() == True:
  61. loweredx.append(i.lower())
  62. elif i.islower() == True:
  63. loweredx.append(i)
  64. elif i.isdigit() == True:
  65. loweredx.append(i)
  66. elif i in symbols:
  67. loweredx.append(i)
  68. x = ''.join(loweredx) # converts above list to string
  69. # search for each occurance of a triple
  70. triple_count = 0
  71. for i in triples:
  72. if i in x:
  73. triple_count += 1
  74. points = points - (triple_count*5)
  75.  
  76. if triple_count >0:
  77. print("number of triple occurances ", triple_count)
  78.  
  79. # Judging whether pass is W, M or s
  80. if points <=0:
  81. print("Password: %s is weak. %i points" % (x, points))
  82. elif points >20:
  83. print("Password: %s is strong. %i points" % (x, points))
  84. else:
  85. print("Password: %s is medium. %i points" % (x, points))
  86.  
  87.  
  88.  
  89. def inputPassword():
  90. checkPW = input("Please enter a password to check its strength: ")
  91. strengthCheck(checkPW)
  92.  
  93. # 3 Password Generator
  94. def generatePW():
  95. lengthofPW = randint(8,12)
  96. print("The Length of your password is: %i characters" % (lengthofPW))
  97. while points < 20:
  98. newPW = []
  99. for i in range(lengthofPW):
  100. choiceOfCharacter = randint(1,3)
  101. if choiceOfCharacter == 1:
  102. y = randint(0,9)
  103. newPW.append(str(y))
  104. elif choiceOfCharacter == 2:
  105. y = choice(letters)
  106. newPW.append(y)
  107. elif choiceOfCharacter == 3:
  108. randomSymbol = randint(0, 11)
  109. y = symbols[randomSymbol]
  110. newPW.append(y)
  111. newPW = ''.join(newPW)
  112. strengthCheck(newPW)
  113. while programmRunning == True:
  114. # 1 Menu
  115. print('''
  116. Password Strength Checker & Generator
  117. Please choose from one of the following options
  118.  
  119. 1. Check Password
  120. 2. Generate a Password
  121. 3. Quit
  122.  
  123. ''')
  124. userchoice = int(input("\n>o>"))
  125.  
  126. if userchoice == 1:
  127. inputPassword()
  128. elif userchoice == 2:
  129. points = 0
  130. generatePW()
  131. elif userchoice == 3:
  132. programmRunning = False
  133. print("You may now close the programme window.")
  134. else:
  135. print("You have made an invalid choice!")
Add Comment
Please, Sign In to add comment