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
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
Related guides
Frequently asked questions
Is HS256 less secure than RS256?
Can I switch from HS256 to RS256 later?
What does the 256 mean?
Related tools
Text Encoding Converter
Convert between Text, Base64, Binary, Hexadecimal, and Decimal formats.
Use tool ➜Unix Timestamp
Convert between Unix timestamps and human-readable dates.
Use tool ➜JSON Formatter
Validate, format, and minify JSON. Browse it as a tree, run JSONPath queries, compare two documents, or validate against a JSON Schema.
Use tool ➜