HS256
acceptableHMACHMAC using SHA-256
RFC 7518 §3.2
HS256 uses a shared secret to both sign and verify JWTs. It is the simplest JWT algorithm – fast, no PKI required, small signatures. However, the secret must be shared with every service that verifies tokens. A compromised verifier can forge tokens. Use HS256 only when all signers and verifiers are fully trusted components of the same system.
Details
HS256 (HMAC-SHA256) computes HMAC(SHA-256, secret, header + '.' + payload). The same key signs and verifies.
Key size: the secret MUST be at least 256 bits (32 bytes) per RFC 7518 §3.2. Shorter secrets are cryptographically weak. Use a cryptographically random generator, not a human-chosen password.
When HS256 is appropriate: - Single-service JWT issuance and verification (same codebase or single service) - Backend-for-frontend patterns where one service controls both ends - Simple session tokens in a monolith
When HS256 is NOT appropriate: - Microservices where multiple services verify tokens – each would need the secret - Any scenario where a verifier could be compromised (a verifier can forge tokens) - Third-party token verification (the third party gets a forgeable secret)
RFC 8725 §3.1 warns that symmetric algorithms like HS256 create confusion attacks where a public-key verifier is switched to HMAC mode. Always pin the expected algorithm in your verification code.
Key Information
| Key type | Shared secret (symmetric) |
| Key size | 256 bits minimum (32 bytes) – same as the hash output length |
| Family | HMAC |
| Spec | RFC 7518 §3.2 |
Advantages
- +Fast – HMAC is much faster than RSA operations
- +Simple – no PKI, no key pairs, no JWKS endpoint needed
- +Small tokens – 32-byte HMAC signature vs 256-byte RSA signature
Limitations
- !Shared secret – every verifier knows the signing secret and can forge tokens
- !No key rotation without coordination – all verifiers must update simultaneously
- !Not suitable for multi-service or federated architectures
When to use
Single-service internal tokens
Session tokens in a monolith
Simple backend-to-frontend tokens