commit 2b6ecc0555bb8b39947baf07223d2d3f84c2d3a8
parent 7a2f6b373bf41741d33afebd88d813e95cca950a
Author: boredpasta <boredpasta@tutanota.com>
Date: Fri, 14 Mar 2025 22:54:14 +0200
badphrase: refactor and fix -h and ? not exiting
Diffstat:
1 file changed, 17 insertions(+), 12 deletions(-)
diff --git a/badphrase b/badphrase
@@ -1,10 +1,8 @@
#!/bin/sh
-# badphrase: creates a passphrase from a word dictionary
+set -e
-# constants
englishchars=26
-# flags
charmin=6
charmax=0
copy=0
@@ -16,37 +14,44 @@ wordnum=4
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