{"id":505,"date":"2026-11-13T13:39:56","date_gmt":"2026-11-13T05:39:56","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-sha-256-compresses-a-padded-message\/"},"modified":"2026-11-13T21:59:20","modified_gmt":"2026-11-13T13:59:20","slug":"how-sha-256-compresses-a-padded-message","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-sha-256-compresses-a-padded-message\/","title":{"rendered":"How SHA-256 compresses a padded message"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>SHA-256<\/strong> maps a message to a 32-byte digest. It pads with a 1 bit, zeros, and the 64-bit length, then compresses each 64-byte block into eight 32-bit words. The initial words are the first 32 bits of the fractional square roots of the first eight primes. The sixty-four round constants are the first 32 bits of the fractional cube roots of the first sixty-four primes. The digest of <code>abc<\/code> is <code>ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad<\/code>.<\/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\">A cryptographic hash has to map an arbitrary byte string to a fixed digest so that a changed input changes the digest and a search for a second input with the same digest is infeasible. SHA-256 is the Merkle-Damgard construction with this compression function. The message is not a secret key. The same message always produces the same digest. That is the wrong shape for a password store and the wrong shape for a tag, which is why a password uses a stretching function and a tag uses HMAC.<\/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\">Every message pads to a whole number of 64-byte blocks, including the length, so two messages of different lengths do not collapse to the same padded string. Each round updates eight working words with rotations, the choice function, the majority function, one message word, and one round constant. The fragment generates the constants. The first initial word is <code>0x6a09e667<\/code> and the first round constant is <code>0x428a2f98<\/code>. The digest is 256 bits. The function is public.<\/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>Take the initial state from the fractional square roots of the first eight primes, and the round constants from the fractional cube roots of the first sixty-four primes.<\/li>\n<li>Append <code>0x80<\/code>, zeros, and the bit length as a 64-bit integer so the length is a multiple of 64 bytes.<\/li>\n<li>Expand each 64-byte block to sixty-four words and run sixty-four rounds.<\/li>\n<li>Add the working words back onto the state. The digest is the eight state words.<\/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 as a password store or a tag<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Store SHA-256 of a password<\/td>\n<td>The function is fast and unsalted in this form. A password store uses a salt and a cost, as in PBKDF2 or scrypt.<\/td>\n<\/tr>\n<tr>\n<td>Append a key and call the digest a tag<\/td>\n<td>A secret prefix or suffix is not HMAC. HMAC uses the inner and outer pads.<\/td>\n<\/tr>\n<tr>\n<td>Omit the length from the padding<\/td>\n<td>The length block is what separates messages that would otherwise share a padded prefix.<\/td>\n<\/tr>\n<tr>\n<td>Use the digest as an encryption<\/td>\n<td>A hash does not have a decryption. It is a one-way digest.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">HMAC-SHA-256 is the keyed tag. PBKDF2 and scrypt are the password stretches. SHA-256 is the digest.<\/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 derives the SHA-256 words from the primes and hashes the bytes <code>abc<\/code>. It prints <code>ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad<\/code>.<\/p>\n\n<pre><code class=\"language-python\">import struct\n\ndef _primes(k: int) -&gt; list[int]:\n    out: list[int] = []\n    n = 2\n    while len(out) &lt; k:\n        if all(n % p for p in out if p * p &lt;= n):\n            out.append(n)\n        n += 1\n    return out\n\ndef _isqrt(n: int) -&gt; int:\n    x = int(n ** 0.5)\n    while x * x &gt; n:\n        x -= 1\n    while (x + 1) * (x + 1) &lt;= n:\n        x += 1\n    return x\n\ndef _icbrt(n: int) -&gt; int:\n    x = int(round(n ** (1 \/ 3)))\n    while x * x * x &gt; n:\n        x -= 1\n    while (x + 1) ** 3 &lt;= n:\n        x += 1\n    return x\n\n_PS = _primes(64)\n_H256 = [_isqrt(p &lt;&lt; 64) &amp; 0xFFFFFFFF for p in _PS[:8]]\n_K256 = [_icbrt(p &lt;&lt; 96) &amp; 0xFFFFFFFF for p in _PS]\n\ndef _rotr(x: int, n: int) -&gt; int:\n    return ((x &gt;&gt; n) | (x &lt;&lt; (32 - n))) &amp; 0xFFFFFFFF\n\ndef sha256(data: bytes) -&gt; bytes:\n    h = _H256[:]\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, 64):\n            s0 = _rotr(w[i - 15], 7) ^ _rotr(w[i - 15], 18) ^ (w[i - 15] &gt;&gt; 3)\n            s1 = _rotr(w[i - 2], 17) ^ _rotr(w[i - 2], 19) ^ (w[i - 2] &gt;&gt; 10)\n            w.append((w[i - 16] + s0 + w[i - 7] + s1) &amp; 0xFFFFFFFF)\n        a, b, c, d, e, f, g, hh = h\n        for i in range(64):\n            s1 = _rotr(e, 6) ^ _rotr(e, 11) ^ _rotr(e, 25)\n            ch = (e &amp; f) ^ (~e &amp; g)\n            t1 = (hh + s1 + ch + _K256[i] + w[i]) &amp; 0xFFFFFFFF\n            s0 = _rotr(a, 2) ^ _rotr(a, 13) ^ _rotr(a, 22)\n            maj = (a &amp; b) ^ (a &amp; c) ^ (b &amp; c)\n            t2 = (s0 + maj) &amp; 0xFFFFFFFF\n            hh, g, f, e, d, c, b, a = g, f, e, (d + t1) &amp; 0xFFFFFFFF, c, b, a, (t1 + t2) &amp; 0xFFFFFFFF\n        h = [(x + y) &amp; 0xFFFFFFFF for x, y in zip(h, (a, b, c, d, e, f, g, hh))]\n    return struct.pack(&quot;&gt;8I&quot;, *h)\n\nassert _H256[0] == 0x6A09E667 and _K256[0] == 0x428A2F98\ndigest = sha256(b&quot;abc&quot;)\nassert digest.hex() == &quot;ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad&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>Bitcoin<\/strong> hashes block headers with SHA-256, TLS and certificate fingerprints use it, and Git&#8217;s new object ids are SHA-256. HMAC-SHA-256 is the keyed form. A password store still wants a salt and a cost on top of the 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 raw SHA-256 as a password hash and as a message tag. Keep it as the 256-bit digest inside HMAC, HKDF, and a signature that hashes before signing.<\/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-256 returns the prime-derived words and the digest of <code>abc<\/code>. <strong>SHA-256 pads a message with its length and compresses each 64-byte block with sixty-four rounds into a 32-byte digest.<\/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-256 maps a message to a 32-byte digest. It pads with a 1 bit, zeros, and the 64-bit length, then compresses each 64-byte block into eight 32-bit words. The initial words are the first 32 bits of the fractional square roots of the first eight primes. 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-505","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\/505","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=505"}],"version-history":[{"count":1,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/505\/revisions"}],"predecessor-version":[{"id":717,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/505\/revisions\/717"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=505"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=505"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=505"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}