Last active
October 21, 2019 10:00
-
-
Save ngsctt/ce8bc4e2800829b5a8c7e51c8d7cb06c to your computer and use it in GitHub Desktop.
Bash functions to: (1) visually compare two strings, and (2) compare a string with a computed SHA/MD5 checksum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
compstring () { | |
STR_A_IN=$1 | |
STR_B_IN=$2 | |
STR_A_OUT="" | |
STR_B_OUT="" | |
POINTER="" | |
LENGTH=${#STR_A_IN} | |
if [ $# -lt 2 ] || [ -z $1 ]|| [ -z $2 ]; then | |
echo "You must provide two strings to compare" | |
return 1 | |
fi | |
if [ $LENGTH -lt ${#STR_B_IN} ]; then echo less; fi | |
if [ $LENGTH -lt ${#STR_B_IN} ]; then LENGTH=${#STR_B_IN}; fi | |
if [[ $STR_A_IN == $STR_B_IN ]]; then | |
STR_A_OUT=$STR_A_IN | |
STR_B_OUT=$STR_B_IN | |
POINTER="===== IDENTICAL\n" | |
else | |
for (( i=0; i<$LENGTH; i++ )); do | |
if [[ ${STR_A_IN:$i:1} == ${STR_B_IN:$i:1} ]]; then | |
STR_A_OUT+=${STR_A_IN:$i:1} | |
STR_B_OUT+=${STR_B_IN:$i:1} | |
POINTER+=" " | |
else | |
STR_A_OUT+="\033[1;7m${STR_A_IN:$i:1}\033[0m" | |
STR_B_OUT+="\033[1;7m${STR_B_IN:$i:1}\033[0m" | |
POINTER+="^" | |
fi | |
done | |
fi | |
echo | |
echo $STR_A_OUT | |
echo $STR_B_OUT | |
echo $POINTER | |
} | |
comphash () { | |
helptext () { | |
echo "comphash - compare hashes" | |
echo " " | |
echo "comphash [options]" | |
echo " " | |
echo "options:" | |
echo "-h, --help show brief help" | |
echo "-a, --algorithm=HASHCODE specify hashing algorithm" | |
echo " (defaults to md5)" | |
echo "-c, --code=HASHCODE specify hashcode to compare" | |
echo "-f, --file=FILE specify a file to check" | |
} | |
if ! test $# -gt 0; then | |
helptext | |
return 1 | |
fi | |
ALGORITHM="md5" | |
while test $# -gt 0; do | |
case "$1" in | |
-h|--help) | |
helptext | |
return 0 | |
;; | |
-a) | |
shift | |
if test $# -gt 0; then ALGORITHM=$1; fi | |
shift | |
;; | |
--algorithm*) | |
export ALGORITHM=`echo $1 | sed -e 's/^[^=]*=//g'` | |
shift | |
;; | |
-c) | |
shift | |
if test $# -gt 0; then | |
HASH_IN=$1 | |
else | |
echo "no hash code specified" | |
return 1 | |
fi | |
shift | |
;; | |
--code*) | |
export HASH_IN=`echo $1 | sed -e 's/^[^=]*=//g'` | |
shift | |
;; | |
-f) | |
shift | |
if test $# -gt 0; then | |
FILE=$1 | |
else | |
echo "no file specified" | |
return 1 | |
fi | |
shift | |
;; | |
--file*) | |
FILE=`echo $1 | sed -e 's/^[^=]*=//g'` | |
shift | |
;; | |
*) | |
break | |
;; | |
esac | |
done | |
if [[ $ALGORITHM == "md5" ]]; then | |
HASH_COMP=`md5 -q $FILE` | |
elif [[ $ALGORITHM == "sha"* ]]; then | |
HASH_COMP=`shasum -a $(echo $ALGORITHM | sed 's/sha//') $FILE | head -n1 | awk '{print $1;}'` | |
else | |
echo "'$ALGORITHM' not a valid algorithm" | |
return 1 | |
fi | |
compstring $HASH_IN $HASH_COMP | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment