d6/d6.sh

124 lines
2.7 KiB
Bash
Raw Normal View History

2020-11-12 13:07:16 +00:00
#!/bin/bash
2024-12-31 17:55:45 +00:00
# Copyright (C) 2024 Nicolás Ortega Froysa <nicolas@ortegas.org> All rights reserved.
# Author: Nicolás Ortega Froysa <nicolas@ortegas.org>
#
# This software is provided 'as-is', without any express or implied
# warranty. In no event will the authors be held liable for any damages
# arising from the use of this software.
2020-11-12 11:58:50 +00:00
#
2024-12-31 17:55:45 +00:00
# Permission is granted to anyone to use this software for any purpose,
# including commercial applications, and to alter it and redistribute it
# freely, subject to the following restrictions:
2020-11-12 11:58:50 +00:00
#
2024-12-31 17:55:45 +00:00
# 1. The origin of this software must not be misrepresented; you must not
# claim that you wrote the original software. If you use this software
# in a product, an acknowledgment in the product documentation would be
# appreciated but is not required.
2020-11-12 11:58:50 +00:00
#
2024-12-31 17:55:45 +00:00
# 2. Altered source versions must be plainly marked as such, and must not be
# misrepresented as being the original software.
2020-11-12 11:58:50 +00:00
#
2024-12-31 17:55:45 +00:00
# 3. This notice may not be removed or altered from any source
# distribution.
2020-11-12 11:58:50 +00:00
VERSION="1.0"
function print_usage() {
echo "USAGE: d6 [OPTIONS]"
}
function print_help() {
print_usage
echo "OPTIONS:
-v print program version
-h print this help information
-n <num> number of words in generated password (default 3)
-f <file> custom dictionary file (default
~/.local/share/wordlist.txt)
-d <delim> delimiter used between words (default \"\", i.e. NONE)
-c <case> the case used for the generated words, can be: upper,
lower, capital (default \"capital\")"
}
function test_case() {
2024-12-31 16:45:52 +00:00
if [ "$1" != "lower" ] && [ "$1" != "upper" ] && [ "$1" != "capital" ]
2020-11-12 11:58:50 +00:00
then
return 1
else
return 0
fi
}
NUM_WORDS=3
WORDLIST="$HOME/.local/share/wordlist.txt"
DELIMITER=""
CASE="capital"
while getopts "vhn:f:d:c:" arg
do
case $arg in
v)
echo "d6 v$VERSION"
2024-12-31 16:45:52 +00:00
exit 0
;;
2020-11-12 11:58:50 +00:00
h)
print_help
2024-12-31 16:45:52 +00:00
exit 0
;;
2020-11-12 11:58:50 +00:00
n)
2024-12-31 16:45:52 +00:00
NUM_WORDS=$OPTARG
;;
2020-11-12 11:58:50 +00:00
f)
2024-12-31 16:45:52 +00:00
WORDLIST="$OPTARG"
;;
2020-11-12 11:58:50 +00:00
d)
2024-12-31 16:45:52 +00:00
DELIMITER="$OPTARG"
;;
2020-11-12 11:58:50 +00:00
c)
2024-12-31 16:45:52 +00:00
if test_case "$OPTARG"
2020-11-12 11:58:50 +00:00
then
CASE="$OPTARG"
else
echo "'$OPTARG' is an invalid case. Use 'capital', 'lower', or 'upper'."
exit 1
2024-12-31 16:45:52 +00:00
fi
;;
2020-11-12 11:58:50 +00:00
?)
print_usage
exit 2
2024-12-31 16:45:52 +00:00
;;
2020-11-12 11:58:50 +00:00
esac
done
2020-11-12 13:07:16 +00:00
WORDS=""
2024-12-31 16:45:52 +00:00
for i in $(seq 1 "$NUM_WORDS")
2020-11-12 13:07:16 +00:00
do
ROLLS=""
for _j in {1..5}
2020-11-12 13:07:16 +00:00
do
2024-12-31 16:45:52 +00:00
ROLLS="$((1 + RANDOM % 6))$ROLLS"
2020-11-12 13:07:16 +00:00
done
2024-12-31 16:45:52 +00:00
if [ "$CASE" == "upper" ]
2020-11-12 13:07:16 +00:00
then
2024-12-31 16:45:52 +00:00
WORD="$(awk "/$ROLLS/{ print toupper(\$2) }" "$WORDLIST")"
elif [ "$CASE" == "lower" ]
2020-11-12 13:07:16 +00:00
then
2024-12-31 16:45:52 +00:00
WORD="$(awk "/$ROLLS/{ print tolower(\$2) }" "$WORDLIST")"
2020-11-12 13:07:16 +00:00
else
2024-12-31 16:45:52 +00:00
WORD="$(awk "/$ROLLS/{ print \$2 }" "$WORDLIST")"
2020-11-12 13:07:16 +00:00
WORD="${WORD^}"
fi
2024-12-31 16:45:52 +00:00
if [ "$i" -eq "$NUM_WORDS" ]
2020-11-12 13:07:16 +00:00
then
WORDS="${WORDS}${WORD}"
else
WORDS="${WORDS}${WORD}${DELIMITER}"
fi
done
echo "$WORDS"