MD5
Header: #include <cryptopp/md5.h> | Namespace: CryptoPP::Weak
Since: Crypto++ 1.0; in the Weak namespace since Crypto++ 5.5
Thread Safety: Not thread-safe per instance; use separate instances per thread
MD5 is completely broken and must not be used for any security purpose. [1]
The first MD5 collisions were published in 2004, and collisions can now be found in seconds on an ordinary PC. It is included only for:
- Legacy system compatibility
- Checksums that protect only against errors, which RFC 6151 still treats as acceptable
For any security application, use SHA-256, SHA-3, or BLAKE3.
MD5 is a message digest algorithm with a 128-bit output, designed by Ronald Rivest and published as RFC 1321 in April 1992. RFC 6151 says MD5 is no longer acceptable where collision resistance is required, such as digital signatures.
Quick Example
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
#include <cryptopp/md5.h>
#include <cryptopp/hex.h>
#include <cryptopp/filters.h>
#include <iostream>
int main() {
using namespace CryptoPP;
// WARNING: MD5 is broken - use only for legacy compatibility!
Weak::MD5 hash;
std::string message = "Hello, World!";
std::string digest, hexOutput;
StringSource(message, true,
new HashFilter(hash,
new StringSink(digest)
)
);
StringSource(digest, true,
new HexEncoder(new StringSink(hexOutput))
);
std::cout << "MD5: " << hexOutput << std::endl;
// Output: 65A8E27D8879283831B664BD8B7F0AD4
return 0;
}The Weak Namespace
Crypto++ places MD5 in the Weak namespace to discourage its use:
// Method 1: Enable weak algorithms explicitly
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
#include <cryptopp/md5.h>
CryptoPP::Weak::MD5 hash;
// Method 2: Without the define, you get a compiler warning
#include <cryptopp/md5.h>
CryptoPP::MD5 hash; // Warning: using weak algorithm
Without the define, md5.h imports the class into CryptoPP and emits a #warning (GCC and Clang) or #pragma message (other compilers). Defining CRYPTOPP_ENABLE_NAMESPACE_WEAK to 1 before the include makes the class available as Weak::MD5 without the warning.
Class: Weak::MD5
Constants
static const int DIGESTSIZE = 16; // 128 bits (16 bytes)
static const int BLOCKSIZE = 64; // 512 bits (64 bytes)
Methods
Update()
void Update(const byte* input, size_t length);Add data to hash computation.
Final()
void Final(byte* digest);Finalize and get 16-byte digest.
Restart()
void Restart();Reset to initial state. Final() also restarts the object.
Complete Example: Legacy System Compatibility
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
#include <cryptopp/md5.h>
#include <cryptopp/hex.h>
#include <cryptopp/filters.h>
#include <iostream>
using namespace CryptoPP;
// Only for legacy compatibility - NOT for security!
std::string md5Hash(const std::string& input) {
Weak::MD5 hash;
std::string digest, hexDigest;
StringSource(input, true,
new HashFilter(hash,
new StringSink(digest)
)
);
StringSource(digest, true,
new HexEncoder(new StringSink(hexDigest))
);
return hexDigest;
}
int main() {
// Example: Verify against legacy system's MD5 hash
std::string data = "test";
std::string hash = md5Hash(data);
std::cout << "MD5 (legacy check): " << hash << std::endl;
// Output: 098F6BCD4621D373CADE4E832627B4F6
// Compare with expected legacy hash
if (hash == "098F6BCD4621D373CADE4E832627B4F6") {
std::cout << "Legacy hash matches!" << std::endl;
}
return 0;
}Security
MD5 Is Completely Broken
| Result | Year | Cost | Impact |
|---|---|---|---|
| Pseudo-collisions for the compression function | 1993 | Academic | |
| Compression-function collision with a chosen initial value | 1996 | Academic | |
| First two collision pairs for MD5 | 2004 | Critical | |
| Collision in about one minute on a notebook PC | 2006 | About one minute | Critical |
| Collision in 10 seconds or less on a 2.6 GHz Pentium 4 | 2007 | Seconds | Critical |
| Preimage faster than exhaustive search | 2009 | 2^123.4 | Still impractical |
1 and 2.2.
Real-World Attacks
Colliding certificates: MD5 collision attacks have been applied to X.509 certificates, including a rogue CA certificate, demonstrated on 30 December 2008 and described in a Crypto 2009 paper.
Flame Malware (2012): Flame, uncovered in May 2012, used a previously unknown variant of the chosen-prefix collision attack on MD5.
What MD5 Cannot Do
- Digital signatures - MD5 is no longer acceptable where collision resistance is required
- Certificates - Collision attacks have produced colliding and rogue certificates
- Password hashing - We recommend Argon2id
- File integrity (security) - An attacker who can prepare both files can give them the same MD5 hash
- New protocol designs - RFC 6151 says they should not use HMAC-MD5 either
Limited Uses
- Error-detecting checksums - Where MD5 only protects against errors
- Legacy compatibility - When you must interface with old systems
- HMAC-MD5 in existing protocols - RFC 6151 says stopping it is not urgent, but new designs should not use it; alternatives include HMAC-SHA256 and AES-CMAC
Migration Guide
Replace MD5 with SHA-256
// OLD (broken)
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
#include <cryptopp/md5.h>
CryptoPP::Weak::MD5 hash;
byte digest[16];
hash.Final(digest);
// NEW (secure)
#include <cryptopp/sha.h>
CryptoPP::SHA256 hash;
byte digest[32];
hash.Final(digest);For Password Hashing
// OLD (completely wrong)
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
#include <cryptopp/md5.h>
Weak::MD5 hash;
hash.Update(reinterpret_cast<const byte*>(password.data()), password.size());
hash.Final(passwordHash); // NEVER DO THIS
// NEW (correct)
#include <cryptopp/argon2.h>
Argon2 argon2(Argon2::ARGON2ID);
argon2.DeriveKey(derivedKey, derivedLen,
reinterpret_cast<const byte*>(password.data()), password.size(),
salt, saltLen,
3, 65536, 4); // t = 3 passes, m = 65536 KiB (64 MiB), p = 4 lanes
The DeriveKey() overload takes the time cost, memory cost in KiB and parallelism in that order. These values are RFC 9106’s second recommended option for memory-constrained environments (Argon2id, t = 3, p = 4, 64 MiB).
MD5 vs Modern Alternatives
| Property | MD5 | SHA-256 | BLAKE3 |
|---|---|---|---|
| Collision resistance | Broken; collisions in seconds | 128 bits | 128 bits |
| Preimage resistance | Best known attack 2^123.4 | 256 bits | 128-bit specification target, 256 bits intended |
| Digest size | 16 bytes | 32 bytes | 32 bytes by default |
| Recommendation | Legacy only | Default | Performance |
Test Vector
The empty-string digest is from the RFC 1321 test suite.
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
#include <cryptopp/md5.h>
// MD5("") = d41d8cd98f00b204e9800998ecf8427e
Weak::MD5 hash;
byte digest[Weak::MD5::DIGESTSIZE];
hash.Final(digest);
byte expected[] = {
0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04,
0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e
};
assert(std::memcmp(digest, expected, 16) == 0);When to Use MD5
Only use MD5 for:
- Legacy Compatibility - Interfacing with systems that require MD5
- Error-Detecting Checksums - Where MD5 only protects against errors
- Academic/Historical - Studying hash function cryptanalysis
Never use MD5 for:
- Anything that needs collision resistance
- Password Hashing - We recommend Argon2id
- Digital Signatures - Use SHA-256 or stronger
- File Integrity - Use SHA-256 or BLAKE3
- New Code - Use a modern alternative
Exceptions
TruncatedFinal(), TruncatedVerify() and VerifyTruncatedDigest() throw InvalidArgument when the requested size is larger than 16 bytes.
See Also
- SHA-256 - Recommended replacement
- BLAKE3 - Fast modern hash
- SHA-1 - Also broken (but less so)
- Argon2 - For password hashing
References
- CERT/CC Vulnerability Note VU#836068: MD5 vulnerable to collision attacks
- RFC 6151: Updated Security Considerations for the MD5 Message-Digest and the HMAC-MD5 Algorithms
- RFC 1321: The MD5 Message-Digest Algorithm
- Sotirov, Stevens, Appelbaum, Lenstra, Molnar, Osvik and de Weger: MD5 considered harmful today
- Stevens: Counter-cryptanalysis (IACR ePrint 2013/358)