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

About these ads

4 Comments

Filed under Computers

4 responses to “random passwords

  1. You, my friend, are a geek. But I got NUTHIN’ but love for ya.

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

  3. Steve

    @Andy

    That’s sweet man … gotta learn Python. :T

    Plus, PHP needs a rand_password() function.

  4. Personally, I use the following shell code:

    head -c 8 /dev/urandom | uuencode -m – | tail -n 2 | head -c 8; echo

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s