On 5/25/11, Wolf Halton <wolf.hal...@gmail.com> wrote: > Is there a less clunky way to do this? > [code] > def new_pass(): > series = ['`', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', > '=', \ > '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', > '+', \ > 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', > '\\', \ > 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', > '|', \ > 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', "'", \ > 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', \ > 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', \ > 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?'] > passwd = [] > p = input("Enter the length you want your password to be: ") > # length of password > for i in range(p): > r = random.randint(0, 94) > passwd.append(series[r]) # Append a random char from series[] to > passwd > #print passwd > #print passwd[0], passwd[1], passwd[2], passwd[3] > print "" > print "".join(map(str, passwd)), " is your new password. \n" > [/code] > > [output] >>>> > Enter 1 to run a MD5 hash on your password > Enter 2 to run a SHA1 hash on your password > Enter 3 to run a SHA224 hash on your password > Enter 9 to get a new randomy password > Enter 10 to run away... he he he > > Enter your choice here> 9 > Enter the length you want your password to be: 4 > > !bnR is your new password. >>>> > [/output]
Depending on what your passwords are going to be protecting, be aware that the default generator in the random module is: "...completely unsuitable for cryptographic purposes." Instead, create an instance of the 'SystemRandom' class. You also don't need to create a list of all of your values. A simple string will do. You should also look into the 'string' module, as it defines 'letters', 'digits' and 'punctuation' characters for you. Thus, your code be something like: import random import string passlen = 10 # How long should it be? r = random.SystemRandom() chars = string.letters + string.digits + string.punctuation passwd = "" for i in range(passlen): passwd += r.choice(chars) print passwd -Modulok- _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor