Mastering Algorithms

MD5 (Message Digest 5)

Overview

MD5 (Message Digest 5) is a widely-used cryptographic hash function that produces a 128-bit hash value. Designed by Ronald Rivest in 1991, MD5 was commonly used for data integrity verification and password hashing.

However, MD5 is now considered cryptographically broken and unsuitable for security purposes. Collision attacks can be performed in seconds, making it vulnerable to various security threats. MD5 should not be used for any security-critical applications.

How It Works

MD5 processes input in 512-bit blocks:

  1. Padding: Add padding to make length multiple of 512 bits
  2. Append Length: Add original message length
  3. Initialize State: Set initial hash values
  4. Process Blocks: Apply compression function to each block
  5. Output: Produce 128-bit hash value

Implementation


import hashlib

def md5_hash(data):
    """Compute MD5 hash"""
    return hashlib.md5(data.encode()).hexdigest()

# Example usage
message = "Hello, MD5!"
hash_value = md5_hash(message)
print(f"MD5 Hash: {hash_value}")

# Note: MD5 should NOT be used for security purposes
# Use SHA-256 or SHA-512 instead
                

Specifications

  • Output Size: 128 bits (16 bytes)
  • Block Size: 512 bits
  • Rounds: 64
  • Status: Cryptographically broken

Security Issues

MD5 has serious security vulnerabilities:

  • Collision Attacks: Can find two different inputs with same hash in seconds
  • Pre-image Attacks: Vulnerable to various attack methods
  • No Longer Secure: Should not be used for any security purpose

MD5 collisions were first demonstrated in 2004, and practical collision attacks are now trivial to perform.

When to Use MD5

Do NOT use MD5 for security purposes. It may only be used for:

  • Non-security checksums (file integrity in non-adversarial environments)
  • Legacy system compatibility
  • Educational purposes

For security applications, always use SHA-256 or SHA-512 instead.

Secure Alternatives

  • SHA-256: Recommended for most applications
  • SHA-512: For high-security requirements
  • BLAKE2: Modern, fast hash function
  • Argon2: For password hashing

Related Algorithms

Explore other encryption algorithms: