Thursday, January 15, 2009

Python password generator

Probably been done before, whatever.

(Update 2010-06-24: Maybe you want to use a command line program like pwgen)

from random import choice

default_number_of_passwords = 100
default_password_length = 8

def generate(length):
"""Make a password.

Password length is an argument. Which characters are used can be set below.
# Define character classes.
az = range(ord('a'),ord('z')+1)
AZ = range(ord('A'),ord('Z')+1)
zero_to_9 = range(ord('0'),ord('9')+1)
special = [ord(x) for x in '!@#$%^&*()-_=+[]{};:,.<>/?\|`~']
# Select character classes to use.
# Special characters may not be valid on the system where you want to use
# these passwords! They are therefore not used by default. Modify the string
# on the line below accordingly, and probably also the 'special' list
# above.
characters = az + AZ + zero_to_9
pw = ''
for i in range(length):
pw += chr(choice(characters))
return pw

def main_program(number_of_passwords=None,password_length=None):
if number_of_passwords is None:
number_of_passwords = default_number_of_passwords
if password_length is None:
password_length = default_password_length
for i in range(number_of_passwords):
print generate(password_length)

if '__main__' in __name__:

No comments: