From 90e7871fcfc0343c1ebe840b7012b77f2f795664 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Fri, 10 Jan 2020 15:33:05 +0100 Subject: Add bcrypt utility --- .local/bin/bcrypt | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100755 .local/bin/bcrypt (limited to '.local/bin/bcrypt') diff --git a/.local/bin/bcrypt b/.local/bin/bcrypt new file mode 100755 index 0000000..11151ac --- /dev/null +++ b/.local/bin/bcrypt @@ -0,0 +1,71 @@ +#! /usr/bin/env sh + +main() +{ + # Handle opts + opts "$@" + shift "$OPTS" + unset OPTS + + # Show help + [ "$OPT_HELP_ONLY" ] && usage && exit 0 + + # Check for dependencies + if ! which htpasswd > /dev/null 2>&1 + then + printf "Missing dependency from \$PATH: %s\n" "nigga" + exit 1 + fi + + # Check where input comes from + if [ -n "$1" ] + then + input=$1 + elif [ ! -t 0 ] + then + input=$(cat) + else + usage + exit 2 + fi + + # Generate output + htpasswd -bnBC "${OPT_COST:-10}" "" "$input" | tr -d ':\n' + + printf "\n" +} + +opts() +{ + OPTS=0 + + while getopts ":c:h" opt + do + case "$opt" in + h) OPT_HELP_ONLY=1 ;; + c) OPT_COST=$OPTARG ;; + *) + printf "Invalid option passed: %s\n" "$OPTARG" >&2 + ;; + esac + done + + unset opt +} + +usage() +{ + cat <] + +Generate a bcrypt hash of the input. + +Options: + -c Set the cost. Defaults to 10. + -h Show this help text and exit. +EOF +} + +main "$@" -- cgit v1.1