Skip to content

Instantly share code, notes, and snippets.

@kou1okada
Last active August 29, 2015 14:18
Show Gist options
  • Save kou1okada/c74c05c21f60086d1837 to your computer and use it in GitHub Desktop.
Save kou1okada/c74c05c21f60086d1837 to your computer and use it in GitHub Desktop.
Read md5 and sha{1,224,256,384,512} sums from the FILEs and check them.
#!/usr/bin/env bash
#
# Copyright (c) 2015 Koich OKADA. All rights reserved.
# This script is distributed under the MIT license.
#
# dummy command for unknown sum.
# Usage: unknownsum
function unknownsum ()
{
return 1
}
# Determine the hash method from a HASH.
# Usage: hash_method HASH
function hash_method ()
{
case "${#1}" in
32) echo md5 ;;
40) echo sha1 ;;
56) echo sha224 ;;
64) echo sha256 ;;
96) echo sha384 ;;
128) echo sha512 ;;
*) echo unknown ;;
esac
}
# Read md5 and sha{1,224,256,384,512} sums from the FILEs and check them.
# Usage: hash_check [FILE ...]
function hash_check ()
{
local f0 f1 f2 method count=0
while true; do
while read f0; do
f1="${f0% *}"
f2="${f0#*\*}"
method="$(hash_method "$f1")sum"
if echo "$f0" | "$method" -c --status; then
echo "hash_check: $method: $f2: OK"
else
echo "hash_check: $method: $f2: FAILED"
count=$(( count + 1 ))
fi
done 0<${1:-<(cat)}
shift
(( $# <= 0 )) && break
done
if (( 0 < count )); then
echo "hash_check: WARNING: $count computed checksum did NOT match"
return 1
fi
return 0
}
hash_check "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment