Skip to content

Instantly share code, notes, and snippets.

@seanbreckenridge
Last active October 17, 2020 04:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanbreckenridge/b357fc74ab1bdf5e8aa35aa5ca28099e to your computer and use it in GitHub Desktop.
Save seanbreckenridge/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"
@seanbreckenridge
Copy link
Author

seanbreckenridge commented Oct 17, 2020

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