Generador de passwords en Python

July 11th, 2011 | Categories: Día a día, Tecnología | Tags: , , , ,

Buscando varias alternativas para generar rápidamente contraseñas copadas, y realimentándome de varias fuentes, terminé eligiendo y adaptando un script ligeramente para generar varias contraseñas por parámetro. A continuación el script:

#!/usr/bin/env python
"""
A simple script for making random passwords, WITHOUT 1,l,O,0.  Because
those characters are hard to tell the difference between in some fonts.
"""

#Import Modules
import sys
from random import Random

rng = Random()

righthand = '23456qwertasdfgzxcvbQWERTASDFGZXCVB'
lefthand = '789yuiophjknmYUIPHJKLNM'
allchars = righthand + lefthand
alternate_hands = True

try:
    passwordLength = int(sys.argv[1])
except:
    #user didn't specify a length.  that's ok, just use 8
    passwordLength = 16

try:
    passwordQty = int(sys.argv[2])
except:
    #user didn't specify a length.  that's ok, just use 8
    passwordQty = 1

for q in range(passwordQty):
    for i in range(passwordLength):
        if not alternate_hands:
            sys.stdout.write( rng.choice(allchars) )
        else:
            if i%2:
                sys.stdout.write( rng.choice(lefthand) )
            else:
                sys.stdout.write( rng.choice(righthand) )

    sys.stdout.write( "\n" )

Para ejecutar, simplemente se debe ejecutar de la siguiente forma (suponiendo que se guarda den tro de $PATH con el nombre “passgen”:

user@unixbox:$ passgen [longitud] [cantidad]
No comments yet.