Choosing a JWT signing algorithm

HS256, RS256, PS256, ES256 or EdDSA? How JWT signing algorithms differ in keys, token size, speed and support, when to pick each, and why none is never one.

API Security6 min read

Most teams take whatever JWT algorithm their library or identity provider defaults to. That’s often fine. But the choice decides who can create tokens, how keys are shared, how big each token is, and which mistakes are possible.

The registered JWS signing algorithms fall into five families, and you can try every one of them in the Encode mode of our free JWT tool.

The families at a glance

The algorithms are defined in RFC 7518 (HMAC, RSA, RSA-PSS and ECDSA), RFC 8037 (EdDSA), RFC 8812 (ES256K) and RFC 9864 (the fully specified Ed25519 and Ed448 identifiers).

Family Algorithms Key Signature size
HMAC HS256, HS384, HS512 One shared secret 32, 48 or 64 bytes
RSA PKCS#1 v1.5 RS256, RS384, RS512 RSA key pair Same as the key: 256 bytes for 2048-bit
RSA-PSS PS256, PS384, PS512 RSA key pair Same as the key
ECDSA ES256 (P-256), ES384 (P-384), ES512 (P-521), ES256K (secp256k1) EC key pair 64, 96, 132 and 64 bytes
EdDSA EdDSA, Ed25519, Ed448 OKP key pair 64 bytes (Ed25519), 114 bytes (Ed448)

The signature is Base64URL-encoded in the token, so it grows by about a third. An RS256 signature with a 2048-bit key adds 342 characters; an ES256 or Ed25519 signature adds 86.

Symmetric or asymmetric

HMAC: one secret, two jobs

With HS256 the same secret signs and verifies, so every service that checks tokens can also create them. That’s fine when one service issues and checks its own tokens. It becomes a problem when several services, partners or a gateway need to verify, because each one is a place the secret can leak from, and a leak anywhere lets an attacker mint tokens for everywhere.

HMAC is also only as strong as its secret. RFC 7518 requires a key at least as long as the hash output: 256 bits (32 bytes) for HS256, 384 for HS384 and 512 for HS512. Use random bytes, not a memorable phrase. A short or guessable secret can be cracked offline from a single captured token.

RSA, ECDSA and EdDSA: sign privately, verify publicly

With an asymmetric algorithm, only the issuer holds the private key. Verifiers get the public key, often as a JWK Set at a well-known URL, so a compromised verifier can’t forge tokens. That’s why identity providers and OpenID Connect use asymmetric signatures, and why they’re the safer default as soon as more than one system is involved.

The cost is key management: publishing public keys, choosing key IDs and rotating keys. Our post JWT keys explained: PEM, JWK and JWK Sets covers that side.

RSA: RS versus PS

RS256 uses RSA with PKCS#1 v1.5 padding. It’s the oldest and most widely supported asymmetric JWT algorithm. RFC 7518 marks it “Recommended”, and OpenID Connect providers are expected to support it for ID tokens, so it’s the safe choice when you don’t control every verifier.

PS256 uses the same RSA keys with PSS padding, a more modern scheme with a stronger security proof and a random salt in every signature. RFC 7518 marks it “Optional”, so check that every verifier supports it before switching.

Both need keys of 2048 bits or more (RFC 7518 requires it). Larger keys, such as 3072 or 4096 bits, are slower and produce bigger signatures. RSA verification is fast; RSA signing and especially key generation are much slower than with elliptic curves.

ECDSA: ES256 and friends

ES256 uses the P-256 curve. A 256-bit EC key gives security comparable to a 3072-bit RSA key, according to NIST SP 800-57 Part 1, with much smaller keys and signatures. RFC 7518 marks ES256 “Recommended+”, meaning it is expected to become more strongly required over time.

ECDSA has two pitfalls:

  • It needs a fresh, unpredictable random value for every signature. If that value is reused or predictable, the private key can be worked out from the signatures. Good libraries handle this, often deterministically, but home-grown signing code is risky.
  • JWS signatures are raw, not DER. ES256 signatures are the r and s values joined into exactly 64 bytes. Code that passes a DER-encoded signature from a general crypto library will produce tokens other systems reject.

ES384 and ES512 use larger curves (ES512 uses P-521, not a 512-bit curve). ES256K uses secp256k1, the curve common in blockchain systems. It’s useful when you need to interoperate with those, but library support is narrower.

EdDSA: Ed25519 and Ed448

EdDSA (RFC 8037) is fast, has small keys and signatures, and is deterministic, so there’s no per-signature random value to get wrong. Ed25519 is the common choice; Ed448 offers a higher security level with larger signatures.

The naming is awkward. RFC 8037 uses a single identifier, EdDSA, for both curves, so the curve comes from the key and not the header. RFC 9864 registers fully specified identifiers, Ed25519 and Ed448, and deprecates EdDSA for new deployments. At the time of writing, support for the new identifiers is still arriving in libraries, and EdDSA itself is newer in some older runtimes, so test every verifier before relying on it.

Which one to pick

  • One service issues and verifies its own tokens: HS256 with a random secret of at least 32 bytes, kept in a secrets manager.
  • Several services or third parties verify: an asymmetric algorithm. ES256 is a good default because it’s small, fast and widely supported.
  • You need the broadest compatibility, or you’re an OpenID Connect provider: RS256 with a 2048-bit or larger key, perhaps alongside ES256.
  • You control every verifier and want something modern and simple: Ed25519 (identifier EdDSA, or Ed25519 where supported).
  • ES256K or Ed448: only when a specific integration needs them.

Whatever you pick, the verifier should enforce the algorithm it expects instead of reading it from the token. Many serious JWT flaws come from a verifier letting the header choose, as covered in JWT mistakes we see in real APIs.

And “none”?

alg: none means the token is unsigned. RFC 7518 defines it for cases where integrity is protected some other way, and RFC 8725, the JWT best current practice, says libraries should not generate or accept it unless the application explicitly asks. For access tokens there’s no good reason to accept it at all, so your server must reject it.

Try each one in the Encode mode

Open the Encode & sign mode of the JWT tool:

  1. Pick an algorithm from the list. It’s grouped by family and includes every algorithm above, plus none.
  2. Get a key. For HMAC, use Generate a strong secret, which creates random bytes the length of the hash. For the others, a key pair is generated for you; for RSA you can choose 2048, 3072 or 4096 bits. You can paste your own test key instead (PEM or JWK).
  3. Build the payload with the claim helpers: iat, exp (from 5 minutes to 30 days), nbf and a random jti. + kid from key sets the header’s kid to the key’s thumbprint.
  4. Compare. Switch algorithms and watch the token length change. The tool shows the public key as PEM and JWK for verifying, and warns if an HMAC secret is too short or an RSA key is under 2048 bits.
  5. Decode and verify it sends the token and key to the decoder, so you can see the signature check succeed and read the findings.

Signing happens in your browser, using its built-in Web Crypto, or an audited JavaScript library for ES256K and Ed448, which browsers don’t support natively. Use test keys only.

The unsigned option is there so you can confirm your own server rejects it. Our JWT Security Testing Guide covers that and other checks, and How to decode and verify a JWT safely walks through the decoder.

Get it

Want to know how your API handles tokens signed with the wrong key or algorithm? See API penetration testing.

One practical security note a week.

No spam. Unsubscribe any time.

Need a pentest for an audit, a customer, or peace of mind?

Tell us what you need tested. You’ll get a fixed-fee proposal with tester-days and timeline stated, usually within a few days of the scoping call.