Welcome, guest | Sign In | My Account | Store | Cart

A simple Password Generator in Python

Python, 24 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/python
# -*- coding: utf-8 -*-
from os import urandom

charsets={
"a":"abcdefghijklmnopqrstuvwxyz",\
"A":"ABCDEFGHIJKLMNOPQRSTUVWXYZ",\
"0":"0123456789",\
"$":"^!\"$%&/()=?{[]}+*~#'-_.:,;<>|\\ "\
}

def GeneratePassword(length=5,charset="aA0"):
	password="";lc="";charset_string=""
	for c in charset:
		if c in charsets.keys():
			charset_string+=charsets[c]
	while len(password)<length:
		c=urandom(1)
		if c in charset_string and c!=lc:
			password+=c;lc=c
	return password

if __name__=="__main__":
	print GeneratePassword(15,"aA0")

Generates a Password (technically a random String) using a given Charset and Length from random Data.

2 comments

Evert van de Waal 9 years, 6 months ago  # | flag

A word of warning: random passwords may contain letter & number combinations that users may find offensive. Sounds unlikely? Happend first time I tried it. Got some very unpleasant emails...

ajaymenon.k 9 years, 5 months ago  # | flag

Hey -

I was really inspired by your recipe. Thanks. I have written a modified version of the algorithm here - http://code.activestate.com/recipes/578169-extremely-strong-password-generator/.

Could you have a look and let me know what you think about it ?

Thanks _Aj