Skip to content

Instantly share code, notes, and snippets.

@purarue
Last active October 17, 2020 04:35
Show Gist options
  • Save purarue/b357fc74ab1bdf5e8aa35aa5ca28099e to your computer and use it in GitHub Desktop.
Save purarue/b357fc74ab1bdf5e8aa35aa5ca28099e to your computer and use it in GitHub Desktop.
#!/bin/bash
# enter length on command line. e.g. genpasswd 50
#B8G6I1l0OQDS5Z2 are problematic characters, could be mistaken as others characters when trying to read passwords.
passwordChars="abcdefghijkmnopqrstuvwxyzACEFHJKLMNPRTUVWXY()~%^&*-+=|{}[]<>,.?/3479"
passwdLength=$1 # get length from CLI
if [[ "$passwdLength" -le 0 ]]; then
passwdLength=30 # default length if not given
fi
# on average (1000 tests), the amount of characters that comes from /dev/urandom that fit the passwordChars
# is 26.1%. Reduced down by tr, 1000 characters fro had a min of 217 and a max of 310, with a standard deviation
# of 13.5, so multiplying by 5 would probably have been enough, 15 is just to be safe.
streamLength=$(( $passwdLength * 15 ))
generatedPasswd=$(head -c "$streamLength" /dev/urandom | LC_CTYPE=C tr -dc "$passwordChars" | head -c "$passwdLength")
echo -e "$generatedPasswd"
@purarue
Copy link
Author

purarue commented Oct 17, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment