Wednesday, September 1, 2010

Really short way to generate a secure password in Python

Needed something like that today, was going to write a character table and mix it up with a pseudo-random number generator (i.e. the normal approach), but a thought struck me...
# This assumes you've imported os somewhere in your code.
new_password = os.urandom(8).encode("base64")[0:-2]

Done! QED! FIN!

The only minor problem: It doesn't generate pronounceable passwords, but the passwords I use aren't pronounceable anyways.

No comments: