Advertisement
Guest User

Untitled

a guest
Oct 14th, 2014
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. from os import urandom
  2. from random import choice
  3.  
  4. char_set = {'small': 'abcdefghijklmnopqrstuvwxyz',
  5. 'nums': '0123456789',
  6. 'big' : 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
  7. 'special': '^!\$%&/()=?{[]}+~#-_.:,;<>|\\'
  8. }
  9.  
  10.  
  11. # Generate a password
  12. def generate_pass(lenght=16):
  13.  
  14. password = []
  15.  
  16. while len(password) < lenght:
  17. key = choice(char_set.keys())
  18. a_char = urandom(1)
  19. if a_char in char_set[key]:
  20. if check_prev_char(password, char_set[key]):
  21. continue
  22. else:
  23. password.append(a_char)
  24. return ''.join(password)
  25.  
  26.  
  27. # Function to ensure that there are no consecutive
  28. # UPPERCASE/lowercase/numbers/special-characters.
  29. def check_prev_char(password, current_char_set):
  30.  
  31. index = len(password)
  32. if index == 0:
  33. return False
  34. else:
  35. prev_char = password[index - 1]
  36. if prev_char in current_char_set:
  37. return True
  38. else:
  39. return False
  40.  
  41. if __name__ == '__main__':
  42. print generate_pass()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement