Advertisement
Guest User

PasswordGenerator

a guest
Nov 27th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.11 KB | None | 0 0
  1.  
  2. import random
  3.  
  4.  
  5. def check(lst, policy):
  6.     if policy > 1:
  7.         while len(lst) < 5:
  8.             lst.insert(0, '')
  9.         while len(lst) > 5:
  10.             lst.pop(0)  # ensure the list is always five chars long
  11.         l1, l2, l3, l4, l5 = lst
  12.         num_lst = [None, None, None, None, None]
  13.         for i in range(5):
  14.             try:
  15.                 a = int([l1, l2, l3, l4, l5][i])
  16.                 if a in (0, 1, 2, 3, 4, 5, 6, 7, 8, 9):
  17.                     num_lst[i] = 1  # test if one char is a number
  18.             except ValueError:
  19.                 pass
  20.         n1, n2, n3, n4, n5 = num_lst
  21.         if policy == 5:
  22.             if (l1.isupper() and l2.isupper() and l3.isupper() and l4.isupper() and l5.isupper()) or \
  23.                     (l1.islower() and l2.islower() and l3.islower() and l4.islower() and l5.islower()) or \
  24.                     (n1 and n2 and n3 and n4 and n5):
  25.                 return False
  26.         elif policy == 4:
  27.             if (l1.isupper() and l2.isupper() and l3.isupper() and l4.isupper()) or \
  28.                     (l2.isupper() and l3.isupper() and l4.isupper() and l5.isupper()) or \
  29.                     (l1.islower() and l2.islower() and l3.islower() and l4.islower()) or \
  30.                     (l2.islower() and l3.islower() and l4.islower() and l5.islower()) or \
  31.                     (n1 and n2 and n3 and n4) or (n2 and n3 and n4 and n5):
  32.                 return False
  33.         elif policy == 3:
  34.             if (l1.isupper() and l2.isupper() and l3.isupper()) or \
  35.                     (l2.isupper() and l3.isupper() and l4.isupper()) or \
  36.                     (l3.isupper() and l4.isupper() and l5.isupper()) or \
  37.                     (l1.islower() and l2.islower() and l3.islower()) or \
  38.                     (l2.islower() and l3.islower() and l4.islower()) or \
  39.                     (l3.islower() and l4.islower() and l5.islower()) or \
  40.                     (n1 and n2 and n3) or (n2 and n3 and n4) or (n3 and n4 and n5):
  41.                 return False
  42.         elif policy == 2:
  43.             if (l1.isupper() and l2.isupper()) or (l2.isupper() and l3.isupper()) or (l3.isupper() and l4.isupper()) \
  44.                     or (l4.isupper() and l5.isupper()) or (l1.islower() and l2.islower()) or \
  45.                     (l2.islower() and l3.islower()) or (l3.islower() and l4.islower()) or \
  46.                     (l4.islower() and l5.islower()) or (n1 and n2) or (n2 and n3) or (n3 and n4) or (n4 and n5):
  47.                 return False
  48.         # compare all the symbols depending on the policy
  49.     return True
  50.  
  51.  
  52. def keygen(key_list, custom_characters, policy, char_policy):
  53.     letters_up, letters_low, numbers, c_chars, length, amount = key_list
  54.     keygen_list = []
  55.  
  56.     cnt = 0  # used to determine which argument causes the error
  57.     try:
  58.         length = int(length)
  59.         amount, cnt = int(amount), 1
  60.         policy, cnt = int(policy), 2
  61.     except (TypeError, ValueError):
  62.         # raised if you pass anything other than an int or float
  63.         raise Exception('Invalid argument figure: {}'.format(
  64.             {0: length.__class__, 1: amount.__class__, 2: policy.__class__}[cnt]))
  65.     if not letters_up and not letters_low and not numbers and not c_chars:
  66.         raise Exception("Character arguments cannot be 0 at the same time.")
  67.  
  68.     elif (letters_up + letters_low + numbers + max(c_chars, {True: 1, False: 0}[bool(custom_characters)]) == 1)\
  69.             and policy > 1:
  70.         # you can't make a password that doesn't contain characters of the same type behind each other
  71.         # with just one type
  72.         raise Exception("For a policy greater than 1, at least 2 character types must be given.")
  73.  
  74.     elif not length or not amount:
  75.         raise Exception("Length or amount arguments cannot be 0.")
  76.  
  77.     if letters_up:
  78.         keygen_list.extend('ABCDEFGHIJKLMNOPQRSTUVWXYZ')  # noqa
  79.         # noqa disables code markup in PyCharm, this ^ is classified as a typo
  80.  
  81.     if letters_low:
  82.         keygen_list.extend('abcdefghijklmnopqrstuvwxyz')  # noqa
  83.  
  84.     if numbers:
  85.         keygen_list.extend('0123456789')
  86.  
  87.     if custom_characters:
  88.         keygen_list.extend(custom_characters)
  89.         # if user wants a different set of custom characters, its used here
  90.     elif c_chars:
  91.         keygen_list.extend('#!?%&§()[]{}^<>*/+-_,;.:~@$')
  92.  
  93.     if len(keygen_list) < length and not char_policy:
  94.         print("Insufficient characters for given length. Enabling character multi use.")
  95.         char_policy += 1
  96.  
  97.     pswrd = []  # list that contains all final passwords, to be evaluated in another file
  98.     for _ in range(0, amount):
  99.         password = ''
  100.         sym_lst = ["", "", ""]  # list for all the symbols to be used in check()
  101.         buffer_keygen_list = keygen_list.copy()  
  102.         # characters will be removed, the original list has to used for all passwords, so it can't be destroyed
  103.         for _ in range(0, length):
  104.             while True:
  105.                 j = random.choice(buffer_keygen_list)
  106.                 sym_lst.append(j)
  107.                 j_ = j.upper()  # current char, capitalized
  108.                 k_ = sym_lst[-2].upper()  # last char, capitalized
  109.                 if j_ != k_ and check(sym_lst, policy):  # if they don't match, and check() is true, break loop
  110.                     if not char_policy:
  111.                         buffer_keygen_list.remove(j)  # remove char
  112.                     break
  113.                 else:
  114.                     sym_lst.pop(-1)  # if failed, remove last char from sym_list and try again
  115.             while len(sym_lst) > 5:
  116.                 sym_lst.pop(0)  # ensure sym_list is always 5 chars long
  117.             password = password + j  # if success, add char to password
  118.         pswrd.append(password)  # after generation of one password is finished, append to password list
  119.         if __name__ == "__main__":
  120.             print(password)  # only print passwords if script is main script
  121.             # I sort the passwords by strength in another file, and often generate 10.000+ passwords,
  122.             # so printing just slows things down
  123.     return pswrd
  124.  
  125.  
  126. if __name__ == '__main__':
  127.     keygen([1, 1, 1, 1, 25, 10], '', 2, 0)
  128.     # Arguments: [lettersUp, lettersLow, numbers, symbols, length, amount], 'custom characters here', policy, use characters multiple times]  # noqa
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement