Skip to content

Instantly share code, notes, and snippets.

@dwallraff
Last active February 5, 2019 08:43
Show Gist options
  • Save dwallraff/3e748bd8c0cfb2dfb4439a9ee037fff2 to your computer and use it in GitHub Desktop.
Save dwallraff/3e748bd8c0cfb2dfb4439a9ee037fff2 to your computer and use it in GitHub Desktop.
Generate random(ish) passwords from the cli (thanks to @markpudd for the idea)
#! /usr/bin/env bash
## Use openssl. Use hex not base64, special characters suck in passwords
# the number refers to the number of bytes, so with 2 hex characters per byte, this is a 32 character password.
openssl rand -hex 16
## Just straight up urandom. Give me 5 32 char alpha-numeric passwords
tr -cd '[:alnum:]' < /dev/urandom | fold -w 32 | head -n 5
tr -cd 'a-zA-Z0-9' < /dev/urandom | fold -w 32 | head -n 5
## Use gpg
# --gen-random 0,1 [count] uses /dev/urandom
# --gen-random 2 [count] uses /dev/random THIS WILL SIGNIFICATNLY DRAIN YOUR ENTROPY
# [count] is the number of bytes to pull. 1000 bytes should be enough for 5 32 char password
gpg --gen-random 1 1000 | tr -cd '[:alnum:]' | fold -w 32 | head -n 5
## XKCD-style rand word generator (assumes you have a word list in /usr/share/dict/words)
shuf -n 5 /usr/share/dict/words | sed 's/./\u&/' | tr -d "'" | tr "\n" " "
for i in {1..6}; do shuf -n 5 /usr/share/dict/words | sed 's/./\u&/' | tr -d "'" | tr "\n" " "; echo; done
## Make passwords, not phrases, and append some digits to the end
# shellcheck disable=SC2005
{ shuf -n 5 /usr/share/dict/words | sed 's/./\u&/' | tr -cd "[:alnum:]"; echo "$(printf "%05d" "$(shuf -i 0-99999 -n 1)")"; }
# shellcheck disable=SC2034
# shellcheck disable=SC2005
for i in {1..6}; do shuf -n 5 /usr/share/dict/words | sed 's/./\u&/' | tr -cd "[:alnum:]"; echo "$(printf "%05d" "$(shuf -i 0-99999 -n 1)")"; done
## If using different locales, you might need to set your locale before doing string operations
# LC_CTYPE=C
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment