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. 😀

4 comments on “random passwords

  1. Andy Dustman

    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.

    Reply

Leave a Reply