2
0
MasterPassword/platform-independent/scripts/mpw_mkpw

54 lines
1.2 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env bash
2017-10-06 17:33:08 +00:00
source bashlib
cdsource
getword() {
local cat=$1 pop_limit=$2 words=()
while read pop word; do
(( pop_limit && pop > pop_limit )) && break
words+=( "$word" )
done < "words.txt.$cat"
(( ${#words[@]} )) || { echo '1 -'; return 1; }
echo "${#words[@]} ${words[RANDOM % ${#words[@]}]}"
}
declare -A categoryByCharacter=(
['a']=adj
['r']=adv
['n']=noun
['v']=verb
)
templates=(
2017-10-06 17:33:08 +00:00
ran # ~32.0 bit
)
permutations=1
while getopts :t:p: arg; do
case $arg in
# The sentence template to use.
t) template=$OPTARG ;;
# Use only the top N most popular words.
p) pop_limit=$OPTARG ;;
esac
done
template=${template:-${templates[$((RANDOM % ${#templates[@]}))]}}
while true; do (
printf 'sentence: '
while read -n1 character && [[ $character ]]; do
category=${categoryByCharacter["$character"]}
read p word < <(getword "$category" "$pop_limit")
(( permutations *= p ))
printf '%s ' "$word"
done <<< "$template"
echo
printf 'permutations: %s, entropy: ~%.1f bit\n' "$permutations" "$(bc -l <<< "l($permutations) / l(2)")"
read -s -n 1 -p 'Generate another? [Yn]' && echo || break
[[ $REPLY = n ]] && break
); done