Home > Coding > How to Check the MD5 Hash of a File Using Python

How to Check the MD5 Hash of a File Using Python

How to Check the MD5 Hash of a File Using Python

Have you ever downloaded a file and wondered if it’s exactly what the provider intended—no corruption, no tampering? One way to verify this is by checking its MD5 hash, a digital fingerprint that ensures file integrity. In this quick tutorial, I’ll show you how to calculate the MD5 hash of any file using Python with a simple script. Let’s dive in!

What is MD5?

MD5 (Message Digest Algorithm 5) is a widely used cryptographic hash function that produces a 32-character hexadecimal string from any input—like a file. If even one byte changes, the hash changes completely, making it perfect for verification. For example, software providers often share MD5 hashes so you can confirm your download matches theirs.

Why Use Python?

  • It’s free and cross-platform (Windows, Mac, Linux).
  • The hashlib library makes it easy.
  • No complex setup—just a few lines of code!

Sample Python Code to Check MD5

Here’s a Python script that calculates the MD5 hash of a file. Copy this into a file (e.g., check_md5.py), and I’ll explain how it works below.


import hashlib

def calculate_md5(file_path):
    # Create an MD5 hash object
    md5_hash = hashlib.md5()
    
    # Open the file in binary mode
    with open(file_path, "rb") as file:
        # Read the file in chunks to handle large files efficiently
        for chunk in iter(lambda: file.read(4096), b""):
            md5_hash.update(chunk)
    
    # Return the hexadecimal representation of the hash
    return md5_hash.hexdigest()

# Example usage
if __name__ == "__main__":
    file_to_check = input("Enter the path to your file: ")
    try:
        md5_result = calculate_md5(file_to_check)
        print(f"MD5 Hash: {md5_result}")
    except FileNotFoundError:
        print("Error: File not found. Please check the path.")
    except Exception as e:
        print(f"An error occurred: {e}")

How to Use This Script

  1. Install Python: If you don’t have it, download it from python.org.
  2. Save the Code: Copy the script above into a file named check_md5.py.
  3. Run It: Open a terminal, navigate to the file’s directory, and type python check_md5.py.
  4. Enter a File Path: Type the path to your file (e.g., C:\Downloads\example.zip on Windows or /home/user/example.zip on Linux/Mac).
  5. Check the Output: You’ll see a 32-character MD5 hash like d41d8cd98f00b204e9800998ecf8427e.

How It Works

  • hashlib.md5(): Creates an MD5 hash object from Python’s built-in hashlib library.
  • open(file_path, "rb"): Opens the file in binary mode—crucial for consistent hashing across platforms.
  • file.read(4096): Reads the file in 4KB chunks to avoid memory issues with large files.
  • md5_hash.update(chunk): Updates the hash with each chunk.
  • hexdigest(): Outputs the final hash as a readable string.

Try It Out!

Download a file with a known MD5 hash (e.g., from a software site), run this script, and compare the result. For example, if a site lists 5d41402abc4b2a76b9719d911017c592 for example.txt, and your script matches it, the file’s intact!

Pro Tip: MD5 isn’t cryptographically secure for passwords (use SHA-256 instead), but it’s fine for integrity checks.

Conclusion

With just a few lines of Python, you can verify any file’s integrity using MD5. Try tweaking the script—add a comparison feature or support for multiple files! Have questions or ideas? Drop a comment below.

Leave a Comment