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 is SHA-256 of the key xored with 0x5c, followed by the inner digest. The RFC 4231 key of twenty 0x0b bytes and the message Hi There produce b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7.
What the problem actually asks
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.
What the algorithm guarantees
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.
The working steps
- If the key is longer than 64 bytes, replace it with its SHA-256 digest.
- Pad the key with zeros to 64 bytes.
- Compute the inner digest of
(key xor 0x36) || message. - Compute the outer digest of
(key xor 0x5c) || inner digest.
Approaches that look simpler and fail
| Approach | Why it fails when the key is only concatenated |
|---|---|
| Hash the key followed by the message | A bare concatenation is not the two-pad construction. HMAC is the inner hash and the outer hash. |
| Compare the tag by stopping at the first different byte and returning that position | The check is the whole tag. A comparison that leaks the mismatch position is a weaker check. |
| Use the tag as a cipher | HMAC does not decrypt. It accepts or rejects a message. |
| Omit the outer pad and publish the inner hash | Both pads are part of the definition. One hash of a padded key is a different function. |
Poly1305 is a one-time tag. HMAC-SHA-256 is the reusable tag built from SHA-256.
A working example
This Python fragment computes the RFC 4231 HMAC-SHA-256 test case. It prints b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7.
import struct
def _primes(k: int) -> list[int]:
out: list[int] = []
n = 2
while len(out) < k:
if all(n % p for p in out if p * p <= n):
out.append(n)
n += 1
return out
def _isqrt(n: int) -> int:
x = int(n ** 0.5)
while x * x > n:
x -= 1
while (x + 1) * (x + 1) <= n:
x += 1
return x
def _icbrt(n: int) -> int:
x = int(round(n ** (1 / 3)))
while x * x * x > n:
x -= 1
while (x + 1) ** 3 <= n:
x += 1
return x
_PS = _primes(64)
_H256 = [_isqrt(p << 64) & 0xFFFFFFFF for p in _PS[:8]]
_K256 = [_icbrt(p << 96) & 0xFFFFFFFF for p in _PS]
def _rotr(x: int, n: int) -> int:
return ((x >> n) | (x << (32 - n))) & 0xFFFFFFFF
def sha256(data: bytes) -> bytes:
h = _H256[:]
bit_len = len(data) * 8
msg = data + b"\x80"
msg += b"\x00" * ((56 - len(msg) % 64) % 64)
msg += struct.pack(">Q", bit_len)
for off in range(0, len(msg), 64):
w = list(struct.unpack(">16I", msg[off : off + 64]))
for i in range(16, 64):
s0 = _rotr(w[i - 15], 7) ^ _rotr(w[i - 15], 18) ^ (w[i - 15] >> 3)
s1 = _rotr(w[i - 2], 17) ^ _rotr(w[i - 2], 19) ^ (w[i - 2] >> 10)
w.append((w[i - 16] + s0 + w[i - 7] + s1) & 0xFFFFFFFF)
a, b, c, d, e, f, g, hh = h
for i in range(64):
s1 = _rotr(e, 6) ^ _rotr(e, 11) ^ _rotr(e, 25)
ch = (e & f) ^ (~e & g)
t1 = (hh + s1 + ch + _K256[i] + w[i]) & 0xFFFFFFFF
s0 = _rotr(a, 2) ^ _rotr(a, 13) ^ _rotr(a, 22)
maj = (a & b) ^ (a & c) ^ (b & c)
t2 = (s0 + maj) & 0xFFFFFFFF
hh, g, f, e, d, c, b, a = g, f, e, (d + t1) & 0xFFFFFFFF, c, b, a, (t1 + t2) & 0xFFFFFFFF
h = [(x + y) & 0xFFFFFFFF for x, y in zip(h, (a, b, c, d, e, f, g, hh))]
return struct.pack(">8I", *h)
def hmac_sha256(key: bytes, msg: bytes) -> bytes:
if len(key) > 64:
key = sha256(key)
key = key.ljust(64, b"\x00")
outer = bytes(b ^ 0x5C for b in key)
inner = bytes(b ^ 0x36 for b in key)
return sha256(outer + sha256(inner + msg))
tag = hmac_sha256(bytes([0x0B]) * 20, b"Hi There")
assert tag.hex() == "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7"
print(tag.hex())
Where it is used
AWS Signature Version 4 and JWT HS256 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.
When you can drop this algorithm
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.
Why this stays published
We publish this so a search for HMAC returns the inner pad 0x36, the outer pad 0x5c, and the RFC 4231 tag. HMAC-SHA-256 hashes the message under the inner pad and hashes that digest under the outer pad.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
