Check a File's Integrity Via Checksums

Check a File's Integrity Via Checksums

Being able to verify that a file has not been modified since it was created or signed is integral to the modern world, without this technology digital signatures would not exist and verifying the origin and integrity of downloads would be impossible. This tutorial is a simple walk through on how you can create checksum values of files and later verify the original file’s integrity against the checksum. We will be using the GNU Core Utilities checksum tools for this tutorial, if you are running Linux (should also be the same for Mac) there is no additional software to install.

Generate a Checksum

The first step is to generate a checksum of the file and save it to another checksum file. This checksum file contains the original file’s hash and the path/to/the/file. IMPORTANT NOTE: Do not move the original file without updating the file directory path in the checksum file. We will generate a checksum and save it to a checksum file using the following command:

							
							
					sha256sum filename > checksumfilename.sha256				
			

In this case we are using the SHA256SUM cryptographic hash function for hashing our “filename” file. There are several other hash functions included in the GNU Core Utilities such as MD5, SHA1, SHA384, and SHA512. This tutorial does not get into the technicalities of hashing and checksums, but basically the larger the number the better the hash (excepting MD5 which is the lowest). The checksumfilename file extension always matches the checksum function used.

Now that we have generated a checksum file containing the checksum of our original function we can check the integrity of our original file by running:

							
							
					sha256sum -c checksumfilename.sha256				
			

This will re-generate the checksum of the original file and check it against the checksum contained in the checksumfilename.sha256. If the checksums are the same (the original file has not changed (and assuming the checksumfilename.sha256 file has not been altered)) then you will get an OK response. If however the checksums are different (which means the original file was modified or the checksumfilename.sha256 file was altered) then you will receive a FAILED response. 

You can test this whole functionality out by creating a test.txt file, generating a checksum file of the test.txt file and then checking the test.txt file against the checksum file, you should receive an OK result. Then modify the test.txt file and again check it against the checksum file, it should return FAILED. 

That’s it, you have successfully generated a checksum of a file, saved it to a checksum file and verified the original file’s integrity!

Walter Miely is a tech entrepreneur and CEO of Phoenix Ignited Tech You can find him on Linkedin. This material is licensed under the CC BY 4.0 License LEGAL DISCLAIMER: The content provided here is provided AS IS, and part of, or the entirety of this content may be incorrect. Please read the entireLegal Disclaimer here.