Generate strong random passwords of specified length.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import random
import string
import time
def mkpass(size=16):
chars = []
chars.extend([i for i in string.ascii_letters])
chars.extend([i for i in string.digits])
chars.extend([i for i in '\'"!@#$%&*()-_=+[{}]~^,<.>;:/?'])
passwd = ''
for i in range(size):
passwd += chars[random.randint(0, len(chars) - 1)]
random.seed = int(time.time())
random.shuffle(chars)
return passwd
|
Thanks for sample. Instead
passwd += chars[random.randint(0, len(chars) - 1)]
better use
passwd += random.choice(chars)
Hi,
This is probably a smaller and perhaps more optimal solution to the same problem:
import string import random
validCharacters = string.ascii_letters + string.digits validCharacters = validCharacters.strip("oO0") return string.join( [random.choice(validCharacters) for x in range(n)], "")
This is a program that I use when a strong, random password is needed.
For better cryptographic use, random.SystemRandom() should be used.