File integrity with MD5 Hashes on Linux

I have talked a few times about the usefullness of hashing programs. They are central to password security and encryption; which most internet users use on a daily basis (most of the times transparently). Another helpful use of hashing can be for checking file integrity. By comparing two hashes of a file, you can tell if that file was modified (even 1 bit) between the time of those two hashes.

If you are on linux, you probably have the program/command md5sum builtin. By piping some text to md5sum, you can calculate a md5 hash of it:

$ echo -n "Hello" | md5sum
> 8b1a9953c4611296a827abf8c47804d7

But md5sum is especially useful for checking whole files like so:

$ md5sum file1.txt file2.doc file3.iso file4.mp3
> e42e2699cda3405fe484f951836d5f5a  file1.txt
> 0692b01aeca5db59c762ad7d16bc0d35  file2.doc
> 01c3ec3dd8e601c81d7fca5151f415ae  file3.iso
> 7bf30953798adad01dc413da8ce5252c  file4.mp3

Running the command in this way simply prints the output to the terminal. If you save the output to a file, you can use md5sum to compare the file hashes later on using the ––check/–c flag like so:

$ md5sum file1.txt file2.doc file3.iso file4.mp3 > integrity_hashes.md5
$ md5sum -c integrity_hashes.md5
> file1.txt: OK
> file2.doc: OK
> file3.iso: OK
> file4.mp3: OK

If you modify one of files and run this integrity check, md5sum will output a FAILED message beside the filename instead of an OK.

Things like this are why I love Linux. I was just thinking one day, “I wonder what the easiest way to calculate hashes of my files is”, and a quick google search showed me that I have the handy md5sum tool already builtin and it does exactly what I want.

Linux FTW!!!

Advertisements
File integrity with MD5 Hashes on Linux