Skip to content

Instantly share code, notes, and snippets.

@absturztaube
Created November 2, 2018 10:09
Show Gist options
  • Save absturztaube/5f935ce8e69335607e65b094aa89f483 to your computer and use it in GitHub Desktop.
Save absturztaube/5f935ce8e69335607e65b094aa89f483 to your computer and use it in GitHub Desktop.
Just a little hash generator for cli
#!/usr/bin/env bash
VERSION="0.1"
function usage
{
echo "hashgen v${VERSION}"
echo "Usage: hashgen [-m hashmethod] [-c config] [-s salt] [-h] string_to_hash"
echo "Options:"
echo " -m hashmethod Defines the Hash Function to be used. Default is sha265"
echo " -c config Uses a config to define the paramters"
echo " -s salt Defines a salt that the hash function should use"
echo " -h Prints this help"
}
function generate_hash
{
method=$1
string=$2
salt=$3
echo -n "${string}${salt}" | openssl dgst -$method | awk '{print $2}'
}
function get_value_from_config
{
config=$1
name=$2
grep "$name=" "$config" | awk '{split($0,a,"="); print a[2]}'
}
method="sha256"
config=''
salt=''
forceMethod=''
forceSalt=''
while getopts ":m:c:sh" o; do
case "${o}" in
m)
forceMethod=${OPTARG}
;;
c)
config=${OPTARG}
if [[ -f $config ]]; then
method=$(get_value_from_config "$config" "method")
salt=$(get_value_from_config "$config" "salt")
else
echo "config not found, ignoring -c"
fi
;;
s)
forceSalt=${OPTARG}
;;
*)
usage
exit
;;
esac
done
shift $((OPTIND-1))
if [[ "$forceSalt" != "" ]] ; then
salt=$forceSalt
fi
if [[ "$forceMethod" != "" ]] ; then
method=$forceMethod
fi
string_to_hash=$1
generate_hash "$method" "$string_to_hash" "$salt"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment