Skip to content

Instantly share code, notes, and snippets.

@darkn3rd
Last active April 28, 2024 03:45
Show Gist options
  • Save darkn3rd/746a7c8c0752f344c9d196594599ec6b to your computer and use it in GitHub Desktop.
Save darkn3rd/746a7c8c0752f344c9d196594599ec6b to your computer and use it in GitHub Desktop.
randpasswd bash function
randpasswd() {
NUM=${1:-32}
# macOS scenario
if [[ $(uname -s) == "Darwin" ]]; then
perl -pe 'binmode(STDIN, ":bytes"); tr/A-Za-z0-9//dc;' < /dev/urandom | head -c $NUM
else
# tested with: GNU/Linux, Cygwin, MSys
tr -dc 'a-zA-Z0-9' < /dev/urandom | fold -w $NUM | sed 1q
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment