Skip to content

Instantly share code, notes, and snippets.

@carlosmcevilly
Last active September 11, 2019 15:55
Show Gist options
  • Save carlosmcevilly/39430505ab56d4478cd7cd6ab00036c8 to your computer and use it in GitHub Desktop.
Save carlosmcevilly/39430505ab56d4478cd7cd6ab00036c8 to your computer and use it in GitHub Desktop.
hashit - wrapper around openssl providing easy command-line access to common hash functions
#!/bin/bash
# hashit - wrapper around openssl providing easy command-line access to common hash functions
#
# by Carlos McEvilly
#
# 'hashit' creates symlinks to itself, named after
# common hashing schemes, and then the script can
# be invoked via any so-named symlink with a text
# string or filename argument in order to obtain
# the hash of the argument with the given hash
# scheme. On a case-insensitive file system such
# as macOS in its default configuration, you can
# also invoke the symlinks by uppercase name in
# order to get the resulting hash back as an
# uppercase string.
#
# If the argument is a filename, hashit will
# output the hash of the contents of the file.
#
# Example: After running 'hashit setup'
# which creates some of the symlinks (or
# 'hashit setup_all' which creates more
# of them), then you can run:
#
# $ sha256 test
# this will give the output:
# 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08
# which is the sha256 hash of 'test'
#
# Or you could run:
# $ SHA256 test
# which will give the upper case version of
# the same output:
# 9F86D081884C7D659A2FEAA0C55AD015A3BF4F1B2B0B822CD15D6C15B0F00A08
#
# Just like sha256, after setup, other common
# hashing schemes are available on the command
# line in the same manner.
#
# This depends on openssl being installed and
# on your path.
#
# Issues:
#
# If you want the hash of a string, but a file
# in your current directory has a filename
# matching that string, you'll have to invoke
# the command from another directory in order
# to avoid having it interpret the string as
# a filename. This will be fixed in a future
# version.
#
export script=$0
export input=$1
if [[ "$input" == '' ]]; then
echo usage: $0 ['setup | setup_all] | <text_string | filename>'
exit -1
fi
function main() {
if [[ "$input" == 'setup' ]]; then
make_basic_links
exit 0
elif [[ "$input" == 'setup_all' ]]; then
make_all_links
exit 0
fi
export cmd=`basename "$script"`
# this huge block of if/elif is kind of gross.
# open to suggestions.
if [[ "$cmd" == "md5" ]]; then
hash_lowercase md5 "$input"
elif [[ "$cmd" == "MD5" ]]; then
hash_uppercase md5 "$input"
elif [[ "$cmd" == "sha1" ]]; then
hash_lowercase sha1 "$input"
elif [[ "$cmd" == "SHA1" ]]; then
hash_uppercase sha1 "$input"
elif [[ "$cmd" == "sha256" ]]; then
hash_lowercase sha256 "$input"
elif [[ "$cmd" == "SHA256" ]]; then
hash_uppercase sha256 "$input"
elif [[ "$cmd" == 'gost-mac' ]]; then
hash_lowercase gost-mac "$input"
elif [[ "$cmd" == 'GOST-MAC' ]]; then
hash_uppercase gost-mac "$input"
elif [[ "$cmd" == 'md4' ]]; then
hash_lowercase md4 "$input"
elif [[ "$cmd" == 'MD4' ]]; then
hash_uppercase md4 "$input"
elif [[ "$cmd" == 'md_gost94' ]]; then
hash_lowercase md_gost94 "$input"
elif [[ "$cmd" == 'MD_GOST94' ]]; then
hash_uppercase md_gost94 "$input"
elif [[ "$cmd" == 'ripemd160' ]]; then
hash_lowercase ripemd160 "$input"
elif [[ "$cmd" == 'RIPEMD160' ]]; then
hash_uppercase ripemd160 "$input"
elif [[ "$cmd" == 'sha' ]]; then
hash_lowercase sha "$input"
elif [[ "$cmd" == 'SHA' ]]; then
hash_uppercase sha "$input"
elif [[ "$cmd" == 'sha224' ]]; then
hash_lowercase sha224 "$input"
elif [[ "$cmd" == 'SHA224' ]]; then
hash_uppercase sha224 "$input"
elif [[ "$cmd" == 'sha384' ]]; then
hash_lowercase sha384 "$input"
elif [[ "$cmd" == 'SHA384' ]]; then
hash_uppercase sha384 "$input"
elif [[ "$cmd" == 'sha512' ]]; then
hash_lowercase sha512 "$input"
elif [[ "$cmd" == 'SHA512' ]]; then
hash_uppercase sha512 "$input"
elif [[ "$cmd" == 'streebog256' ]]; then
hash_lowercase streebog256 "$input"
elif [[ "$cmd" == 'STREEBOG256' ]]; then
hash_uppercase streebog256 "$input"
elif [[ "$cmd" == 'streebog512' ]]; then
hash_lowercase streebog512 "$input"
elif [[ "$cmd" == 'STREEBOG512' ]]; then
hash_uppercase streebog512 "$input"
elif [[ "$cmd" == 'whirlpool' ]]; then
hash_lowercase whirlpool "$input"
elif [[ "$cmd" == 'WHIRLPOOL' ]]; then
hash_uppercase whirlpool "$input"
elif [[ "$cmd" == 'hashit' ]]; then
echo
echo "Please run at least one of these commands first:"
echo
echo " $ $0 setup"
echo " $ $0 setup-all"
echo
echo "The first command will create script symlinks named for each"
echo "of the most common supported hash types. The second command"
echo "creates more symlinks, for a larger set of supported hash"
echo "types. Then after setup, invoke the script using the name"
echo "of the hash you want to run, e.g. ./sha1 inputstring for a"
echo "lowercase result, or ./SHA1 inputstring for an uppercase"
echo "result."
echo
fi
}
function make_link() {
export type=$1
export dir=`dirname "$script"`
ln -s "$script" "$dir/$type"
}
function make_basic_links() {
for type in md5 sha1 sha256; do
make_link $type
done
}
function make_all_links() {
make_basic_links
for type in gost-mac md4 md_gost94 ripemd160 sha sha224 sha384 sha512 streebog256 streebog512 whirlpool; do
make_link $type
done
}
function hash_lowercase() {
export type="$1"
export input="$2"
if [[ -f "$input" ]]; then
cat "$input" | openssl dgst -$type
else
echo -n "$input" | openssl dgst -$type
fi
}
function hash_uppercase() {
export type="$1"
export input="$2"
if [[ -f "$input" ]]; then
cat "$input" | openssl dgst -$type | tr '[a-z]' '[A-Z]'
else
echo -n "$input" | openssl dgst -$type | tr '[a-z]' '[A-Z]'
fi
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment