passphrase/passphrase

107 lines
2.5 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
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
for i in {1..5}; do
for (( z = 1; z <= $WORDS; z++ )); do # give us WORDS words words
THERAND=$(( ((RANDOM<<15)|RANDOM) % DICTCOUNT )) # generate a pseudorandom number beteween 1 and the word count
printf `sed -n ''"$THERAND"' p' < $CLEANDICT`"$SPACE"
done
echo ''
done
echo ''
done
exit 1
for (( z = 1; z <= $COUNT; z++ )); do # give us COUNT groupings of five passphrases
for i in {1..5}; do
PASSPHRASE=`grep "^[a-z]\{3,8\}$" /usr/share/dict/words | shuf -n$WORDS`
# select all words from the standard linux words file with a length between
# 3 and 8 characters. Give us some of those words at random.
if [ "$SPACE" -eq 0 ]; then # if we want spaces stripped
PASSPHRASE=`echo $PASSPHRASE | tr -d '[[:space:]]'` # trim all spaces
fi
echo $PASSPHRASE # dump result to screen
done
echo ""
done