Skip to content

Instantly share code, notes, and snippets.

@tcely
Last active September 21, 2023 04:21
Show Gist options
  • Save tcely/0de3da1cd08d83902673 to your computer and use it in GitHub Desktop.
Save tcely/0de3da1cd08d83902673 to your computer and use it in GitHub Desktop.
replace 'sha256sum -c' with openssl / cmp in bash
#!/bin/bash
sha256sumc ()
{
local err file hash out rc=0;
while IFS=' ' read -r hash file; do
file="${file#[*]}";
out="$(openssl dgst -sha256 -r "$file" 2>/dev/null)" && cmp -s <(echo "$out") <(printf -- '%s *%s\n' "$hash" "$file") && printf -- '%s: OK\n' "$file" || {
printf -- '%s: FAILED' "$file";
err="$(openssl dgst -sha256 -r "$file" 2>&1 >/dev/null)";
if [[ "$err" =~ ': No such file or directory'$'\n' ]]; then
printf -- ' %s' 'open or read';
fi;
printf -- '\n';
rc=1
};
done <"${1:-/dev/stdin}" || rc=$?;
return $rc
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment