Skip to content

Instantly share code, notes, and snippets.

@mgor
Last active July 4, 2016 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgor/736328fdc0f449a85a8c to your computer and use it in GitHub Desktop.
Save mgor/736328fdc0f449a85a8c to your computer and use it in GitHub Desktop.
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