#!/bin/sh NORM="\033[0m" CYN="\033[0;36m" GRN="\033[0;32m" RED="\033[0;31m" printhelp() { echo "\ Usage: $(basename "$0") [options] [file/directory]... OPTIONS: -m, --md5 Create/verify md5 checksum -s[alg], --sha[alg] Create/verify sha checksum -x[alg], --xxh[alg] Create/verify xxh checksum -c [alg], --crypt [alg] Create/verify [alg] checksum (uses [alg]sum) -v, --verify Verify files with existing checksums -h, --help Print help \ "; } crypt=xxh while true; do case "$1" in -m|--md5) crypt=md5 ;; -s*|--sha*) crypt=sha$(echo "$1" | tr -cd [:digit:]) ;; -x*|--xxh*) crypt=xxh$(echo "$1" | tr -cd [:digit:]) ;; -c|--crypt) crypt=$2 && shift ;; # Shift twice -v|--verify) verify=1 ;; -h|--help) printhelp; exit 0 ;; -*) printhelp; exit 2 ;; *) break ;; esac shift done hashalg=${crypt}sum # Works for current algs command -v $hashalg >/dev/null || (echo "Hashing algorithm '$hashalg' does not exist!" && exit 2) while [ -n "$1" ]; do file="$1" [ ! "`echo "$file" | sed "s/.$crypt$//"`" = "$file" ] && shift && continue # Skip hash files [ ! -e "$file" ] && echo "file '$file' does not exist!" && exit 1 [ -d "$file" ] && type=directory || type=file hashfile="$file.$crypt" [ -n "$verify" ] && [ ! -e "$hashfile" ] && shift && continue # Skip files without a hash in verify mode echo "Getting $crypt checksum of $type '$file'..." hash=`find "$file" -type f -print0 | sort -z | xargs -r0 pv -EE -F'%r [%b] [%t] [%e] %p' | $hashalg | cut -d' ' -f1` if [ -e "$hashfile" ]; then phash=`cat "$hashfile"` [ $phash = $hash ] \ && echo "${GRN}Success: $type matches saved checksum!" \ || (echo "${RED}Error: $type does not match saved checksum!\nExpected checksum: $phash\nActual checksum: $hash" && ec=1) else echo $hash > "$hashfile" \ && echo "${CYN}$crypt checksum file saved at '$hashfile'!" \ || (echo "${RED}'$hashfile' could not be created!\n$crypt checksum of $type '$file' is $hash" && ec=1) fi printf "$NORM"; [ -n "$2" ] && echo shift done exit $ec