>>> import string, random >>> def passgen(length) : ... keys = list(string.ascii_letters + string.digits) ... return "".join(random.choice(keys) for i in range(length)
With this definition of the passgen function, we can generate alphanumeric passwords with whatever length we want. If you’d like to include all characters available, try the one below:
>>> import string, random >>> def passgen(length) : ... keys = list(string.ascii_letters + string.digits + ".,;:-_()@\"\\[]?!'^+*$%&/=~`<>|") ... return "".join(random.choice(keys) for i in range(length)
A sample output :
>>> passgen(16) 'pP!3p"(-uxdIqpAK'
You can find some methods of password generation using MD5 algorithms. For example for password generation in MySQL some people prefer this method; >SELECT SUBSTRING(MD5(RAND()) FROM 1 FOR 5) But this will generate very very weak passwords, no uppercase characters and a lot of characters missing, not even to mention the non-alpha numeric characters. Also you’ll have a limit for maximum character number since the MD5 algorithm has a limit for it. So it’s best to stay away from the md5 approach for password generation. Some people also use it for bash password generation too (which is wrong! due to same reasons)