Hi, On Mon, 17 Feb 2003 15:41:17 +0100 Dario Lopez-Kästen <dario@ita.chalmers.se> wrote:
From: "Asad Habib" <ahabib1357@yahoo.com>
Hello. Does anyone know if there are any built-in Python methods which allow you to generate passwords randomly? Any help would be greatly appreciated. Thanks.
-Asad
---------- def generatePassword(self, pwdLen=8): # Returns a random password. import random random.seed() passwd= '' # Since I, 1 ~= l and 0 =~ O, don't generate passwords with them. # This will just confuse people using ugly fonts. charset = 'abcdefghijkmnopqrstuxyzABCDEFGHJKLMNPQRSTUXYZ0123456789' for i in range(pwdLen): passwd = passwd + random.choice(charset) return passwd --------
Thats OK for most cases, but its not really random because it uses a sequence of the pseudo random number generators values. Improvements would be seeding with more then one source (seed() uses time.time() by default) after generating some values and generating variable lenght passwords. ## import random chars=[x for x in 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ123456789+-#!$?'] random.seed() # first seeding from time skip=int(random.random()*8)+1 # random values to skip random.seed(context.REQUEST.channel.creation_time) # another seeding from channel time - hard to guess lengh=int(random.random()*3)+7 # variable lenght of generated password from 7 to 10 chars random.shuffle(chars) random.seed(context.REQUEST.HTTP_USER_AGENT+context.REQUEST.HTTP_ACCEPT_CHARSET+str(DateTime())) random.shuffle(chars) random.seed() # get a fresh seed now return ''.join([random.choice([random.choice(chars) for i in range(skip)]) for o in range(lenght)]) # the above consists of two loops - the inner selects random chars from shuffled big string, "skip" times, the outer selects one char from each loop cycle randomly. # even knowing the channel time, the time, the random number generator internals would it make a challenge to break this password. Ok its not impossible, of course... # the goal was to introduce as much entrophy as possible HTH Tino Wildenhain