Thursday 26 May 2011

Generate random passwords, passphrases or keys

One of the things I never remember how to do without looking it up is creating a (pseudo) random string of hex characters. Recently I had need to create a new hex WPA pre shared key for a wireless network I was setting up.
Running the following command on a Linux box did the trick:

dd if=/dev/urandom bs=1 count=32 2>/dev/null | xxd -ps

I know there is also a /dev/random device, so I looked up what the difference is. It seems /dev/random takes it's data from the kernel entropy pool, and if there is not enough data to serve you it will block waiting for more to become available. So If you replace /dev/random with /dev/urandom you may have to wait longer but your resulting key will be more random:

dd if=/dev/random bs=1 count=32 2>/dev/null | xxd -ps

The xxd command simply converts the output of the /dev/random device to hexadecimal output.