Downloaded File: Automated Verification Update I
Ok last time I made a tool that helps with file verification using the checkum of that file entered manually. Now there is another method that is also used and that is the use of a checksum file, usually named MD5SUM or SHA256.
Note both files have to be in the same folder!
I updated my earlier tool:
With this version you can enter the name of a file that holds the calculated checksums of the file in question and use that to verify that your file has not been tampered with. It works similar to the other tool but in this case the first step will be to choose either a manual entry or the use of a file for verification.
Here is the updated code:
#!/usr/bin/env bashset -e# Create variables to hold the algorithm to use
opt1="sha256"
opt2="md5sum"
aopt1="Use File Authentication"
aopt2="Enter Check Manually"# Declare the functions to be used# Create a function to use
# for repeated tasks
function remove_whitespaces {
# Remove white space
r="$(printf "%s" "$r" | tr -d '[:space:]')" # Get the checksum for verification from user
shasum="$(zenity --height=200 --width=350 --entry --title "Checksum Value" --text "Please enter the checksum" --entry-text "add checksum here")" # Remove white space
shasum="$(printf "%s" "$shasum" | tr -d '[:space:]')"
if [[ "$shasum" == "add checksum here" || "$shasum" == " " ]]
then
zenity --error --text "A checksum is required! "
exit 1
else
if [[ "$r" == "$shasum" ]]
then
zenity --info --height=100 --width=100 --title "Success! " --text "The file is ok! "
exit 0
else
zenity --height=100 --width=200 --error --text "Checksum mismatch! "
exit 1
fi
fi}function checksumfile_success {zenity --info --height=100 --width=100 --title "Success! " --text "The file is ok! "
exit 0}function checksumfile_failure {
zenity --height=100 --width=100 --error --text "Checksume mismatch! "
exit 1}# Decide if a checksum should be entered manually
# or a checksum file should be used
authtype="$(zenity --height=275 --width=300 --list --radiolist --text "Choose authentication method" --column 'Select...' --column 'Method' FALSE "$aopt1" FALSE "$aopt2")"if [[ "$authtype" == "$aopt1" ]]
then
checksumfile="$(zenity --height=200 --width=30 --entry --title "Checksum File Name" --text "Enter the checksum file to use" --entry-text "Enter the filename here")"
# If user made a choice
if [[ ! "$checksumfile" == " " ]]
then
# Search for file name is present in
# current directory
fexists="$(find . -maxdepth 1 -type f -name "$checksumfile" | wc -l)" # If file exists
if [[ "$fexists" -gt 0 ]]
then
algotype="$(zenity --height=275 --width=300 --list --radiolist --text "Choose Algorithm to Use" --column 'Select...' --column 'Algorithm' FALSE "$opt1" FALSE "$opt2")" # Remove white space
checksumfile="$(printf "%s" "$checksumfile" | tr -d '[:space:]')" # Use selected algorithm if [[ "$algotype" == "$opt1" ]]
then
# Carry out sha56 on the file
result="$(grep -Eo "OK" <<< "$("$algotype" -c "$checksumfile")")" # Check if the file is ok
if [[ "$result" == "OK" ]]
then
cheksumfile_success
else
checksumfile_failure
fi
elif [[ "$algotype" == "$opt2" ]]
then
# Do md5sum on the file
result="$(grep -Eo "OK" <<< "$("$algotype" -c "$checksumfile")")" # Check if the file is ok
if [[ "$result" == "OK" ]]
then
checksumfile_success
else
checksumfile_failure
fi
else
exit 1
fi
else
exit 1
fi
fi
elif [[ "$authtype" == "$aopt2" ]]
then
algotype="$(zenity --height=275 --width=300 --list --radiolist --text "Choose Algorithm to Use" --column 'Select...' --column 'Algorithm' FALSE "$opt1" FALSE "$opt2")"
# Use selected algorithm
if [[ "$algotype" == "$opt1" ]]
then
# Carry out md5sum on the file
r="$(grep -Eo "^.* " <<< "$($algotype -a "$1")")"
remove_whitespaces
elif [[ "$algotype" == "$opt2" ]]
then
# Do sha256 on the file
r="$(grep -Eo "^.* " <<< "$($algotype "$1")")"
remove_whitespaces
else
exit 1
fi
else
exit 1
fiexit 0
This is how you use it: