Skip to content

Instantly share code, notes, and snippets.

@david-thorman
Created March 3, 2016 17:03
Show Gist options
  • Save david-thorman/4fdf6beb4669d0c1ce5e to your computer and use it in GitHub Desktop.
Save david-thorman/4fdf6beb4669d0c1ce5e to your computer and use it in GitHub Desktop.
Generate random passphrase on OSX with bash
#!/bin/bash
# Only tested on OSX
DICT="/usr/share/dict/words"
CEIL=$(wc -l "$DICT" | tr -s '[:blank:]' ' ' | cut -d' ' -f2)
if [ "$#" -lt 1 ] ; then
LEN="5"
else
LEN="$1"
fi
i=0
while [ "$i" -lt "$LEN" ] ; do
RN=$(dd if=/dev/urandom count=4 bs=1 2>/dev/null | od -t u | head -n1 | tr -s '[:blank:]' ' ' | cut -f 2 -d' ')
IDX=$(((RN % CEIL) + 1))
sed "${IDX}q;d" "$DICT"
i=$((i+1))
done | xargs echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment