Short answer: MD5 maps a message to a 16-byte digest. The words are little-endian. Sixty-four rounds use four Boolean functions, a left rotation, and a constant. The constants are the integer parts of 2^32 times the absolute value of the sine of 1 through 64. The digest of abc is 900150983cd24fb0d6963f7d28e17f72. Distinct messages can share an MD5 digest. A collision is practical.
What the problem actually asks
MD5 is a Merkle-Damgard compression to 128 bits. The padding is a 1 bit, zeros, and the bit length, with the length stored little-endian. The narrow digest and the practical collisions are the reasons it is retired for signatures and certificates. A random flipped bit still changes the digest. A chosen second message with the same digest is a different event, and that event is practical. The fragment computes the digest. It does not build a colliding pair.
What the algorithm guarantees
The function is public and deterministic. The sine formula fixes the sixty-four constants. The four rounds index the message words differently: in order, then 5i + 1, then 3i + 5, then 7i, all modulo 16. The 128-bit output is the wrong width, and the wrong collision resistance, for a new signature or a password store.
The working steps
- Build the sixty-four constants from the sine formula and start from the four fixed words.
- Pad with
0x80, zeros, and the little-endian bit length. - Run the four groups of sixteen rounds, rotating the working words.
- Add the working words back onto the state and write them little-endian.
Approaches that look simpler and fail
| Approach | Why it fails when a collision must be infeasible |
|---|---|
| Use MD5 in a new signature or certificate | A collision is two messages with one signature. A new signature uses a collision-resistant hash. |
| Treat a matching MD5 as proof that a file was not chosen by an adversary | An accidental mismatch is still a mismatch. A chosen collision is the case MD5 does not prevent. |
| Store MD5 of a password | The function is fast and unsalted here, and it is not collision-resistant. |
| Read the words big-endian because SHA-256 does | MD5 packs and unpacks little-endian words. The other order is a different function. |
SHA-256 is the hash to use for a new digest. MD5 is the retired 128-bit compression.
A working example
This Python fragment runs the MD5 rounds on the bytes abc. It prints 900150983cd24fb0d6963f7d28e17f72.
import math
import struct
def _rotl(x: int, n: int) -> int:
return ((x << n) | (x >> (32 - n))) & 0xFFFFFFFF
def md5(data: bytes) -> bytes:
t = [int(abs(math.sin(i + 1)) * 2**32) & 0xFFFFFFFF for i in range(64)]
s = [
7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21,
]
a0, b0, c0, d0 = 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476
bit_len = len(data) * 8
msg = data + b"\x80"
msg += b"\x00" * ((56 - len(msg) % 64) % 64)
msg += struct.pack("<Q", bit_len)
for off in range(0, len(msg), 64):
m = list(struct.unpack("<16I", msg[off : off + 64]))
a, b, c, d = a0, b0, c0, d0
for i in range(64):
if i < 16:
f, g = (b & c) | (~b & d), i
elif i < 32:
f, g = (d & b) | (~d & c), (5 * i + 1) % 16
elif i < 48:
f, g = b ^ c ^ d, (3 * i + 5) % 16
else:
f, g = c ^ (b | ~d), (7 * i) % 16
f = (f + a + t[i] + m[g]) & 0xFFFFFFFF
a, d, c, b = d, c, b, (b + _rotl(f, s[i])) & 0xFFFFFFFF
a0 = (a0 + a) & 0xFFFFFFFF
b0 = (b0 + b) & 0xFFFFFFFF
c0 = (c0 + c) & 0xFFFFFFFF
d0 = (d0 + d) & 0xFFFFFFFF
return struct.pack("<4I", a0, b0, c0, d0)
digest = md5(b"abc")
assert digest.hex() == "900150983cd24fb0d6963f7d28e17f72"
print(digest.hex())
Where it is used
MD5 remains a checksum for accidental file damage and a relic inside old certificate signatures. It is not a signature hash and not a password hash. SHA-256 is the digest those jobs moved to.
When you can drop this algorithm
Drop MD5 wherever a collision would matter, including signatures, certificates, and password storage. The sine constants and the four Boolean rounds are the historical function.
Why this stays published
We publish this so a search for MD5 returns the little-endian rounds and the reason a collision matters. MD5 compresses a padded message to 16 bytes in sixty-four rounds, and a collision on that digest is practical.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
