JWT Algorithms·6 min read·Updated October 23, 2026

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!
javascriptSecure HS256 verification pattern with algorithm whitelisting
const jwt = require('jsonwebtoken');

// Always enforce algorithms whitelist:
jwt.verify(token, process.env.JWT_SECRET, {
  algorithms: ['HS256'], // Enforces HS256; rejects "none" or "RS256"
  issuer: 'https://api.jwtsecretkey.com',
  audience: 'https://jwtsecretkey.com',
});
Generate in Browser

Need a cryptographically secure JWT secret right now?

Generate 128 to 512-bit keys in Hex or Base64 using Web Crypto with zero server transmission.

Launch Generator

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 | JWT Secret Key Generator | JWT Secret Key Generator