Skip to content

Instantly share code, notes, and snippets.

@JayWalker512
Created December 13, 2013 01:54
Show Gist options
  • Save JayWalker512/7938758 to your computer and use it in GitHub Desktop.
Save JayWalker512/7938758 to your computer and use it in GitHub Desktop.
Functions for creating checksum files and easily testing them against the parent file. These go in your .bashrc file. Works on Linux.
md5sc()
{
local target="$1"
local checksumString=$(md5sum "$target")
local checksum=${checksumString:0:32}
echo "$checksum" > "$target.md5"
echo "$checksum $target"
}
md5v() #md5 verifies a file based on it's .md5 sidecar file. return 0 == passed.
{
local target="$1"
local targetNameLength=${#target}
let targetNameLength-=4
local targetExtension=${target:targetNameLength:4}
local controlChecksum=0 #checksum in sidecar file
local testChecksumTarget=0 #file to create checksum from
if [ "$targetExtension" == ".md5" ]; then
controlChecksum=$(cat "$target")
controlChecksum=${controlChecksum:0:32}
testChecksumTarget=${target:0:targetNameLength}
else
controlChecksum=$(cat "$target.md5")
controlChecksum=${controlChecksum:0:32}
testChecksumTarget="$target"
fi
local testChecksum=0 #checksum of file we're verifying
if [ -e "$testChecksumTarget" ] && [ "$controlChecksum" != "" ]; then
local testChecksumString=$(md5sum "$testChecksumTarget")
testChecksum=${testChecksumString:0:32}
if [ "$controlChecksum" == "$testChecksum" ]; then
echo "Control: $controlChecksum"
echo "Test : $testChecksum"
echo "$testChecksumTarget passed verification!"
return 0;
else
echo "Control: $controlChecksum"
echo "Test : $testChecksum"
echo "$testChecksumTarget FAILED verification!"
return 1;
fi
else
echo "Sidecar or target file missing or unreadable."
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment