commit - 7a2f6b373bf41741d33afebd88d813e95cca950a
commit + 2b6ecc0555bb8b39947baf07223d2d3f84c2d3a8
blob - 0e7f7b549b3c38ef52ba877fbc163039c8d36d7b
blob + 055d8fe7cb155a1a64e5825dac2eb255d94584e1
--- badphrase
+++ badphrase
#!/bin/sh
-# badphrase: creates a passphrase from a word dictionary
+set -e
-# constants
englishchars=26
-# flags
charmin=6
charmax=0
copy=0
usage() {
echo "usage: ${0##*/} [-ci] [-e entropymin] [-l wordlist] [-m charmax] \
[-n charmin] [-w wordnum]" 1>&2
- return 1;
}
while getopts ce:hil:m:n:w: arg; do
case $arg in
c) copy=1;;
e) entropymin=${OPTARG};;
- h) usage;;
+ h) usage; exit;;
i) includenames=1;;
l) wordlist=${OPTARG};;
m) charmax=${OPTARG};;
n) charmin=${OPTARG};;
w) wordnum=${OPTARG};;
- ?) usage;;
+ ?) usage; exit 1;;
esac
done
shift $((OPTIND - 1))
# password length should satisfy entropy requirements
minlen=$(echo "l(2^${entropymin})/l($englishchars)" | bc -l)
-if [ "$(echo "${wordnum}*${charmin} >= ${minlen}" | bc -l)" -ne 1 ]; then
- printf "%s: wordnum * charmin needs to be >= %.2f\n" "${0##*/}" "$minlen" 1>&2
- exit 1;
+if echo "${wordnum}*${charmin} >= ${minlen}" | bc -l | xargs test 1 -ne; then
+ printf "%s: wordnum * charmin needs to be >= %.2f (entropymin = %d)\n" \
+ "${0##*/}" "$minlen" "$entropymin" 1>&2
+ exit 1
fi
# generate the passphrase
sort -R "$wordlist" | \
-if [ "$includenames" -eq 0 ]; then grep '^[^A-Z]'; else cat; fi | \
-if [ "$charmax" -lt "$charmin" ]; then grep -x ".\{${charmin},\}"; else grep -x ".\{${charmin},${charmax}\}"; fi | \
+if [ "$includenames" -eq 0 ]; then
+ grep '^[^A-Z]'
+else
+ cat
+fi | \
+if [ "$charmax" -lt "$charmin" ]; then
+ grep -x ".\{${charmin},\}"
+else
+ grep -x ".\{${charmin},${charmax}\}"
+fi | \
head -n "$wordnum" | \
-# output the passphrase
if [ "$copy" -eq 1 ]; then
paste -sd '' - | xclip -selection clipboard
else