HS256 vs RS256: shared secret or public key for signing JWTs?

Use HS256 when one service both issues and checks tokens. Use RS256 (or ES256) when other services, partners or the public need to verify tokens they must never be able to forge.

HS256 is HMAC with SHA-256. The same secret signs and verifies, so every service that checks tokens could also mint them. RS256 is RSA with SHA-256. The issuer signs with a private key, and anyone can verify with the matching public key, which can be published openly. The choice is mostly about who you trust with the power to create tokens.

By TechCompare · Updated

Topic
HS256 vs RS256
Signing algorithms
Category
Algorithm Comparison
Signing algorithms side by side

How this is calculated

With HS256, the secret is the whole security story. RFC 7518 requires a key at least as long as the hash output, so 256 bits, which is 32 random bytes. A human password like "secret123" is far too weak, because anyone holding one valid token can test guesses offline on a GPU at a huge rate. Every verifier needs a copy of the secret, so a leak from any of them lets an attacker sign tokens for all of them. RS256 splits those roles. Only the issuer holds the private key. APIs, gateways and other teams fetch the public key, usually from a JWKS endpoint, and a leaked public key is harmless. That's why OpenID Connect providers sign ID tokens with RS256 by default and publish their keys. The cost is size and speed. An HS256 signature is 32 bytes, which is 43 Base64url characters, while a 2048-bit RSA signature is 342. Signing with RSA also takes noticeably longer than HMAC, though verifying is still quick. Whichever you choose, the verifier should only accept the algorithm it expects, never whatever the header says.

Verdict

For a single app with its own login, HS256 with a 32-byte random secret is simple and safe. As soon as a second service or a third party verifies your tokens, move to asymmetric keys, either RS256 for the widest library support or ES256 for compact signatures. Pin the expected algorithm in every verifier.

More JWT scenarios

Frequently asked questions

Is HS256 less secure than RS256?
Not in the math. Both are strong with proper keys. HS256's risk is operational: the same secret has to live on every verifier, and a weak secret can be cracked offline from any token. RS256 keeps the signing key in one place.
Can I switch from HS256 to RS256 later?
Yes. Issue new tokens with RS256 and a new kid, let verifiers accept both for one token lifetime, then turn HS256 off. Make sure each verifier checks the algorithm against the key type so the overlap can't be abused.
What does the 256 mean?
It's the SHA-256 hash used inside the algorithm, not the key size. RS256 usually uses a 2048-bit RSA key, and HS256 needs a secret of at least 256 bits.