Skip to content

Instantly share code, notes, and snippets.

@fipar
Created March 21, 2013 18:22
Show Gist options
  • Save fipar/5215395 to your computer and use it in GitHub Desktop.
Save fipar/5215395 to your computer and use it in GitHub Desktop.
Generate a random salt from bash
#!/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.$$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment