You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
1.6 KiB
83 lines
1.6 KiB
#!/bin/sh
|
|
##########################################################################
|
|
# Copyright (C) 2021 Derek Olsen <orpheusui@protonmail.com> #
|
|
##########################################################################
|
|
|
|
# Usage:
|
|
# ./do-hashcheck [file] (trusted checksum)
|
|
#
|
|
# Currently supports invoking md5sum, sha1sum, sha256sum, and sha512sum.
|
|
#
|
|
# Note: This is a Quality-of-Life frontend script, not a hashing algorithm,
|
|
# and it is not a substitute for proper security!
|
|
|
|
c=32
|
|
GO="echo -e \e[$((c))m"
|
|
STOP="echo -e \e[$((c+9))m"
|
|
|
|
# Sanity check 1
|
|
if [[ -z $1 ]]; then
|
|
$STOP'No file'
|
|
exit 1
|
|
fi
|
|
|
|
# Sanity check 2
|
|
# Ask user for prehash
|
|
if [[ -n $2 ]]; then
|
|
echo "do-hashcheck Copyright (C) 2021 Derek Olsen"
|
|
PREHASH=$2
|
|
else
|
|
$GO'Enter trusted hash:'
|
|
read
|
|
elif [[ -z ${REPLY} ]]; then
|
|
$STOP'No hash'
|
|
exit 2
|
|
else
|
|
PREHASH=${REPLY}
|
|
fi
|
|
fi
|
|
|
|
# Sanity check 3
|
|
# Assign algorithm based on length --really hacky and should be replaced with the algorithms themselves
|
|
|
|
if [[ ${#PREHASH} == 32 ]]; then
|
|
ALGO="md5sum"
|
|
elif [[ ${#PREHASH} == 40 ]]; then
|
|
ALGO="sha1sum"
|
|
elif [[ ${#PREHASH} == 64 ]]; then
|
|
ALGO="sha256sum"
|
|
elif [[ ${#PREHASH} == 128 ]]; then
|
|
ALGO="sha512sum"
|
|
|
|
elif [[ -z $3 ]]; then
|
|
$STOP'Unrecognized hash'
|
|
exit 3
|
|
fi
|
|
|
|
# Only repeat the prehash if non-interactive
|
|
|
|
if [[ -n $2 ]]; then
|
|
$GO$PREHASH
|
|
fi
|
|
|
|
# Hash out the file
|
|
|
|
HASH=$($ALGO $1)
|
|
HASH=${HASH:0:${#PREHASH}}
|
|
|
|
# Test
|
|
|
|
i=c-32
|
|
[[ $PREHASH != $HASH ]] && ((i=i-1))
|
|
|
|
# Output
|
|
|
|
if [[ $i == -1 ]]; then
|
|
$STOP$HASH
|
|
$STOP'!! Hashes do not match !!'
|
|
echo "$ALGO"
|
|
exit -1
|
|
else
|
|
$GO$HASH
|
|
echo "$ALGO"
|
|
fi
|