d6/d6.sh

124 lines
2.7 KiB
Bash
Executable File

#!/bin/bash
# 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.
#
# 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:
#
# 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.
#
# 2. Altered source versions must be plainly marked as such, and must not be
# misrepresented as being the original software.
#
# 3. This notice may not be removed or altered from any source
# distribution.
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() {
if [ "$1" != "lower" ] && [ "$1" != "upper" ] && [ "$1" != "capital" ]
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"
exit 0
;;
h)
print_help
exit 0
;;
n)
NUM_WORDS=$OPTARG
;;
f)
WORDLIST="$OPTARG"
;;
d)
DELIMITER="$OPTARG"
;;
c)
if test_case "$OPTARG"
then
CASE="$OPTARG"
else
echo "'$OPTARG' is an invalid case. Use 'capital', 'lower', or 'upper'."
exit 1
fi
;;
?)
print_usage
exit 2
;;
esac
done
WORDS=""
for i in $(seq 1 "$NUM_WORDS")
do
ROLLS=""
for _j in {1..5}
do
ROLLS="$((1 + RANDOM % 6))$ROLLS"
done
if [ "$CASE" == "upper" ]
then
WORD="$(awk "/$ROLLS/{ print toupper(\$2) }" "$WORDLIST")"
elif [ "$CASE" == "lower" ]
then
WORD="$(awk "/$ROLLS/{ print tolower(\$2) }" "$WORDLIST")"
else
WORD="$(awk "/$ROLLS/{ print \$2 }" "$WORDLIST")"
WORD="${WORD^}"
fi
if [ "$i" -eq "$NUM_WORDS" ]
then
WORDS="${WORDS}${WORD}"
else
WORDS="${WORDS}${WORD}${DELIMITER}"
fi
done
echo "$WORDS"