{"id":504,"date":"2026-11-13T13:39:54","date_gmt":"2026-11-13T05:39:54","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-sha-1-compresses-a-padded-message\/"},"modified":"2026-11-13T21:59:18","modified_gmt":"2026-11-13T13:59:18","slug":"how-sha-1-compresses-a-padded-message","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-sha-1-compresses-a-padded-message\/","title":{"rendered":"How SHA-1 compresses a padded message"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>SHA-1<\/strong> maps a message to a 20-byte digest. It pads like SHA-256, with a 1 bit, zeros, and the length, then runs eighty rounds on five 32-bit words. The digest of <code>abc<\/code> is <code>a9993e364706816aba3e25717850c26c9cd0d89d<\/code>. Distinct messages can share a SHA-1 digest. A collision is practical, so the function is not a collision-resistant hash.<\/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\">SHA-1 answers the same padding-and-compression question as SHA-256, with a shorter state and a different round function. The eighty rounds use four Boolean functions and four constants. The message schedule rotates a xor of four earlier words. The output is 160 bits. Collision resistance is the property a signature needs from a hash, and SHA-1 does not provide it. The fragment computes the digest. It does not construct 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 padding includes the length, and the compression is completely determined by the message. Two inputs that collide produce one digest, and such inputs exist and can be built. A checksum that only has to notice an accidental flipped bit still changes when the bit flips. That is a weaker job than collision resistance. A signature, a certificate, and a password store need the stronger job, which SHA-1 does not do.<\/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>Start from the five fixed initial words.<\/li>\n<li>Pad with <code>0x80<\/code>, zeros, and the 64-bit length.<\/li>\n<li>Expand each block to eighty words and run the four groups of twenty rounds.<\/li>\n<li>Add the working words back onto the five-word state.<\/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 SHA-1 inside a new signature<\/td>\n<td>A collision becomes two messages with one signature. A new signature hashes with a collision-resistant function.<\/td>\n<\/tr>\n<tr>\n<td>Shorten SHA-256 and call the prefix SHA-1<\/td>\n<td>SHA-1 is its own eighty-round function. It is not a prefix of SHA-256.<\/td>\n<\/tr>\n<tr>\n<td>Keep SHA-1 because a random edit still changes the digest<\/td>\n<td>Accidental damage and a chosen collision are different events. The chosen collision is the one that is practical.<\/td>\n<\/tr>\n<tr>\n<td>Store passwords as SHA-1 digests<\/td>\n<td>The function is fast, and it is not collision-resistant. A password store uses a salt and a cost.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">SHA-256 is the 256-bit hash to use in this family. SHA-1 is the retired 160-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 SHA-1 compression on the bytes <code>abc<\/code>. It prints <code>a9993e364706816aba3e25717850c26c9cd0d89d<\/code>.<\/p>\n\n<pre><code class=\"language-python\">import struct\n\ndef _rotr(x: int, n: int) -&gt; int:\n    return ((x &gt;&gt; n) | (x &lt;&lt; (32 - n))) &amp; 0xFFFFFFFF\n\ndef _rotl(x: int, n: int) -&gt; int:\n    return ((x &lt;&lt; n) | (x &gt;&gt; (32 - n))) &amp; 0xFFFFFFFF\n\ndef sha1(data: bytes) -&gt; bytes:\n    h0, h1, h2, h3, h4 = 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0\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;&gt;Q&quot;, bit_len)\n    for off in range(0, len(msg), 64):\n        w = list(struct.unpack(&quot;&gt;16I&quot;, msg[off : off + 64]))\n        for i in range(16, 80):\n            w.append(_rotr(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 31))\n        a, b, c, d, e = h0, h1, h2, h3, h4\n        for i in range(80):\n            if i &lt; 20:\n                f, k = (b &amp; c) | (~b &amp; d), 0x5A827999\n            elif i &lt; 40:\n                f, k = b ^ c ^ d, 0x6ED9EBA1\n            elif i &lt; 60:\n                f, k = (b &amp; c) | (b &amp; d) | (c &amp; d), 0x8F1BBCDC\n            else:\n                f, k = b ^ c ^ d, 0xCA62C1D6\n            a, b, c, d, e = (_rotl(a, 5) + f + e + k + w[i]) &amp; 0xFFFFFFFF, a, _rotl(b, 30), c, d\n        h0 = (h0 + a) &amp; 0xFFFFFFFF\n        h1 = (h1 + b) &amp; 0xFFFFFFFF\n        h2 = (h2 + c) &amp; 0xFFFFFFFF\n        h3 = (h3 + d) &amp; 0xFFFFFFFF\n        h4 = (h4 + e) &amp; 0xFFFFFFFF\n    return struct.pack(&quot;&gt;5I&quot;, h0, h1, h2, h3, h4)\n\ndigest = sha1(b&quot;abc&quot;)\nassert digest.hex() == &quot;a9993e364706816aba3e25717850c26c9cd0d89d&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\"><strong>Git<\/strong> still names most objects by SHA-1, and the project is moving those ids to SHA-256. Old TLS certificates used SHA-1 as well. A collision is practical, so a new signature does not use this digest.<\/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 SHA-1 wherever a collision would matter, including signatures, certificates, and password storage. The compression function remains a historical example.<\/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 SHA-1 returns the eighty-round compression and the reason a collision matters. <strong>SHA-1 compresses a padded message to 20 bytes in eighty 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: SHA-1 maps a message to a 20-byte digest. It pads like SHA-256, with a 1 bit, zeros, and the length, then runs eighty rounds on five 32-bit words. The digest of abc is a9993e364706816aba3e25717850c26c9cd0d89d. Distinct messages can share a SHA-1 digest. A collision is practical, so the&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-504","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\/504","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=504"}],"version-history":[{"count":1,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/504\/revisions"}],"predecessor-version":[{"id":716,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/504\/revisions\/716"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=504"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=504"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=504"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}