HS256, RS256, ES256, EdDSA: choosing a signature algorithm
In one line: Symmetric HMAC versus asymmetric RSA, ECDSA and EdDSA, key and signature sizes, and when to prefer PS256 or ES256.
The alg header says what signed the token. The first split is symmetric (HMAC) versus asymmetric (RSA, ECDSA, EdDSA); the first question is whether every verifier may hold the secret.
Comparison
| alg | Key | Hash | Signature | Typical use |
|---|---|---|---|---|
| HS256/384/512 | Shared secret | SHA-256/384/512 | 32/48/64 B | One service or internal API |
| RS256/384/512 | RSA 2048 bits or more | SHA-256/384/512 | 256 B | Many verifiers, existing PKI |
| PS256/384/512 | RSA with PSS padding | SHA-256/384/512 | 256 B | RSA with stronger padding |
| ES256/384 | EC P-256 or P-384 | SHA-256/384 | 64/96 B | Mobile, IoT, compact tokens |
| EdDSA | Ed25519 | SHA-512 internally | 64 B | Modern stacks, deterministic signatures |
How to choose
- Are there several verifiers? Then go asymmetric. Handing the secret to every verifier means one compromise breaks all of them.
- Do you already run RSA? With existing certificates or an HSM on RSA, RS256 or PS256 is natural. For new systems PS256 has the stronger padding.
- Must the token be small? An ES256 signature is 64 bytes, a quarter of RS256. That matters on mobile headers.
- Is the runtime current? Ed25519 signs deterministically and verifies quickly, but older libraries and HSMs may not support it.
The HMAC trap
HS256 is only as strong as the secret's entropy. Words such as secret or changeme and short strings fall to a dictionary attack immediately. Use a random value of at least 32 bytes and keep it in an environment variable or a secret manager. A plaintext secret in a config file or repository is equivalent to letting anyone mint tokens.
# a 32-byte random secret (example) openssl rand -base64 32 # HS256 verification in pseudo code expected = HMAC-SHA256(secret, header_b64 + "." + payload_b64) ok = constant_time_compare(expected, signature)
Signature encoding differs too
In a JWT an ES256 signature is fixed-length r||s (32 bytes each). Feeding that to a library that expects DER fails verification. RSA and EdDSA have a single signature encoding, so no conversion is needed. This is why "it works in another tool but not here" usually means one side expects DER and the other raw values.
Decoding and signature checks run right here in your browser. The token you paste is not stored.
Open the JWTlab decoderFrequently asked questions
How long should an HS256 secret be?
A random value of at least 32 bytes (256 bits). Passphrases have low entropy. Storing 64 bytes or more as base64 keeps management easy and resists dictionary attacks.
What is the difference between PS256 and RS256?
Both are RSA with different padding: RS256 uses PKCS#1 v1.5, PS256 uses probabilistic PSS. PSS is more resistant to padding-related attacks and is what current specifications recommend. Check that your verifier supports PSS first.
Sources
Read next
- JWT structure and Base64URL: reading the three parts correctlyWhat the header, payload and signature parts carry, how Base64URL differs from Base64, and where people go wrong when reading a token.
- alg=none and algorithm confusion: why the header alg is not trustworthyHow alg=none skips signature checking, how algorithm confusion uses a public key as an HMAC secret, and how to defend against both.
- Validating exp, nbf, iat, aud and iss, and handling clock skewThe order for validating time claims, audience and issuer, how to set a leeway, and what to do about a token without exp.