Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.30 KB | None | 0 0
  1. from random import choice
  2. from time import time
  3. import string
  4.  
  5. upper = string.ascii_uppercase
  6. lower = string.ascii_lowercase
  7. digits = string.digits
  8. punctuation = string.punctuation
  9. groups = [upper, lower, digits, punctuation]
  10.  
  11. password = input("Enter a password ")
  12. print_gen_pass = input("Pring the generated passwords? y/n ")
  13.  
  14.  
  15. def pass_contains(group):
  16.     """return a list of the characters used in password
  17.    then appended to 'res' variable"""
  18.     for ch in password:
  19.         if ch in group:
  20.             return True
  21.     return False
  22.  
  23.  
  24. def gen_password(length):
  25.     """generate random password"""
  26.     res = [pass_contains(g) for g in groups]
  27.     new_group = []
  28.     g_pass = ""
  29.     for i, group in zip(res, groups):
  30.         if i:
  31.             new_group.append(group)
  32.     while len(g_pass) < length:
  33.         g_pass += choice(choice(new_group))
  34.     return g_pass
  35.  
  36.  
  37. def timing(fn):
  38.     """used to calculate time"""
  39.     def wrapper():
  40.         start_time = time()
  41.         a = fn()
  42.         end_time = time()
  43.         print(f"Time elapsed {end_time - start_time}")
  44.  
  45.     return wrapper
  46.  
  47.  
  48. @timing
  49. def run():
  50.     """run the code"""
  51.     r = True
  52.     while r:
  53.         rand_pass = gen_password(len(password))
  54.         r = rand_pass != password
  55.     print("Password cracked!")
  56.  
  57.  
  58. print(run())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement