Linux trivia: check SHA checksum
This is a small Linux trivia post about checking SHA checksums. To compute a SHA checksum is very straightforward, for example for sha256sum (other sha variants should work similarly):
jj@MBX20:~/Downloads$ sha256sum besseggen.jpg
3a48c6e5bfcf1f3d50d908bdbcd776555dbab437d65b39673bfffa70af07da02 besseggen.jpg
However, the “usual” case is that you have downloaded a file, and you should not only compute its sha256, but also check it against an expected value. This is a tiny more complex to do, as there is not, as far as I know, a simple way to do it only using the sha256sum
command in a simple one-liner. The best solution I know of is to do something like:
jj@MBX20:~/Downloads$ echo "3a48c6e5bfcf1f3d50d908bdbcd776555dbab437d65b39673bfffa70af07da02 besseggen.jpg" | sha256sum --check
besseggen.jpg: OK
Of course, this is a bit long to type, and the full command, while simple, is easy to forget if you use it only now and then. A simple solution for this is to write a small helper function in bash, for example a function similar to this. This function can then simply be sourced (either by hand or automatically in your .bashrc
):
jj@MBX20:~/Downloads$ source sha256check.sh
jj@MBX20:~/Downloads$ sha256check besseggen.jpg 3a48c6e5bfcf1f3d50d908bdbcd776555dbab437d65b39673bfffa70af07da02
besseggen.jpg: OK
Simpler, right? :)