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:


[ $# -eq 0 ] && {
        echo "usage: salt <length>">&2
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
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

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



Fernando Ipar

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

Back to Overview