Simple password generators

We all need passwords all the time… Yet, according to recent studies, between 30% and 60% of people use the same password for all their accounts! See [1] [2] and more…

For those of us that do need (or better yet, do want) a good password, unique to each application – system account, encrypted partition, ssh pubkeys, server administration, etc. etc. – we need a good generator; don’t forget that simple random typing also decreases entropy!

Here are 2 simple password generators I use (easy to carry around on your flash); both of them use what is readily available on most *nix systems.
Feel free to post your favourite one in comment, so that we all can learn and prosper!

sh/tr/urandom based

From Top 20 OpenSSH Server Best Security Practices, #10).

genpasswd()
{
    local l=$1
    [ "$l" == "" ] && l=20
    tr -dc A-Za-z0-9_!#$%^,. < /dev/urandom | head -c ${l} | xargs
}

You can put this code in your .bashrc file and use it directly from bash.

Or put it in a shebang script like this:

#! /bin/bash
l=$1
[ "$l" == "" ] && l=20
tr -dc A-Za-z0-9_!#$%^,. < /dev/urandom | head -c ${l} | xargs

Single parameter to the script is length of the generated password, defaults to 20 if none provided.

By changing the “A-Za-z0-9_!#$%^,.” part of the script, you can control what characters enter your password. The more the better!

PHP based

This script simply encrypts uname and current time, and cuts out usable part. Thus, this script is limited to upper and lower case letters and numbers.

Readable version is:

$pass_raw = str_replace(array("$1$", "$2$", "$2a$", "$", ".", "/"), "",
    crypt(php_uname() . microtime()));
echo " 0-10: " . substr($pass_raw, 0, 10) . "\n";
echo " 5-15: " . substr($pass_raw, 5, 10) . "\n";
echo "10-20: " . substr($pass_raw, 10, 10) . "\n";
echo " 0-20: " . substr($pass_raw, 0, 20) . "\n";

In bash scripts, simply use the above code as a one-liner in php -r ‘…’.

One response to “Simple password generators

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