Home –  SHA
Tag Archives: SHA

Your own command line password generator!

random password from command line

When you need to create a random password, stop brain storming over random characters as you have Linux command line at your disposal. It can accomplish things perfectly and, there are multiple ways to do it. Today we are creating our own command line password generator. We have assembled some ways to create a random password. We will start off with an easy one, its easy to remember:

Using md5sum

It doesn’t give you enough options but its random enough.

date +”%N” | md5sum If you want some more control, keep reading.

For rest of the following random password commands, you are allowed to modify them according to your requirement like restricting the number of characters returned.

Using /dev/urandom

It is a built-in feature. Following command return only those characters which you need to use in a password.

< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-32};echo;

Another way to do it but in reverse order:

tr -cd '[:alnum:]' < /dev/urandom | fold -w30 | head -n1

Using SHA

Date is hashed via SHA, passed on to base64 and 32 characters appear in output.

date +%s | sha256sum | base64 | head -c 32 ; echo

Using Strings

String command filters and returns printable straings:

strings /dev/urandom | grep -o ':alnum:' | head -n 30 | tr -d '\n'; echo

Using urandom (again!)

This one is less complicated:

< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c6 ; echo ;

Using dd command

Ever thought dd is capable of doing this?

dd if=/dev/urandom bs=1 count=32 2>/dev/null | base64 -w 0 | rev | cut -b 2- | rev

Using openssl’s rand

It may not be available on your system:

openssl rand -base64 32

If you feel like using some method again and again, you can save yourself the trouble of executing the command by putting it into a function and placing that function in ~/.bashrc. For an instance, if you want to create a function for SHA one here is the code:

genpass() { date +%s | sha256sum | base64 | head -c 32 ; echo } We are calling our random password generator as “genpass”.

All the commands are tested on our own system, screenshot:
random password from command line

So whats your favorite method? Post it in comments.