Skip to content

Commit

Permalink
Remove old code from passphrase
Browse files Browse the repository at this point in the history
  • Loading branch information
deniscostadsc committed Nov 23, 2023
1 parent 959b382 commit 3c08ef6
Showing 1 changed file with 10 additions and 49 deletions.
59 changes: 10 additions & 49 deletions roles/bash/files/bin/passphrase.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,27 @@ set -euo pipefail
min_word_length=4
max_word_length=6
password_word_count=5
dice_number=""

function dice2int {
dice_base=6

i=0
number=0

for current_dice_digit in $(fold -w1 <<< "$(echo "$1" | rev)"); do
current_dice_digit_index=$((current_dice_digit - 1))
number=$(("$number" + "$current_dice_digit_index" * "$dice_base" ** "$i"))
i=$(("$i"+1))
done

echo $number
}

function show_help {
echo
echo "diceware"
echo "passphrase"
echo
echo " --dice (-d)"
echo " --min (-n) sets minimum number of characters in the words"
echo " --max (-m) sets maximum number of characters in the words"
echo " --count (-c) sets how many word the password will contain"
echo " --help (-h) shows help message and exit"
echo
}


while [[ $# -gt 0 ]]; do
case $1 in
--dice|-d)
dice_number=$2
if ! [[ $dice_number =~ ^[123456]{1,6}$ ]]; then
echo "Invalid dice input!"
exit 1
fi
shift
;;
--min|-n)
min_word_length=$2
shift
;;
--max|-m)
max_word_length=$2
if [[ $max_word_length -lt $min_word_length ]]; then
if [[ ${max_word_length} -lt ${min_word_length} ]]; then
echo
echo "max should be greater than min"
exit 1
Expand All @@ -75,28 +49,15 @@ while [[ $# -gt 0 ]]; do
shift
done


elegible_words=$(
grep -E "^[A-Za-z]{${min_word_length},${max_word_length}}$" /usr/share/dict/words | tr '\n' ' ')
word_count=$(wc -w <<< "$elegible_words")


if [[ -z $dice_number ]]; then
for ((i = 0; i < "$password_word_count"; i++)); do
echo -n "$(cut -d ' ' -f "$((RANDOM % word_count + 1))" <<< "$elegible_words" | tr '[:upper:]' '[:lower:]') "
done

echo
exit 0
fi
word_count=$(wc -w <<< "${elegible_words}")

if [ "$(dice2int "$dice_number")" -gt "$word_count" ]; then
echo
echo -n "$dice_number represents the $(dice2int "$dice_number")th word, but"
echo " we just have $word_count words."
echo

exit 1
fi
for ((i = 0; i < "${password_word_count}"; i++)); do
word_index="$((RANDOM * RANDOM * RANDOM * RANDOM % word_count + 1))"
word="$(cut -d ' ' -f "${word_index}" <<< "${elegible_words}" | tr '[:upper:]' '[:lower:]')"
echo -n "${word} "
done

cut -d ' ' -f "$(dice2int "$dice_number")" <<< "$elegible_words" | tr '[:upper:]' '[:lower:]'
echo
exit 0

0 comments on commit 3c08ef6

Please sign in to comment.