md5sum Command: Tutorial & Examples

Computing and comparing hash codes

The md5sum command is a pre-installed utility in most Linux distributions that is used to compute and check MD5 (Message-Digest algorithm 5) hashes. This command calculates an almost unique 128-bit signature for each input of data. It is commonly used to verify the integrity of files. However, it is not considered to be secure anymore for integrity checks, as it is vulnerable to hash collisions, where different data inputs can produce the same hash output, making it feasible for an attacker to manipulate data without detection.

How Does md5sum Work?

The md5sum command implements the MD5 cryptographic hash function. When the md5sum command is executed, it takes a file as input and produces a 32-character hexadecimal number. This number is the file's MD5 hash or checksum. The primary purpose of this checksum is to compare it with other MD5 hashes. If the hashes are the same, then the files are identical. However, if they are different, it means the files have differences, whether they are minor or major.

Importance of md5sum

The md5sum command is important for verifying file integrity. When you download a file, especially large ones, there's a chance that they could become corrupted during the download process. By comparing the MD5 hash of the file you have with the hash provided by the file's source, you can verify that your downloaded file has not been tampered with or corrupted.

However, the md5sum command is not recommended for security critical uses. It is vulnerable to hash collisions and therefore not considered secure anymore in the cybersecurity world. Better hashing algorithms such as SHA256 or SHA3 are recommended.

Using the md5sum Command

To use the md5sum command, you simply need to type md5sum followed by the file name. Here's an example:

md5sum myfile.txt

This command will output the MD5 hash of myfile.txt. The output will look something like this:

68b329da9893e34099c7d8ad5cb9c940  myfile.txt

The string of numbers and letters before myfile.txt is the file's MD5 hash.

Common Parameters of md5sum

The md5sum command has several parameters that you can use to modify its behavior. Here are a few common ones:

  • -b or --binary: Read in binary mode.
  • -c or --check: Read MD5 sums from the FILEs and check them.
  • -t or --text: Read in text mode (default).
  • --status: Exit with zero status when no check sums are missing.

Checking File Integrity with md5sum

To check the integrity of a file, you can use the -c or --check option. Here's an example:

echo '68b329da9893e34099c7d8ad5cb9c940  myfile.txt' | md5sum -c -

This command will output:

myfile.txt: OK

If the file's MD5 hash matches the one you provided. If they don't match, you'll see:

myfile.txt: FAILED

Conclusion

While the md5sum command is useful for basic file integrity checks, it's not considered secure for more critical uses due to its vulnerability to hash collisions. For those cases, you should use more secure hashing algorithms such as SHA256 or SHA3.

The text above is licensed under CC BY-SA 4.0 CC BY SA