Skip to content

Instantly share code, notes, and snippets.

@mgor

mgor/dice-pwgen.sh

Last active Jul 4, 2016
Embed
What would you like to do?
diceware password generator (as secure as $RANDOM would be?)
#!/usr/bin/env bash
DICTIONARY="/tmp/diceware.lst"
DICTIONARY_URL="http://world.std.com/~reinhold/diceware.wordlist.asc"
WORDS="3"
COUNT="1"
SEPARATOR="-"
while getopts ":w:n:s:" optchar; do
case "${optchar}" in
w)
WORDS="${OPTARG}"
;;
n)
COUNT="${OPTARG}"
;;
s)
SEPARATOR="${OPTARG}"
;;
*)
;;
esac
done
OPTIND=1
join_array() {
local IFS="${1}"
shift
echo "${*}"
}
get_entropy() {
local entropy=""
for ((i=0; i < 5; i++))
{
n=$((RANDOM%6+1))
entropy="${entropy}${n}"
}
echo ${entropy}
}
generate_password() {
local password=()
for ((x = 0; x < WORDS; x++ ))
{
entropy="$(get_entropy)"
word="$(awk '/^'"${entropy}"'/ {print $2}' "${DICTIONARY}")"
password+=("${word}")
}
join_array "${SEPARATOR}" "${password[@]}"
}
if [[ ! -e "${DICTIONARY}" || "$(find "${DICTIONARY}" -cmin +60)" ]]; then
echo "Downloading dictionary..."
curl -s -o "${DICTIONARY}" "${DICTIONARY_URL}" \
|| { echo "Failed to download dictionary" >&2; exit 1; }
fi
for ((n=0; n < COUNT; n++))
{
generate_password
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.