read

From the 'just because it can be done' column, here comes a handy shell script to generate random salts.

So, without further ado,  here it goes:

#!/bin/bash 

[ $# -eq 0 ] && {
        echo "usage: salt <length>">&2
        exit
}
strings </dev/urandom | while read line; do
        echo $line | tr '\n\t ' $RANDOM:0:1 >> /tmp/.salt.$$
        salt=$(cat /tmp/.salt.$$)
        [ ${#salt} -ge $1 ] && salt=${salt:0:$1} && echo $salt && break
done
rm -f /tmp/.salt.$$

I had to use $1 and not a var, and echo the salt right from inside the while, because the '|' creates another shell, so I can't pass variables to or from the while in this case.

If you want to keep things simple, you can go perl and just do

#!/usr/bin/perl
use Crypt::Salt;
my $length = shift;
print salt($length);

But if you ever find yourself in a server with no cpan, the first option might prove useful :)

Blog Logo

Fernando Ipar


Published

Image

Fernando Ipar

"Tell my wife I love her very much, she knows"

Back to Overview