Created
September 19, 2021 18:57
-
-
Save AaronActu/cd987e185012b983dce40e47168a41de to your computer and use it in GitHub Desktop.
Test integrity of a given file, with a given hash. Supports MD5 and SHA256.
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
#!/bin/bash | |
filename=$1 | |
hash=${2^^} | |
algo=sha256 | |
[[ -n "$3" ]] && algo=$3 | |
if [[ ! -f "$filename" ]]; then | |
echo "$filename" does not exists | |
exit 1 | |
fi | |
echo "Verifying file '$filename' with $algo in progress..." | |
sum="" | |
case "${algo,,}" in | |
"md5") | |
sum=$(md5sum "$filename");; | |
"sha256") | |
sum=$(sha256sum "$filename");; | |
*) | |
echo "${hash} encryption algorythm is not supported" | |
exit 2 | |
;; | |
esac | |
sum=$(echo "$sum" | awk '{print $1}') | |
sum=${sum^^} | |
if [[ "$sum" == "$hash" ]]; then | |
echo "The file's hash match the reference hash : $sum" | |
else | |
echo "The file's hash ($sum) does not match the reference hash ($hash)" | |
exit 255 | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment