{"id":500,"date":"2026-11-13T13:39:46","date_gmt":"2026-11-13T05:39:46","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-pbkdf2-stretches-a-password-by-repeating-hmac\/"},"modified":"2026-11-13T21:59:12","modified_gmt":"2026-11-13T13:59:12","slug":"how-pbkdf2-stretches-a-password-by-repeating-hmac","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-pbkdf2-stretches-a-password-by-repeating-hmac\/","title":{"rendered":"How PBKDF2 stretches a password by repeating HMAC"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>PBKDF2<\/strong> stretches a password by repeating HMAC. For one block, the first value is HMAC of the password and the salt followed by a block counter. Each later value is HMAC of the password and the previous value. The block is the xor of those values. The fragment uses one iteration, so the block is that first HMAC. A password store uses a large iteration count.<\/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 password is a low-entropy secret. A fast hash lets a guesser try many passwords. PBKDF2 makes each guess run the HMAC many times. The salt makes two equal passwords store different strings, and it makes a precomputed table for one salt useless for another. The iteration count is the cost. The function is not memory-hard: a guesser can evaluate the chain with a small amount of memory and can try many passwords in parallel. The fragment uses one iteration so the output is the standard one-iteration block, not a suggested store cost.<\/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 password, salt, count, and length reproduce the same bytes. A different salt reproduces different bytes. With one iteration, the first 32 bytes are HMAC-SHA-256 of <code>salt || 00000001<\/code>. Raising the count multiplies the work for the honest verifier and for the guesser by about the same factor. It does not add a large memory requirement.<\/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>Set the block counter to 1 as a four-byte big-endian integer.<\/li>\n<li>Compute HMAC of the password and the salt followed by that counter.<\/li>\n<li>Repeat HMAC, using the previous HMAC output as the next message, and xor the outputs together.<\/li>\n<li>Stop after the chosen number of iterations. Ask for further blocks by incrementing the counter if more than 32 bytes are required.<\/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 memory-hard password hash<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Use one iteration in a password store<\/td>\n<td>One iteration is the test vector. A store chooses a count that makes a guess expensive.<\/td>\n<\/tr>\n<tr>\n<td>Omit the salt<\/td>\n<td>Equal passwords then store equal strings, and one precomputed table applies to every user.<\/td>\n<\/tr>\n<tr>\n<td>Expect the iteration count to demand a large memory<\/td>\n<td>The chain stores a hash-sized state. A memory-hard function such as scrypt fills a table.<\/td>\n<\/tr>\n<tr>\n<td>Encrypt the password with the result and throw the salt away<\/td>\n<td>Verification needs the salt and the count. They are stored beside the derived bytes.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">scrypt wraps this construction around a large table. PBKDF2 is the iteration.<\/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 PBKDF2-HMAC-SHA-256 for one iteration on the password <code>password<\/code> and the salt <code>salt<\/code>. It prints <code>120fb6cffcf8b32c43e7225256c4f837a86548c92ccc35480805987cb70be17b<\/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\ndef pbkdf2_sha256(password: bytes, salt: bytes, rounds: int, length: int) -&gt; bytes:\n    out = b&quot;&quot;\n    block_index = 1\n    while len(out) &lt; length:\n        u = hmac_sha256(password, salt + struct.pack(&quot;&gt;I&quot;, block_index))\n        acc = int.from_bytes(u, &quot;big&quot;)\n        for _ in range(rounds - 1):\n            u = hmac_sha256(password, u)\n            acc ^= int.from_bytes(u, &quot;big&quot;)\n        out += acc.to_bytes(32, &quot;big&quot;)\n        block_index += 1\n    return out[:length]\n\nderived = pbkdf2_sha256(b&quot;password&quot;, b&quot;salt&quot;, 1, 32)\nassert derived.hex() == &quot;120fb6cffcf8b32c43e7225256c4f837a86548c92ccc35480805987cb70be17b&quot;\nprint(derived.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>WPA2<\/strong> turns a passphrase into a key with PBKDF2-HMAC-SHA1, and password stores such as Django&#8217;s historical default use PBKDF2-HMAC-SHA-256. The iteration count is the cost. A memory-hard store uses scrypt or Argon2.<\/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 a one-iteration PBKDF2 as a password store. Keep PBKDF2 where the iteration count is an explicit cost and a memory-hard alternative is not required. Keep the one-iteration case as the test vector.<\/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 PBKDF2 returns the iterated HMAC and the one-iteration test block. <strong>PBKDF2 xors a chain of HMAC outputs whose first input is the salt and a block counter, and the iteration count is the cost.<\/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: PBKDF2 stretches a password by repeating HMAC. For one block, the first value is HMAC of the password and the salt followed by a block counter. Each later value is HMAC of the password and the previous value. The block is the xor of those values. 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-500","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\/500","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=500"}],"version-history":[{"count":1,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/500\/revisions"}],"predecessor-version":[{"id":712,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/500\/revisions\/712"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=500"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=500"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}