badphrase (2057B)
1 #!/bin/sh 2 # badphrase: generate passphrases or something lol 3 # diceware example: ./badphrase -dvl /tmp/diceware.wordlist.asc -w 10 4 # please use with diceware bacause all other wordlists suck ToT 5 # yay! 6 set -e 7 8 charmin=0 9 charmax=0 10 copy=0 11 diceware=0 12 entropymin=128 13 includenames=0 14 verbose=0 15 wordlist=/usr/share/dict/words 16 wordnum=8 17 18 usage() { 19 echo "usage: ${0##*/} [-cdhiv] [-e entropymin] [-l wordlist] [-m charmax] \ 20 [-n charmin] [-w wordnum]" 1>&2 21 } 22 23 while getopts cde:hil:m:n:vw: arg; do 24 case $arg in 25 c) copy=1;; 26 d) diceware=1;; 27 e) entropymin=${OPTARG};; 28 h) usage; exit;; 29 i) includenames=1;; 30 l) wordlist=${OPTARG};; 31 m) charmax=${OPTARG};; 32 n) charmin=${OPTARG};; 33 v) verbose=1;; 34 w) wordnum=${OPTARG};; 35 ?) usage; exit 1;; 36 esac 37 done 38 shift $((OPTIND - 1)) 39 # setup regex 40 if [ "$includenames" -eq 1 ]; then 41 re_class='.' 42 else 43 re_class='^[^A-Z]' 44 fi 45 [ "$charmin" -lt 0 ] && charmin=0 46 [ "$charmax" -le 0 ] && charmax='' 47 # password generation pipeline 48 # extract 2nd column if we're on a diceware list 49 awk -v diceware="$diceware" 'BEGIN { 50 if (diceware) 51 rec = 2 52 else 53 rec = 1 54 } 55 { print $rec }' "$wordlist" | 56 # filter and shuffle the wordlist 57 grep -x "${re_class}\{${charmin},${charmax}\}" | sort -R | 58 # test entropy against $entropymin and if that worked print first $wordnum lines 59 { awk -v prog="${0##*/}" -v entropymin="$entropymin" -v wordnum="$wordnum" -v \ 60 verbose="$verbose" 'NR <= wordnum { line[NR] = $0 } 61 END { 62 perms = NR 63 # show entropy 64 entropy = log(perms^wordnum) / log(2) 65 if (verbose) 66 printf("log2(%d^%d) = %.2f bits of entropy\n", perms, wordnum, \ 67 entropy) >"/dev/stderr" 68 # stop if we dont satisfy entropy requirements 69 lengthmin = log(2^entropymin) / log(perms) 70 if (wordnum < lengthmin) { 71 printf("%s: wordnum needs to be >= %.2f so that entropy would be \ 72 >= %d\n", prog, lengthmin, entropymin) >"/dev/stderr" 73 exit 1 74 } else { 75 for (i = 1; i < wordnum; i++) 76 print line[i] 77 } 78 }' || exit 1; } | 79 # output passphrase 80 if [ "$copy" -eq 1 ]; then 81 paste -sd '' - | xclip -selection clipboard 82 else 83 paste -sd ' ' - 84 fi