How HMAC-SHA256 Operates on Secrets
HMAC combines a cryptographic hash function (SHA-256) with a secret key. In the HMAC construction:
$$\text{HMAC}(K, m) = H((K' \oplus \text{opad}) \parallel H((K' \oplus \text{ipad}) \parallel m))$$
If the secret key $K$ is shorter than the block size of the hash function (64 bytes for SHA-256), it must be padded. If it is shorter than the digest length (32 bytes / 256 bits), it represents a weak link.
Verifying HS256 in Node.js (jsonwebtoken)
When verifying an HS256 token, always explicitly declare the allowed algorithms. Never permit the library to accept whatever algorithm is declared in the untrusted token header!
Frequently Asked Questions
What is the "alg": "none" vulnerability in HS256?
Earlier JWT libraries allowed tokens with the algorithm header set to "none" to bypass signature verification entirely. Always use modern, patched libraries and explicitly whitelist allowed algorithms.