Random passwords generation (Python vs Perl) =)

Laszlo Nagy gandalf at designaproduct.biz
Mon Jan 29 10:24:18 EST 2007


NoName írta:
> Hmmm..
> In the Perl example password generates after user hit ENTER not 
> continously like in Python you wrote... :)
>
> i want see various ways to generate passwords even if they some 
> indirect like using BASE64
>   
I copied this from a recipe, I do not remember which one. I like it very 
much because it creates password that are easy to type in. You can type 
every odd letter with your left hand and every even letter with your 
right hand.

Best,

   Leslie

from random import Random

PASSWORD_LENGTH = 10

rng = Random()

def generatePassword(length=PASSWORD_LENGTH,alternate_hands=True):
    righthand = '23456qwertasdfgzxcvbQWERTASDFGZXCVB'
    lefthand = '789yuiophjknmYUIPHJKLNM'
    allchars = righthand + lefthand
    res = ""
    for i in range(length):
        if not alternate_hands:
            res += rng.choice(allchars)
        else:
            if i%2:
                res += rng.choice(lefthand)
            else:
                res += rng.choice(righthand)
    return res


if __name__ == '__main__':
    for i in range(10):
        print generatePassword()



More information about the Python-list mailing list