Advertisement
Guest User

Help please!

a guest
Feb 25th, 2021
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. import random
  2. import time
  3.  
  4. LOWERCASE_CHARS = tuple(map(chr, range(ord('a'), ord('z')+1)))
  5. UPPERCASE_CHARS = tuple(map(chr, range(ord('A'), ord('Z')+1)))
  6. DIGITS = tuple(map(str, range(0, 10)))
  7. SPECIALS = ('!', '@', '#', '$', '%', '^', '&', '*')
  8.  
  9. SEQUENCE = (LOWERCASE_CHARS,
  10. UPPERCASE_CHARS,
  11. DIGITS,
  12. SPECIALS,
  13. )
  14.  
  15. def generate_random_password(total, sequences):
  16. r = _generate_random_number_for_each_sequence(total, len(sequences))
  17.  
  18. password = []
  19. for (population, k) in zip(sequences, r):
  20. n = 0
  21. while n < k:
  22. position = random.randint(0, len(population)-1)
  23. password += population[position]
  24. n += 1
  25. random.shuffle(password)
  26.  
  27. while _is_repeating(password):
  28. random.shuffle(password)
  29.  
  30. return ''.join(password)
  31.  
  32. def _generate_random_number_for_each_sequence(total, sequence_number):
  33. current_total = 0
  34. r = []
  35. for n in range(sequence_number-1, 0, -1):
  36. current = random.randint(1, total - current_total - n)
  37. current_total += current
  38. r.append(current)
  39. r.append(total - sum(r))
  40. random.shuffle(r)
  41.  
  42. return r
  43.  
  44. def _is_repeating(password):
  45. n = 1
  46. while n < len(password):
  47. if password[n] == password[n-1]:
  48. return True
  49. n += 1
  50. return False
  51.  
  52. intnum = tuple(map(str, range(1, 1001)))
  53. passamount = str(intnum[-1])
  54. passwordgenerator = generate_random_password(random.randint(6, 30), SEQUENCE)
  55. charcount = len(passwordgenerator)
  56.  
  57. if __name__ == '__main__':
  58. f = open("generated_passwords.txt", "w")
  59. f.write("" + passamount + " strong password(s) were generated.\n\n")
  60. for num in intnum:
  61. f.write("[" + str(charcount) + " | RANDOM." + num + "]: " + passwordgenerator + "\n")
  62. f.write("\nSave the password you want!")
  63. print("Generated " + passamount + " random passwords in \'generated_passwords.txt\'.")
  64. f.close()
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement