random passwords

I needed to write a random password generator today, and instead of bothering to search for some code that I could copy and paste, I just whipped up my own real quick.

Then I got thinking about how all those Perl monks brag about how they can cram all this code into as few lines as possible, so I crammed mine into just one line. :)

Here it is:

for($x = 0, $password = ”, $password_len = rand(8,24), $alphabet_range = range(‘A’, ‘z’); $x < $password_len; $x++) { $password .= $alphabet_range[rand(1, count($alphabet_range) – 1)]; }

The great thing is it spits out cool passwords like this:

  • luhRDKyMDX\oey
  • rbKGTt`dLHWkHQZ
  • LJyeO_UwTpQzIni
  • ltonXGkho[e\JWbBl^uyEL

Fun stuff. :D

4 thoughts on “random passwords

  1. Python version:

    def generate_password(bits=48):
    “””Generate a password that has at least the number of bits of
    entropy requested (default: 48).”””
    from math import ceil
    bytes = int(ceil(bits / 8.0))
    return file(‘/dev/urandom’).read(bytes).encode(‘base64’).replace(‘=’, ”)

    or if you just want an one-line expression:

    password = file(‘/dev/urandom’).read(6).encode(‘base64’).replace(‘=’, ”)

    will give you 8-character (48-bit) passwords.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s