{"id":497,"date":"2026-11-13T13:39:40","date_gmt":"2026-11-13T05:39:40","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-hmac-sha-256-mixes-a-key-into-a-hash\/"},"modified":"2026-11-13T21:59:05","modified_gmt":"2026-11-13T13:59:05","slug":"how-hmac-sha-256-mixes-a-key-into-a-hash","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-hmac-sha-256-mixes-a-key-into-a-hash\/","title":{"rendered":"How HMAC-SHA-256 mixes a key into a hash"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>HMAC-SHA-256<\/strong> turns a key and a message into a 32-byte tag. A key longer than 64 bytes is hashed first. The key is padded with zeros to 64 bytes. The inner hash is SHA-256 of the key xored with <code>0x36<\/code>, followed by the message. The outer hash is SHA-256 of the key xored with <code>0x5c<\/code>, followed by the inner digest. The RFC 4231 key of twenty <code>0x0b<\/code> bytes and the message <code>Hi There<\/code> produce <code>b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7<\/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 tag has to depend on a secret so that someone who can compute the hash still cannot forge the tag. Concatenating the key and the message and hashing once is not that construction. HMAC hashes twice, with two different pads, so the key is mixed on both sides of the inner digest. Verification recomputes the tag and compares the whole value. The tag does not hide the message.<\/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 same key and message reproduce the same tag. A key longer than the SHA-256 block is reduced with SHA-256 before the pads are applied. The construction is a message authentication code, not an encryption and not a password hash. The cost is two SHA-256 compressions plus the cost of hashing the message.<\/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>If the key is longer than 64 bytes, replace it with its SHA-256 digest.<\/li>\n<li>Pad the key with zeros to 64 bytes.<\/li>\n<li>Compute the inner digest of <code>(key xor 0x36) || message<\/code>.<\/li>\n<li>Compute the outer digest of <code>(key xor 0x5c) || inner digest<\/code>.<\/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 the key is only concatenated<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Hash the key followed by the message<\/td>\n<td>A bare concatenation is not the two-pad construction. HMAC is the inner hash and the outer hash.<\/td>\n<\/tr>\n<tr>\n<td>Compare the tag by stopping at the first different byte and returning that position<\/td>\n<td>The check is the whole tag. A comparison that leaks the mismatch position is a weaker check.<\/td>\n<\/tr>\n<tr>\n<td>Use the tag as a cipher<\/td>\n<td>HMAC does not decrypt. It accepts or rejects a message.<\/td>\n<\/tr>\n<tr>\n<td>Omit the outer pad and publish the inner hash<\/td>\n<td>Both pads are part of the definition. One hash of a padded key is a different function.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">Poly1305 is a one-time tag. HMAC-SHA-256 is the reusable tag built from SHA-256.<\/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 computes the RFC 4231 HMAC-SHA-256 test case. It prints <code>b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7<\/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\ndef hmac_sha256(key: bytes, msg: bytes) -&gt; bytes:\n    if len(key) &gt; 64:\n        key = sha256(key)\n    key = key.ljust(64, b&quot;\\x00&quot;)\n    outer = bytes(b ^ 0x5C for b in key)\n    inner = bytes(b ^ 0x36 for b in key)\n    return sha256(outer + sha256(inner + msg))\n\ntag = hmac_sha256(bytes([0x0B]) * 20, b&quot;Hi There&quot;)\nassert tag.hex() == &quot;b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7&quot;\nprint(tag.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>AWS Signature Version 4<\/strong> and JWT <code>HS256<\/code> authenticate with HMAC-SHA-256. Older TLS cipher suites used HMAC as the record MAC before AEAD suites replaced them. The construction is a tag, not an encryption.<\/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 HMAC when the job is encryption or password storage. Keep it when a shared key has to authenticate a message and the same key is used for more than one message.<\/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 HMAC returns the inner pad <code>0x36<\/code>, the outer pad <code>0x5c<\/code>, and the RFC 4231 tag. <strong>HMAC-SHA-256 hashes the message under the inner pad and hashes that digest under the outer pad.<\/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: HMAC-SHA-256 turns a key and a message into a 32-byte tag. A key longer than 64 bytes is hashed first. The key is padded with zeros to 64 bytes. The inner hash is SHA-256 of the key xored with 0x36, followed by the message. The outer hash&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-497","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\/497","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=497"}],"version-history":[{"count":1,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/497\/revisions"}],"predecessor-version":[{"id":709,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/497\/revisions\/709"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}