JWT Algorithms·6 min read·2026-01-20T11:00:00Z

HS256 Secret Key Requirements: Specifications, Code Examples, and Common Traps

HS256 (HMAC with SHA-256) is by far the most widely adopted JWT signing algorithm across web applications. Yet a surprising number of implementations violate basic cryptographic prerequisites. Here is what you must know to implement HS256 securely.

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.

HS256 Secret Key Requirements – RFC 7518 Specs & Code Examples | Generador de Clave Secreta JWT | JWT Secret Key Generator