HS256 vs RS256: Symmetric vs Asymmetric JWTs
Choose the right signing algorithm for your authentication system.
HS256 vs RS256: Symmetric vs Asymmetric JWTs is a free Cryptology Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Cryptology Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Algorithm Families
JWT signing uses either symmetric (HMAC) or asymmetric (RSA, ECDSA) algorithms. HS256 = HMAC-SHA256. RS256 = RSA-PKCS1v1.5-SHA256. ES256 = ECDSA-P256-SHA256. The choice impacts key distribution and trust model.
HS256: Symmetric HMAC
HS256 uses a single shared secret key for both signing and verification. Both the issuer and every verifier must possess the same secret. Simple to implement but requires secure key distribution to all verifiers.
HS256 Use Cases
HS256 is appropriate when issuer and verifier are the same service (e.g., a monolithic API signing and verifying its own tokens). Never use HS256 if multiple independent services need to verify tokens — they all need the secret.
RS256: Asymmetric RSA
RS256 uses a private key to sign and a public key to verify. Only the auth server holds the private key. All resource servers fetch the public key from the JWKS endpoint and verify locally — no secret sharing required.
RS256 Use Cases
RS256 is ideal for microservices architectures: the identity provider signs tokens; each downstream service verifies with the public key without contacting the IDP per request. Scales horizontally without key distribution risk.
ES256: ECDSA Alternative
ES256 uses ECDSA over P-256. Signature size is 64 bytes vs RS256's 256 bytes for a 2048-bit key. Verification is ~10x faster than RS256. Preferred when bandwidth or verification speed is critical (mobile APIs, IoT).
Key Rotation
For RS256/ES256: rotate keys by publishing new key (new kid) to JWKS, signing new tokens with the new key, allowing a grace period for old tokens to expire, then removing the old key. HMAC rotation requires re-distributing the new secret.
Performance Comparison
HS256 verification: ~1 µs. RS256 verification: ~100 µs (2048-bit). ES256 verification: ~50 µs (P-256). HS256 is fastest but requires secret sharing. ES256 best balances security and performance for distributed systems.
The alg Header Attack Preview
A critical vulnerability: if the verifier trusts the alg header blindly, an attacker can change alg from RS256 to HS256 and sign the token with the server's public key (which is public) as the HMAC secret. Fix: never trust the alg header.
Selecting an Algorithm
Decision: single-service → HS256. Multi-service/microservices → RS256 or ES256. High-volume mobile → ES256. If in doubt, use ES256 (P-256) or RS256 with 2048+ bit keys. Never use RS512 for performance-sensitive paths.
Libraries and Standards
Use PyJWT (Python), jose (Node.js), or auth0/java-jwt. Always pin the expected algorithm in the verify call: jwt.decode(token, secret, algorithms=["HS256"]). Never pass algorithms=None or omit the parameter.
Quick Check
Why is RS256 preferred over HS256 in microservices architectures?
Recap
HS256 is symmetric (shared secret); RS256/ES256 are asymmetric (private sign, public verify). Use asymmetric algorithms for multi-party systems. Next: exploiting JWT vulnerabilities.
Frequently asked questions
Is the “HS256 vs RS256: Symmetric vs Asymmetric JWTs” lesson free?
Yes — the full text of “HS256 vs RS256: Symmetric vs Asymmetric JWTs” is free to read here on the web, and the Cryptology Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Cryptology Academy course, upgrade to CoddyKit PRO.
What will I learn in “HS256 vs RS256: Symmetric vs Asymmetric JWTs”?
Choose the right signing algorithm for your authentication system. You practise Cryptology Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Cryptology Academy?
No prior experience is required. Cryptology Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “HS256 vs RS256: Symmetric vs Asymmetric JWTs” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Cryptology Academy lesson?
Yes. Every Cryptology Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- JWT Anatomy: Header, Payload, Signature
- HS256 vs RS256: Symmetric vs Asymmetric JWTs
- JWT Vulnerabilities: alg=none & Key Confusion
- Secure JWT Implementation Best Practices