Skip to content

Instantly share code, notes, and snippets.

@harlanji
Created September 28, 2014 02:53
Show Gist options
  • Save harlanji/e2a8b41e5f9fbef6593d to your computer and use it in GitHub Desktop.
Save harlanji/e2a8b41e5f9fbef6593d to your computer and use it in GitHub Desktop.
dict-password.sh - Generate words on the CLI like XKCD #936
#!/bin/sh
# dict-password.sh
# Harlan Iverson <h.iverson@gmail.com>
# Generate words on the CLI like XKCD #936 -- http://xkcd.com/936/
# usage: DICT=/usr/share/dict/words COUNT=4 ASK=y ./dict-password.sh > newpass
# or simply: ./dict-password.sh
# The command has the property that prompts go to stderr so that output can be used with pipes.
# eg. ./dict-password.sh > newpass.txt
# or with pass: ./dict-password.sh | pass insert -e MyNewPass
DICT=${DICT-/usr/share/dict/words}
COUNT=${COUNT-4}
ASK=${ASK-'Y'}
dict_count=`wc -l < ${DICT}`
pass=''
i=0
while [ $i -lt $COUNT ]; do
random=$(( ((RANDOM<<15)|RANDOM) % ${dict_count} ))
word=`head -$((${random} % ${dict_count} + 1)) ${DICT} | tail -1`
word=`echo $word| tr '[:upper:]' '[:lower:]'`
if [[ $ASK =~ ^[Yy]$ ]]; then
>&2 echo "$pass$word"
>&2 read -p "Add the word ${word}? [y/n]: " -n 1 -r
>&2 echo
fi
if [[ ! $ASK =~ ^[Yy]$ || $REPLY =~ ^[Yy]$ ]]; then
pass="${pass}${word} "
i=$[$i + 1]
fi
done
pass=`echo $pass | xargs`
echo $pass
$ ./dict-password.sh | pass insert -e TestPass
scopiformly
Add the word scopiformly? [y/n]: n
overleer
Add the word overleer? [y/n]: n
affective
Add the word affective? [y/n]: n
austerely
Add the word austerely? [y/n]: n
orchilla
Add the word orchilla? [y/n]: n
blick
Add the word blick? [y/n]: y
blick sprightlily
Add the word sprightlily? [y/n]: n
blick dinarzade
Add the word dinarzade? [y/n]: n
blick verbification
Add the word verbification? [y/n]: n
blick xeromyrum
Add the word xeromyrum? [y/n]: n
blick prepunishment
Add the word prepunishment? [y/n]: n
blick lissamphibia
Add the word lissamphibia? [y/n]: n
blick vacancy
Add the word vacancy? [y/n]: y
blick vacancy microphyllous
Add the word microphyllous? [y/n]: n
blick vacancy dardic
Add the word dardic? [y/n]: y
blick vacancy dardic acryl
Add the word acryl? [y/n]: n
blick vacancy dardic depressingness
Add the word depressingness? [y/n]: y
[master a94c7cc] Add given password for TestPass to store.
1 file changed, 0 insertions(+), 0 deletions(-)
$ pass TestPass
blick vacancy dardic depressingness
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment