How to check the integrity of a file using the MD5 hash

Jonas Maro
2 min readJan 28, 2022

--

Pexels

MD5 is a one-way hash algorithm as defined in RFC1321 and can be used to help determine the integrity of a file by providing a 128-bit digital signature. This digital signature is like a fingerprint for a file. Changing a single byte in a file will result in a different MD5 hash.

MD5 hashes can be used to catalog files on a file system and then determine at a later date that the files have not been tampered with in any way, for example if someone accessed a system and modified the files on it.

They can also be used to ensure that a downloaded file from a website is the same as expected. This can be especially important when downloading a file from a mirror, to make sure you are not installing a modified program that contains a Trojan or other malware.

You can check how an MD5 Hash code generator works, here. Or just type ‘MD5 generator’ into your browser, and you’ll get plenty of results.

How to check the integrity of a file using the MD5 hash

This is very useful when downloading a distribution of an operating system, which we will later install. Usually, on the mirrors from where these images are downloaded, there is usually a file called MD5SUMS (although SHA256 is increasingly used). These files indicate the MD5 hashes corresponding to the different hosted files. Thus, once downloaded, you can perform the checksum on them and check that the file has really been downloaded correctly.

By simply comparing the MD5 hash of a file, you have downloaded from the mirror, with the one into the MD5SUMS file, you can determine if the file is exactly the same.

To check the MD5 hash of any file, on Linux or MacOS, assuming ‘filename.ext’ as our target file, you simply have to run the following command:

$ md5sum filename.ext

This command will depend on the Linux distribution you are using. On other distributions, the command may be:

$ md5 filename.ext

In both cases, after executing the command, the terminal output is something similar to this:

c4e5f7fcbcef75924b2abde2b2e75f3f    filename.ext

where you can see the MD5 hash belonging to the file in question.

Using the MD5 hash we can be sure of the integrity of any file, since the simple change of a single byte in it will make the MD5 hash completely different, so if this is the case, we will have to re-download the file, in the case of downloads from the Internet, or to recover it from the last backup, in the case of files on our system.

--

--