pyzuki

password.py

Sep 13th, 2011
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.77 KB | None | 0 0
  1. #password.py
  2.  
  3. from random import randint, choice, shuffle, sample
  4. from mycalc import unique
  5. from time import sleep
  6. import winsound
  7.  
  8. def get_user_choices(special_chars_string):
  9.     print("How many characters long do you want your password to be? Default is 16")
  10.     user_num_chars = input("Enter the number of characters: " )
  11.     if user_num_chars == "":
  12.         user_num_chars = 16
  13.         print("The number of characters was set to 16, by default")
  14.     else:
  15.         user_num_chars = int(user_num_chars)
  16.     print()
  17.     print("These are the special characters available:", special_chars_string)
  18.     print("Or you can make your own list of special characters to use:")
  19.     print("Enter your list of special characters if you want to make your own list")
  20.     print("Enter nothing to accept the default of the list available")
  21.     ans = input("Enter your list or nothing: ")
  22.     if ans != "":
  23.         special_chars_string = unique(ans)
  24.         print("After removing any duplicate characters, your list is", special_chars_string)
  25.     special_chars_list = list(special_chars_string)
  26.     special_chars_list_length = len(special_chars_list)
  27.     user_max_num_special_chars = calc_user_max_num_special_chars(user_num_chars, special_chars_list_length)
  28.     print("Enter the number of special characters. Default is 1. Maximum is", user_max_num_special_chars)
  29.     print()
  30.     user_num_special_chars = input("Enter an integer: ")
  31.     if user_num_special_chars == "":
  32.         user_num_special_chars = 1
  33.         print("The number of special characters was set to 1, by default")
  34.     else:
  35.         user_num_special_chars = int(user_num_special_chars)
  36.     if user_num_special_chars > user_max_num_special_chars:
  37.             user_num_special_chars = user_max_num_special_chars
  38.             winsound.PlaySound("/P31Working/Sounds/beat", winsound.SND_ALIAS)
  39.             limit = user_max_num_special_chars
  40.             print("You asked for too many special characters; the number was reduced to the limit", limit)
  41.             sleep(2)
  42.     return user_num_chars, user_num_special_chars, special_chars_list
  43.  
  44. def calc_user_max_num_special_chars(user_num_chars, special_chars_list_length):
  45.     user_max_num_special_chars = user_num_chars - 2
  46.     if user_max_num_special_chars > special_chars_list_length:
  47.         user_max_num_special_chars = special_chars_list_length
  48.     return user_max_num_special_chars
  49.  
  50. def select_special_chars_to_add(user_num_special_chars, user_num_chars, special_chars_list):
  51.     return sample(special_chars_list, user_num_special_chars)
  52.  
  53. def password(user_num_chars, special_chars_num):
  54.     pw_as_list = []
  55.     # these 2 lines ensure at least 1 alpha and 1 digit
  56.     pw_as_list += choice(all_alpha_list)  
  57.     pw_as_list += choice(all_digits_list)
  58.     # get the special characters into pw
  59.     pw_as_list += select_special_chars_to_add(user_num_special_chars, user_num_chars, special_chars_list)
  60.     # add alphanum characters to fill out the pw to the length desired by user
  61.     while len(pw_as_list) < user_num_chars:
  62.         pw_as_list += choice(alphanum_list)
  63.     # shuffle the pw characters
  64.     shuffle(pw_as_list)
  65.     pw = ''.join(pw_as_list)
  66.     return pw
  67.  
  68.  
  69. alphanum_string = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ0123456789" # Removed "l", "I", "O"
  70. alphanum_list = list(alphanum_string)
  71.  
  72. # other_chars_string = "!#$%&)(*+/.:;<=>?@^" in Webmaster Password Generator
  73. special_chars_string = "!$#@%&^*" # These seem to be the only spec. chars permitted by Comcast
  74. special_chars_list = list(special_chars_string)
  75.  
  76. all_alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV"
  77. all_alpha_list = list(all_alpha)
  78.  
  79. all_digits_string = "01234567890"
  80. all_digits_list = list(all_digits_string)
  81.  
  82. print("So you need a password.")
  83. print()
  84.  
  85. while True:
  86.     user_num_chars, user_num_special_chars, special_chars_list = get_user_choices(special_chars_string)
  87.     pw = password(user_num_chars, user_num_special_chars)
  88.     print()
  89.     print("Here are 10 passwords of length", user_num_chars, "that meet your conditions:")
  90.     while True:
  91.         for z in range(10):
  92.             sleep(.2)
  93.             pw = password(user_num_chars, user_num_special_chars)
  94.             print()
  95.             print(pw)
  96.         print()
  97.         print("Another 10 or start over with a different style and number?")
  98.         print("Enter nothing for another 10; 'q' to quit; 'd' to start over.")
  99.         ans = input("Your answer? ")
  100.         print()
  101.         if ans == "":
  102.             print()
  103.             print("Here are another 10 of the same style:")
  104.             print()
  105.         elif ans == "d":
  106.             break
  107.         else:
  108.             break
  109.    
  110.     if ans == "d":
  111.         print("Starting over:")
  112.     else:
  113.         break
  114.    
  115. print("Bye.")
  116.      
  117. sleep(2.1)
Add Comment
Please, Sign In to add comment