{"id":498,"date":"2026-11-13T13:39:42","date_gmt":"2026-11-13T05:39:42","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-md5-compresses-a-padded-message\/"},"modified":"2026-11-13T21:59:07","modified_gmt":"2026-11-13T13:59:07","slug":"how-md5-compresses-a-padded-message","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-md5-compresses-a-padded-message\/","title":{"rendered":"How MD5 compresses a padded message"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>MD5<\/strong> 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 <code>2^32<\/code> times the absolute value of the sine of 1 through 64. The digest of <code>abc<\/code> is <code>900150983cd24fb0d6963f7d28e17f72<\/code>. Distinct messages can share an MD5 digest. A collision is practical.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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 <code>5i + 1<\/code>, then <code>3i + 5<\/code>, then <code>7i<\/code>, all modulo 16. The 128-bit output is the wrong width, and the wrong collision resistance, for a new signature or a password store.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Build the sixty-four constants from the sine formula and start from the four fixed words.<\/li>\n<li>Pad with <code>0x80<\/code>, zeros, and the little-endian bit length.<\/li>\n<li>Run the four groups of sixteen rounds, rotating the working words.<\/li>\n<li>Add the working words back onto the state and write them little-endian.<\/li>\n<\/ol>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when a collision must be infeasible<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Use MD5 in a new signature or certificate<\/td>\n<td>A collision is two messages with one signature. A new signature uses a collision-resistant hash.<\/td>\n<\/tr>\n<tr>\n<td>Treat a matching MD5 as proof that a file was not chosen by an adversary<\/td>\n<td>An accidental mismatch is still a mismatch. A chosen collision is the case MD5 does not prevent.<\/td>\n<\/tr>\n<tr>\n<td>Store MD5 of a password<\/td>\n<td>The function is fast and unsalted here, and it is not collision-resistant.<\/td>\n<\/tr>\n<tr>\n<td>Read the words big-endian because SHA-256 does<\/td>\n<td>MD5 packs and unpacks little-endian words. The other order is a different function.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">SHA-256 is the hash to use for a new digest. MD5 is the retired 128-bit compression.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment runs the MD5 rounds on the bytes <code>abc<\/code>. It prints <code>900150983cd24fb0d6963f7d28e17f72<\/code>.<\/p>\n\n<pre><code class=\"language-python\">import math\n\nimport struct\n\ndef _rotl(x: int, n: int) -&gt; int:\n    return ((x &lt;&lt; n) | (x &gt;&gt; (32 - n))) &amp; 0xFFFFFFFF\n\ndef md5(data: bytes) -&gt; bytes:\n    t = [int(abs(math.sin(i + 1)) * 2**32) &amp; 0xFFFFFFFF for i in range(64)]\n    s = [\n        7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,\n        5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,\n        4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,\n        6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21,\n    ]\n    a0, b0, c0, d0 = 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476\n    bit_len = len(data) * 8\n    msg = data + b&quot;\\x80&quot;\n    msg += b&quot;\\x00&quot; * ((56 - len(msg) % 64) % 64)\n    msg += struct.pack(&quot;&lt;Q&quot;, bit_len)\n    for off in range(0, len(msg), 64):\n        m = list(struct.unpack(&quot;&lt;16I&quot;, msg[off : off + 64]))\n        a, b, c, d = a0, b0, c0, d0\n        for i in range(64):\n            if i &lt; 16:\n                f, g = (b &amp; c) | (~b &amp; d), i\n            elif i &lt; 32:\n                f, g = (d &amp; b) | (~d &amp; c), (5 * i + 1) % 16\n            elif i &lt; 48:\n                f, g = b ^ c ^ d, (3 * i + 5) % 16\n            else:\n                f, g = c ^ (b | ~d), (7 * i) % 16\n            f = (f + a + t[i] + m[g]) &amp; 0xFFFFFFFF\n            a, d, c, b = d, c, b, (b + _rotl(f, s[i])) &amp; 0xFFFFFFFF\n        a0 = (a0 + a) &amp; 0xFFFFFFFF\n        b0 = (b0 + b) &amp; 0xFFFFFFFF\n        c0 = (c0 + c) &amp; 0xFFFFFFFF\n        d0 = (d0 + d) &amp; 0xFFFFFFFF\n    return struct.pack(&quot;&lt;4I&quot;, a0, b0, c0, d0)\n\ndigest = md5(b&quot;abc&quot;)\nassert digest.hex() == &quot;900150983cd24fb0d6963f7d28e17f72&quot;\nprint(digest.hex())\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for MD5 returns the little-endian rounds and the reason a collision matters. <strong>MD5 compresses a padded message to 16 bytes in sixty-four rounds, and a collision on that digest is practical.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,49],"tags":[],"class_list":["post-498","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-cryptography"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/498","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=498"}],"version-history":[{"count":1,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/498\/revisions"}],"predecessor-version":[{"id":710,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/498\/revisions\/710"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=498"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}