Skip to content

Instantly share code, notes, and snippets.

@pc-magas
Created May 16, 2018 12:58
Show Gist options
  • Save pc-magas/9e0030f635ae2d76d2d3de669a136106 to your computer and use it in GitHub Desktop.
Save pc-magas/9e0030f635ae2d76d2d3de669a136106 to your computer and use it in GitHub Desktop.
SHA256verify hash from a provided hash and file script
#!/bin/bash
#
# Copyright 2018 DESYLLAS DIMITRIOS
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
#################################### DOCUMENTATION #############################
#
# Arguments:
# -f,--file The file you want to check the sha256checksum
# --hash The SHA256 checksum
#
################################################################################
TEMP=`getopt -o f: --long hash:,file: -n 'sha256verify.sh' -- "$@"`
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"
file=""
hashSum=""
while true ; do
case "$1" in
-f|--file) file=$2 ; shift 2 ;;
--hash) hashSum=$2 ; shift 2 ;;
--) shift ; break ;;
*) echo "Uknown Argument \`$1\`" ; exit 1 ;;
esac
done
if [ ! -f ${file} ]; then
>&2 echo "File does not exist"
exit 1;
fi
filehash=$(sha256sum ${file} | awk '{print $1}')
BLUE='\033[0;36m'
NC='\033[0m' # No Color
RED='\033[0;31m'
GREEN='\033[0;32m'
if [ ${filehash} == ${hashSum} ]; then
echo -e "${GREEN}File is not corurpted.${NC}"
echo -e "The provided file: ${BLUE}${file}${NC}"
echo -e "Provided checksum: ${BLUE}${hashSum}${NC}";
else
echo -e "${RED}File seems to be corurpted.${NC}"
echo -e "The provided file: ${BLUE}${file}${NC}"
echo -e "Provided checksum: ${BLUE}${hashSum}${NC}";
echo -e "Calculated checksum: ${BLUE}${filehash}${NC}";
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment