passphrase/passphrase

96 lines
2.3 KiB
Bash
Executable File

#!/bin/bash
# Name: passphrase
# Author: Nick Warzin <tapesonthefloor@gmail.com>
# License: GNU GENERAL PUBLIC LICENSE
# Description: Generates random phrases appropriate for memorable but secure passwords.
SPACE='' # default to no spaces between words
COUNT=1 # default to one group of passphrases
WORDS=3 # default to three-word phrases
DICT='/usr/share/dict/words' # standard location of the words file
# DICT='/home/nicholas/passphrase/testdict' # a test dictionary
CLEANDICT="$(mktemp)" # create a temp file to store our sanitized dictionary
PASSPHRASE=`grep "^[a-z]\{3,8\}$" $DICT` # sanitize the dictionary
echo "$PASSPHRASE" >> $CLEANDICT # store the results in our temp file
DICTCOUNT=`wc -l < $CLEANDICT` # grab dictionary line count
echo ""
while [[ $# > 0 ]]; do # iterate through arguments, one or two steps at a time
key="$1" # argument key
case $key in
-s) # we want spaces b/w words
SPACE=' '
# since this option takes no value argument, we only want
# to shift ahead one argument
;;
-c) # we want a certain number of groups
COUNT="$2"
# and since this does have a value, skip the value itself on
# the next scan... we know it's not an argument key
shift
;;
-w) # we are specifying a word count
WORDS="$2"
# skip the argument; see above
shift
;;
*)
# we were given an argument we don't understand or don't care
# about. Let's just... ignore it completely, shall we?
;;
esac
shift # shift ahead one (more) argument
done
if [ "$COUNT" -lt 1 ] || [ "$WORDS" -lt 1 ]; then
echo -e "Don't be cheeky.\n"
exit 1
fi
for (( y = 1; y <= $COUNT; y++ )); do # give us COUNT groups of passwords
for i in {1..5}; do # give us five passwords per group
for (( z = 1; z <= $WORDS; z++ )); do # give us WORDS words words w...ords
THERAND=$(( ((RANDOM<<15)|RANDOM) % DICTCOUNT + 1 )) # generate a pseudorandom number beteween 1 and the word count
THEWORD=`sed -n ''"$THERAND"' p' < $CLEANDICT`
THEWORDSUM=`echo $THEWORD | cksum`
if [ "$THEWORDSUM" = "2364352366 7" ] || [ "$THEWORDSUM" = "1786501751 7" ] || [ "$THEWORDSUM" = "3985821159 5" ] || [ "$THEWORDSUM" = "3381753483 6" ]; then
(( z-- ))
else
printf "$THEWORD "
fi
done
echo ''
done
echo ''
done
exit 1