Skip to content

Instantly share code, notes, and snippets.

@grugq
Last active July 14, 2022 05:54
Show Gist options
  • Save grugq/d2383ee4ff5ee0b085e8f5612fe3cb73 to your computer and use it in GitHub Desktop.
Save grugq/d2383ee4ff5ee0b085e8f5612fe3cb73 to your computer and use it in GitHub Desktop.
passphrase generator using cmdline tools because wtf not
#!/bin/bash
#
# simplistic program that creates passphrases using bash. the passphrases
# are a Number of '-' separated words, ea. of max Chars, with limited post
# processing (upper case, lower case, 1337.)
#
# (c) 2019, thaddeus t. grugq <the.grugq@gmail.com>
#
CRACKLIB=/usr/share/dict/cracklib-small
DICT=/usr/share/dict/words
lower() {
echo $1 | tr A-Z a-z
}
upper() {
echo $1 | tr a-z A-Z
}
l33t() {
echo "$1" | tr 'iletas' '1137@$'
}
qstrip() {
echo "$1" | sed -e "s/'//g" -e 's/"//g'
}
random_word() {
local n="$1"
local munge="${2:-l}" # l u e
local fname="$DICT"
local total=$( sed -E -n "/^.{1,$n}$/p" $fname | wc -l | awk '{ print $1 }')
while true
do
ndx=$RANDOM
if [[ "$total" -lt "$ndx" ]]; then
ndx=$(( $ndx % $total ))
fi
word=$(sed -E -n "/^.{1,$n}$/p" $fname | sed -n "${ndx}p")
case $munge in
"l"|"L")
word=$(lower "$word")
;;
"u"|"U")
word=$(upper "$word")
;;
"e"|"3")
word=$(l33t "$word")
word=$(lower "$word")
;;
*)
word=$(lower "$word")
;;
esac
break
done
echo "$(qstrip $word)"
}
gen_passphrase() {
local num="${1:-3}"
local len=${2:-5}
local sep="${3:--}"
local munge="${4:-l}"
words=""
for i in $(seq $num)
do
word=$(random_word $len $munge)
if [[ $i -eq 1 ]]; then
words="$word"
else
words="${words}${sep}$word"
fi
done
echo $words
}
usage() {
echo "ppw [-c|--chars N] [-n|--number N] [-l|--lower,-u|--upper,-e|--elite]"
echo " [-s|--sep C] [-k|--krack]"
echo ""
echo " -c,--chars max chars per word, default: 6"
echo " -n,--number number of words, default: 4"
echo " -l,--lower convert to lowercase {default}"
echo " -u,--upper convert to uppercase"
echo " -e,--elite convert to 1337"
echo " -s,--sep separator char, default: -"
echo " -k,--krack use cracklib, default /usr/share/dict/words"
echo ""
echo "example: "
echo " ppw -c 8 -n 4 -u -s @"
echo " JUNE@PIKE@PEABODY@FIORDS"
echo ""
echo " ppw -c 9 -n 8 "
echo " susanna-duncan-henrys-goethe-izaaks-abrams-degeneres-murdochs"
echo ""
echo " ppw -c 9 -n 5 -k "
echo " serviette-salver-notches-catchy-sheppard"
}
TEMP=$(getopt -o 'c:n:lues:kh' --long 'chars:,number:,lower,upper,elite,sep:,krack,help' -n 'ppw' -- "$@")
if [ $? -ne 0 ]; then
echo 'Terminating...' >&2
exit 1
fi
#
eval set -- "$TEMP"
unset TEMP
while true
do
case "$1" in
'-c'|'--chars')
chars=$2
shift 2
continue
;;
'-n'|'--number')
number=$2
shift 2
continue
;;
'-l'|'--lower')
munge="l"
shift
continue
;;
'-u'|'--upper')
munge="u"
shift
continue
;;
'-e'|'--elite')
munge="e"
shift
continue
;;
'-s'|'--sep')
sep=$2
shift 2
continue
;;
'-k'|'--krack')
DICT=$CRACKLIB
shift
continue
;;
'-h'|'--help')
usage
exit 1
continue
;;
'--')
shift
break
;;
*)
echo 'Internal error!' >&2
exit 1
;;
esac
done
chars=${chars:-6}
number=${number:-4}
munge=${munge:-l}
sep=${sep:--}
gen_passphrase "$number" "$chars" "$sep" "$munge"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment