JWT Signature Algorithms
You'll learn how JWTs use different cryptographic algorithms to ensure tokens haven't been tampered with, and the security trade-offs between symmetric and asymmetric approaches.
Why JWTs Need Signatures
A JWT's signature is what makes it trustworthy. Without verification, anyone could create a fake token claiming to be an admin. The signature proves the token came from a legitimate source and hasn't been modified.
Two Types of Signing Algorithms
HMAC (Symmetric)
Algorithm codes: HS256, HS384, HS512
HMAC uses a shared secret — the same key both signs and verifies the token. Think of it like a password you and your friend both know to seal and open messages.
Pros:
- Fast and simple
- Lower computational overhead
Cons:
- Every service that verifies tokens needs the secret
- If one service is compromised, all tokens are at risk
- Can't distinguish who created the token (anyone with the key could have)
RSA/ECDSA (Asymmetric)
Algorithm codes: RS256, RS512 (RSA), ES256, ES512 (ECDSA)
Asymmetric algorithms use a key pair: a private key signs tokens, but a public key verifies them. It's like a wax seal — only you have the stamp (private key), but anyone can verify it's authentic (public key).
Pros:
- Public keys can be freely distributed for verification
- Private key compromise doesn't expose all services
- Enables trust across organizational boundaries
Cons:
- Slower than HMAC
- More complex key management
When to Use Each
Use HMAC when your authentication server and verification services are tightly coupled and trust each other completely.
Use RSA/ECDSA when distributing tokens to third parties or across multiple services that shouldn't share signing capabilities.
Key Takeaway: HMAC is simpler but requires sharing secrets; RSA/ECDSA is more complex but enables secure public verification without exposing signing capability.