HS256 and RS256 both produce a valid JWT signature, but they put the power to mint tokens in very different places: with HS256 every party that can verify a token can also forge one, while with RS256 only the holder of the private key can sign. That single difference drives key distribution, rotation, blast radius after a leak and the algorithm confusion attacks that still appear in token libraries. This guide compares the two, adds ES256, PS256 and EdDSA, and covers OpenID Connect, service-to-service calls and non-human identity issuers, for engineers and identity architects choosing a signing design. For the full validation checklist, read the JWT Security Guide first; this page goes deeper on the algorithm decision only.
Key takeaways
- The real choice is trust topology: HS256 is a shared secret, so it fits only when issuer and verifier are the same service or a tightly held trust domain; RS256 and other public-key algorithms fit whenever tokens cross a team, service or organisation boundary.
- HS256 is secure when the key is: a random key of at least 256 bits, held by few parties, is sound; a human-chosen password as the HMAC key can be brute forced offline from any captured token (RFC 8725 s2.2 and s3.5).
- Algorithm confusion is a verifier bug, not an algorithm flaw: forging an RS256 token as HS256 with the public key as the HMAC secret works only when the verifier lets the token header pick the routine; binding each key to exactly one algorithm closes it.
- Public keys make rotation boring: RS256, ES256 and EdDSA publish verification keys in a JWKS with a
kid, so rotation needs no secret redistribution; HS256 rotation means a coordinated secret change at every verifier. - RS256 is the interoperable default, not the best curve: RS256 is mandatory to support for RFC 9068 access tokens and the default for OpenID Connect ID tokens, while ES256 and EdDSA give smaller, faster signatures where every verifier supports them.
The short answer
Use an asymmetric algorithm (RS256, PS256, ES256 or EdDSA) for any token that one party issues and another party verifies. Use HS256 only where the same component, or a small set of components you operate as one, both signs and verifies, and the key is a random value stored in a secrets manager or KMS. If you are unsure, choose asymmetric: RFC 9068 says the use of asymmetric cryptography is recommended for JWT access tokens because it makes it simpler for resource servers to obtain validation keys.
Whichever you choose, the verifier must fix the expected algorithm per issuer in configuration. RFC 8725 s3.1 states that each key must be used with exactly one algorithm, and that this must be checked when the key is used. Most real HS256 and RS256 incidents come from ignoring that rule, not from the mathematics of either algorithm.
How each algorithm works
HS256: HMAC with SHA-256
The issuer computes an HMAC over the base64url header and payload using a secret key, and the verifier recomputes the HMAC with the same key and compares. RFC 7518 s3.2 requires a key of at least the hash output size (256 bits for HS256) and a constant-time comparison to stop timing attacks. There is no public half: the verification key is the signing key. RFC 7518 s3.1 lists HS256 as Required, which is why it appears in so many quick-start examples.
RS256: RSASSA-PKCS1-v1_5 with SHA-256
The issuer signs with an RSA private key and publishes the public key. Verifiers check the signature with the public key, which cannot be used to sign. RFC 7518 s3.3 requires RSA keys of 2048 bits or larger, and RS256 is listed as Recommended. RFC 7518 s8.3 notes that RFC 3447 advises against PKCS#1 v1.5 signatures for new applications in favour of RSASSA-PSS, and that keys with a low public exponent must not be used.
The asymmetric alternatives
- PS256: RSASSA-PSS with SHA-256, same key sizes as RS256 (2048 bits or larger, RFC 7518 s3.5), Optional in the JWA table.
- ES256: ECDSA on P-256 with SHA-256, Recommended+ in RFC 7518 s3.1. Signatures are a fixed 64-octet R and S sequence (s3.4), and verifiers must reject other lengths. ECDSA needs a strong per-signature random value, so sign in a maintained library or KMS.
- EdDSA: RFC 8037 adds key type
OKPand theEdDSAalgorithm with Ed25519 and Ed448. Signing is deterministic and needs no random numbers, which removes the ECDSA nonce failure class. Confirm every verifier library, gateway and cloud service in the path supports it before adopting.
Side-by-side comparison
| Property | HS256 | RS256 | ES256 / EdDSA |
|---|---|---|---|
| Key type | Shared symmetric secret (JWK kty oct) | RSA key pair (kty RSA), 2048 bits or more | EC P-256 (kty EC) or OKP Ed25519 |
| Who can mint tokens | Every holder of the key, including every verifier | Only the private key holder | Only the private key holder |
| Key distribution | Secret must be copied to each verifier over a protected channel | Public key published in a JWKS at jwks_uri | Public key published in a JWKS |
| Rotation | Coordinated secret change at every verifier | Add new kid, switch signing, retire old key after overlap | Same as RS256 |
| Signature size | 32 bytes | 256 bytes at 2048 bits | 64 bytes |
| JWA requirement (RFC 7518) | Required | Recommended | ES256 Recommended+; EdDSA defined in RFC 8037 |
| Main attack | Offline brute force of weak keys; any verifier compromise is an issuer compromise | Algorithm confusion when the verifier trusts alg; private key theft | Same as RS256, plus ECDSA nonce misuse for ES256 |
| Best fit | Single service sessions, internal state tokens, one-team systems | Federation, OIDC, multi-service APIs, maximum library support | New designs wanting compact tokens and fast signing |
Threat model: HS256
With HS256 the attacker's goal is the secret, because the secret is both the verifier and the mint. Three paths matter.
- Offline guessing: anyone who captures one token (from a log, a browser, a proxy capture or a public bug report) can test candidate keys against it without touching your systems. RFC 8725 s2.2 calls out applications that use human-memorable passwords as HS256 keys, and s3.5 says such passwords must not be used directly as HMAC keys. A random 256-bit key makes this path infeasible; a phrase from a tutorial or a framework default does not.
- Verifier compromise: every service that verifies the token holds the key. A leaked configuration file or container image gives the attacker a full issuer. The blast radius is every service that trusts that key.
- Secret reuse: the same value used as the HMAC key in several environments, or reused as an OAuth client secret, spreads one leak across systems. OpenID Connect Core 3.1.3.7 validates HS256 ID tokens with the UTF-8 octets of the
client_secret, so a weak client secret is a weak signing key, which is why s16.19 requires client secrets to contain enough entropy to generate strong keys.
The ATT&CK technique for the outcome is T1606 Forge Web Credentials: once the key is known, the attacker can produce tokens with any subject, role or audience, then use them as T1550.001 Application Access Token. See the JWT and Session Tokens in Code and Logs Guide.
Threat model: RS256 and other asymmetric algorithms
Public-key algorithms move the crown jewel to one place, the private key at the issuer, and make the verification material safe to publish. The remaining risks are different in kind.
- Private key theft: a signing key in a file on the issuer host, in a repository or in a backup lets an attacker mint tokens every verifier accepts. RFC 9068 s5 warns that using separate keys for ID tokens and access tokens is not a safeguard, because resource servers accept any key the authorisation server publishes. Keep signing keys in a KMS or HSM so they cannot be exported. See the Signing keys and certificates in breaches page for how stolen keys have been used.
- Untrusted key sources: header parameters such as
jku,x5uandjwklet a token point at its own verification key. RFC 8725 s3.10 says these must not be trusted blindly, thatjkuandx5uURLs should be allowlisted to prevent server-side request forgery, and thatkidmust be validated or sanitised to prevent injection. OpenID Connect Core section 2 says ID tokens should not usex5u,x5c,jkuorjwkat all. - Stale JWKS caches: verifiers that cache a key set forever keep accepting a retired or compromised key. OpenID Connect Core 10.1.1 tells verifiers to re-fetch the
jwks_uriwhen they see an unfamiliarkid, with a rate limit on refetches.
Algorithm confusion: how it works and how to stop it
RFC 8725 s2.1 describes two classic substitutions. In the first, the attacker sets alg to none and strips the signature. In the second, the attacker takes a token meant for RS256, changes alg to HS256, and signs it with HMAC using the issuer's RSA public key as the secret. A verifier that reads alg from the header and passes "the key" to whichever routine the header names will run HMAC with the public key bytes and accept the forgery.
Both share one root cause: untrusted input chooses how trust is checked. They are reasons to configure the verifier properly, not to avoid RS256. RFC 9068 s2.1 forbids none for access tokens outright.
| Confusion variant | Root cause in the verifier | Control | Reference |
|---|---|---|---|
alg set to none | Unsecured tokens accepted | Allowlist per issuer that excludes none | RFC 7518 s3.6; ASVS 5.0 V9.1.2 |
| RS256 changed to HS256 | One key object passed to any algorithm; public key used as HMAC secret | Bind each key to exactly one algorithm and key type; never mix MAC and public-key algorithms for one issuer | RFC 8725 s2.1, s3.1 |
Self-supplied key via jwk, jku or x5u | Key taken from the token instead of configuration | Keys only from pre-configured sources per issuer; allowlist URLs | RFC 8725 s3.10; ASVS 5.0 V9.1.3 |
kid injection | kid used in a file path, query or command | Treat kid as an opaque lookup key into a fixed map | RFC 8725 s3.10 |
| Cross-token substitution | ID token accepted as access token, or one service's token at another | Explicit typ (for example at+jwt), audience checks, mutually exclusive rules | RFC 8725 s3.11, s3.12; RFC 9068 s4 |
ASVS 5.0 V9.1.2 asks for an algorithm allowlist per context and says it should ideally contain only symmetric or only asymmetric algorithms, not both. If one verifier must accept both (for example during a migration from HS256 to RS256), keep the keys in separate, typed key objects and route by issuer, never by the header alone.
Key distribution and rotation
Publishing asymmetric keys with JWKS
A JWK Set (RFC 7517 s5) is a JSON document with a keys array. Each public key should carry kty, kid, use set to sig, and alg naming its single algorithm. Authorisation servers advertise the set through the jwks_uri metadata field (RFC 8414), which must use HTTPS, and RFC 9068 s4 says resource servers must use the keys the authorisation server provides. The detailed procedure, including overlap periods and emergency rollover, is in the JSON Web Key Sets (JWKS) and Signing Key Rotation Guide.
Rotating HS256 secrets
There is no public channel for an HMAC key. RFC 7517 s9.2 requires symmetric keys to be protected from unintended parties, so a symmetric JWK must never appear in a public jwks_uri. Rotation is a secrets-management task: generate a new random key, deliver it to every verifier through a secrets manager, have verifiers accept both keys selected by kid, switch the issuer to the new key, then remove the old one after the longest token lifetime has passed.
| Rotation step | HS256 | RS256, ES256, EdDSA |
|---|---|---|
| Generate new key | Random 256-bit or larger value in a secrets manager or KMS | New key pair in a KMS or HSM, private key non-exportable |
| Distribute | Push the secret to every verifier | Add the public key to the JWKS with a new kid |
| Overlap | Verifiers hold old and new secrets | JWKS holds old and new public keys; OIDC Core 10.1.1 suggests keeping recently retired keys for a while |
| Switch signing | Issuer signs with new secret | Issuer signs with new private key |
| Retire | Delete old secret everywhere after max token lifetime | Remove old public key from JWKS after max token lifetime plus cache time |
| Emergency after leak | Replace at every verifier at once; expect outages | Remove the key from JWKS; verifiers refetch on unknown kid or cache expiry |
Where each algorithm fits: decision guidance
| Scenario | Recommended | Why |
|---|---|---|
| Single web application signing its own session or CSRF state token | HS256 with a KMS-held random key | Issuer and verifier are the same code; smallest token; no distribution |
| OAuth authorisation server issuing access tokens to many APIs | RS256, PS256 or ES256 via JWKS | RFC 9068 recommends asymmetric and requires RS256 support |
| OpenID Connect ID tokens | RS256 (the default) or the client's registered id_token_signed_response_alg | OIDC Core 3.1.3.7; HS256 only for confidential clients, never public clients (s10.1) |
| Microservices calling each other through a gateway | Asymmetric issuer, verification at gateway and services | A compromised service must not be able to mint tokens for others |
| Client authentication to a token endpoint | private_key_jwt rather than client_secret_jwt | No shared secret at the server; see the JWT Client Authentication (private_key_jwt) Guide |
| High-volume, constrained or mobile clients | ES256 or EdDSA | 64-byte signatures and fast signing, if every verifier supports them |
Small organisations: RS256 works everywhere, and a managed identity provider runs the private key and JWKS for you. Large organisations: standardise on one or two asymmetric algorithms across issuers, put every signing key in a KMS or HSM, and treat any HS256 use across a service boundary as an exception that needs approval. RFC 8725 s3.2 requires cryptographic agility, and NIST IR 8547 (final, October 2026) sets out the transition from quantum-vulnerable algorithms, which include RSA and elliptic curve signatures. See the Crypto-Agility Guide.
Non-human identity token issuers
Most JWTs are now issued to and verified by machines: workload identity tokens, Kubernetes projected service account tokens, CI/CD OIDC tokens, SPIFFE JWT-SVIDs and agent-to-tool access tokens. Workload identity federation depends on asymmetric signing: the relying cloud fetches the issuer's JWKS and trusts the signature, so no long-lived secret crosses the boundary. See the Workload Identity Federation Guide and the Kubernetes Service Account Token Guide.
HS256 in machine-to-machine flows usually signals a shared secret pasted into several services' configuration, which behaves like any other static API key: it spreads, it is hard to rotate, and every holder is an issuer. Inventory these as non-human credentials and move them to an asymmetric issuer or to mutual TLS. The mTLS vs Signed Tokens for Service Authentication and X.509 SVIDs vs JWT SVIDs guides cover those choices.
Worked scenario: moving an internal platform from HS256 to ES256
A platform team runs an internal token service that signs HS256 tokens for 14 microservices. All services hold the same never-rotated secret, and an audit finds it in two container images and a CI log.
- Contain: treat the secret as compromised. Generate a new random 256-bit HMAC key in the secrets manager, push it to all 14 services, and accept both old and new keys for no longer than the token lifetime (15 minutes here) while the issuer switches. Then remove the old key everywhere.
- Design the target: choose ES256 because every service runs a library that supports it and the gateway validates it; keep RS256 as the fallback for one legacy Java service. Create non-exportable key pairs in the cloud KMS.
- Publish: expose a JWKS endpoint over HTTPS with each key's
kid,usesigandalg. - Dual verify in audit mode: issue tokens signed with both the HMAC key and ES256 under different issuer values, and log, per service, which validation succeeded. Route by issuer, never by
algheader, so no verifier ever accepts a mix for one issuer. - Cut over in rings: two low-risk services first, then the rest. Roll back by switching the issuer flag back to HMAC signing; the HMAC key remains available until the final ring passes.
- Retire HS256: remove HMAC keys from all verifiers and secrets stores, set the per-issuer allowlist to ES256 (RS256 for the legacy service), and add a test that a token with
algHS256 ornoneis rejected.
Afterwards no service can mint tokens for another, and the next rotation is a JWKS change rather than a 14-service secret push.
Detection and measurement
- Log rejections by reason: disallowed
alg, bad signature, unknownkid, wrongtyporaud. A burst ofalgnoneor HS256 attempts against an RS256 issuer is probing for confusion; see the JWT Validation Errors Troubleshooting Guide for separating attacks from misconfiguration. - Alert on unexpected signing and leaked keys: KMS sign operations from any principal other than the token service, and secret scanner hits for JWT keys in repositories, images and logs.
| Metric | Target |
|---|---|
| Cross-service issuers using a symmetric algorithm | Zero, or each one an approved exception |
| Verifiers with an explicit per-issuer algorithm allowlist | 100% |
| Signing keys held in a KMS or HSM as non-exportable | 100% of asymmetric issuers |
| Signing keys older than the rotation policy | Zero |
| Verifiers passing the negative test suite (none, confusion, foreign key) | 100%, run in CI |
Common failure patterns
- Tutorial secrets in production: an HS256 key copied from sample code or a short memorable phrase, recoverable offline from one token.
- One key object for every algorithm: the verifier passes the RSA public key to whatever routine the header names, which is the precondition for RS256 to HS256 confusion.
- HS256 shared across teams: a "temporary" shared secret between services that grows to dozens of holders, any of which can mint tokens for all.
- Keys from the token: honouring
jku,x5uor an embeddedjwkinstead of a configured key source. - Private key on disk: an RS256 PEM file in the issuer's image or repository, which turns a container leak into a forgery capability.
Framework mapping
| Control area | OWASP ASVS 5.0 | NIST SP 800-53 Rev 5 | Other |
|---|---|---|---|
| Signature or MAC validated before use | V9.1.1 | SC-23 Session Authenticity | CSF 2.0 PR.AA-03 |
| Algorithm allowlist per context | V9.1.2 | SC-13 Cryptographic Protection | RFC 8725 s3.1 |
| Keys from trusted pre-configured sources | V9.1.3 | SC-12 Cryptographic Key Establishment and Management | RFC 8725 s3.10 |
| Validity period enforced | V9.2.1 | SC-23 | RFC 9068 s4 |
| Signing key and secret lifecycle | V9.1.3 | SC-12, IA-5 Authenticator Management | CSF 2.0 PR.AA-01 |
| Forged token threat | V9.1.1, V9.1.2 | SC-13, SC-23 | ATT&CK T1606, T1550.001 |
Practitioner checklist
- Every issuer has one documented algorithm and one key source, stored in verifier configuration.
- Tokens that cross a service or organisation boundary use RS256, PS256, ES256 or EdDSA.
- Any HS256 key is random, at least 256 bits, held in a secrets manager and known to the fewest possible services.
- Public clients never use HS256 ID tokens or
client_secret-based MACs. - Verifiers reject
none, mismatched algorithms, self-supplied keys and unknownkidvalues after one rate-limited refetch. - Asymmetric signing keys are non-exportable in a KMS or HSM, 2048-bit or larger for RSA.
- JWKS entries carry
kid,useandalg; no symmetric key appears in a public key set. - Negative tests for algorithm confusion run in CI for every verifier.
Standards and references
- IETF: RFC 7518 JSON Web Algorithms (JWA)
- IETF: RFC 8725 JSON Web Token Best Current Practices
- IETF: RFC 7517 JSON Web Key (JWK)
- IETF: RFC 8037 CFRG Elliptic Curve Diffie-Hellman and Signatures in JOSE
- IETF: RFC 9068 JWT Profile for OAuth 2.0 Access Tokens
- IETF: RFC 8414 OAuth 2.0 Authorization Server Metadata
- OpenID Foundation: OpenID Connect Core 1.0
- NIST: IR 8547 Transition to Post-Quantum Cryptography Standards
Related NHI Mgmt Group resources: JWT Security Guide · JWT vs Opaque Tokens · JSON Web Key Sets (JWKS) and Signing Key Rotation Guide · Cryptographic Key Management Guide · Client ID and Client Secret Guide