What is MD5?
MD5 (Message Digest Algorithm 5) is a widely used cryptographic hash function that produces a 128-bit (16-byte) hash value. Developed by Ronald Rivest in 1991, it was designed as a successor to MD4.
How MD5 Works
MD5 processes input data in 512-bit blocks through four rounds of operations:
- Padding: Input is padded to ensure its length is congruent to 448 modulo 512
- Append Length: Original message length is appended as a 64-bit value
- Initialize MD Buffer: Four 32-bit registers are initialized with specific constants
- Process Blocks: Each 512-bit block goes through 64 operations using nonlinear functions
- Output: The final 128-bit state is the MD5 hash
Characteristics
Fast
Very quick computation, suitable for checksums
Fixed Size
Always produces 32 hexadecimal characters
Deterministic
Same input always produces same output
Broken
Vulnerable to collision attacks
Common Use Cases
- File Checksums: Verify file integrity during transfers
- Cache Busting: Generate unique identifiers for cached resources
- Data Deduplication: Identify duplicate files or data
- Non-security Hashing: Quick hashing where security isn't critical
JavaScript
// Using crypto-js library
const CryptoJS = require('crypto-js');
const hash = CryptoJS.MD5('Hello World').toString();
console.log(hash); // b10a8db164e0754105b7a99be72e3fe5
// Using Node.js crypto module
const crypto = require('crypto');
const hash = crypto.createHash('md5')
.update('Hello World')
.digest('hex');
// Hash a file
const fs = require('fs');
const fileBuffer = fs.readFileSync('file.txt');
const fileHash = crypto.createHash('md5')
.update(fileBuffer)
.digest('hex');
Python
import hashlib
# Hash a string
text = "Hello World"
hash_object = hashlib.md5(text.encode())
hex_dig = hash_object.hexdigest()
print(hex_dig) # b10a8db164e0754105b7a99be72e3fe5
# Hash a file
def hash_file(filename):
h = hashlib.md5()
with open(filename, 'rb') as file:
while chunk := file.read(8192):
h.update(chunk)
return h.hexdigest()
file_hash = hash_file('file.txt')
PHP
// Hash a string
$text = "Hello World";
$hash = md5($text);
echo $hash; // b10a8db164e0754105b7a99be72e3fe5
// Hash with binary output
$binaryHash = md5($text, true);
// Hash a file
$fileHash = md5_file('file.txt');
// Hash with salt (for non-security purposes)
$salt = "mysalt";
$saltedHash = md5($salt . $text);
Command Line
# Linux/Mac
echo -n "Hello World" | md5sum
# b10a8db164e0754105b7a99be72e3fe5 -
# Mac alternative
echo -n "Hello World" | md5
# b10a8db164e0754105b7a99be72e3fe5
# Hash a file
md5sum file.txt
# Windows
certutil -hashfile file.txt MD5
Security Vulnerabilities
Critical Security Warning
MD5 has been cryptographically broken since 2004. It is vulnerable to:
- Collision Attacks: Different inputs can produce the same hash
- Preimage Attacks: Finding an input that produces a specific hash
- Length Extension Attacks: Appending data to a message without knowing the original
What NOT to Use MD5 For
Password Storage
Use bcrypt, scrypt, or Argon2 instead
Digital Signatures
Use SHA-256 or SHA-3
SSL Certificates
Use SHA-256 minimum
Security Tokens
Use HMAC with SHA-256
Safe Uses for MD5
- Non-cryptographic checksums (file integrity in trusted environments)
- Hash table keys
- Cache identifiers
- Non-security data deduplication
Security Warning: MD5 is Broken
MD5 should NOT be used for security purposes
MD5 has been cryptographically broken since 2004. Collision attacks can be performed in seconds on modern hardware. It's vulnerable to:
- Collision Attacks: Finding two different inputs with the same MD5 hash
- Chosen-Prefix Collisions: Creating collisions with specific prefixes
- Length Extension Attacks: Appending data without knowing the original input
Use SHA-256 or SHA-3 for cryptographic purposes instead.
Common Use Cases (Non-Security)
File Verification
Verify downloaded files haven't been corrupted during transfer (not for security)
Database Indexing
Create fixed-length identifiers for variable-length data in databases
Cache Keys
Generate cache keys from complex objects or long strings
Duplicate Detection
Find duplicate files or records by comparing MD5 hashes
MD5 Hash Examples
| Input |
MD5 Hash |
| "" (empty string) |
d41d8cd98f00b204e9800998ecf8427e |
| "Hello World" |
b10a8db164e0754105b7a99be72e3fe5 |
| "The quick brown fox jumps over the lazy dog" |
9e107d9d372bb6826bd81d3542a419d6 |
| "123456" |
e10adc3949ba59abbe56e057f20f883e |
Frequently Asked Questions
Q: Is MD5 secure for passwords?
A: No, MD5 is completely insecure for password storage. Use bcrypt, Argon2, or scrypt instead. MD5 can be cracked at billions of hashes per second on consumer GPUs.
Q: Can I reverse an MD5 hash?
A: MD5 is a one-way function and cannot be mathematically reversed. However, rainbow tables and brute force can find common inputs.
Q: Why do some websites still use MD5?
A: MD5 is still used for non-security purposes like checksums, cache keys, and legacy system compatibility. It's fast and produces consistent results.
Q: What's the difference between MD5 and SHA-256?
A: SHA-256 is cryptographically secure, produces 256-bit output (vs 128-bit), and is slower but still fast enough for most applications. Always use SHA-256 for security.
Q: How fast is MD5?
A: MD5 can hash gigabytes per second on modern CPUs. This speed makes it useful for checksums but terrible for password security.
Technical Specifications
| Digest Size: |
128 bits (16 bytes) |
| Block Size: |
512 bits (64 bytes) |
| Rounds: |
64 (4 rounds × 16 operations) |
| First Published: |
April 1992 (RFC 1321) |
| Cryptanalysis: |
Broken (collisions found in 2004) |
| Speed: |
~3-5 GB/s on modern CPUs |