by coldpie · 3y ago · 0 comments
$ echo hi > some_file
$ shasum -a 256 some_file > check
$ cat check
98ea6e4f216f2fb4b69fff9b3a44842c38686ca685f3f55dc48c5d3fb1107be4 some_file
$ shasum -a 256 -c check
some_file: OK
$ echo $?
0
$ echo bye > some_file
$ shasum -a 256 -c check
some_file: FAILED
shasum: WARNING: 1 computed checksum did NOT match
$ echo $?
1
Edit: Oh cool, at least perl's shasum allows reading from stdin so you can even skip the file if you're just copying some check file off the software's website: $ shasum -a 256 -c - <<EOF
> 98ea6e4f216f2fb4b69fff9b3a44842c38686ca685f3f55dc48c5d3fb1107be4 some_file
> EOF
some_file: OKWhen people claim they wrote a prediction at some later date, they always have to document the EXACT command used to avoid this, e.g. `echo "smart prediction" | md5sum`
hash="4e575a5ee4af2925477c9eea887ff560d23a586dbaf90b616d26c47ec429ca13"
[[ "$hash" == "$(shasum -a 256 file | awk '{print $1 }')" ]] && echo "Valid" || echo "Invalid checksum"
I use that little if-statement in some build systems. echo '98ea6e4f216f2fb4b69fff9b3a44842c38686ca685f3f55dc48c5d3fb1107be4 some_file' | sha256sum -c
also works (with and without passing `-n` to echo, because the `-c` option ensures the file is checked without even noticing any new line). Thanks :)