opyebJWTlab

HS256, RS256, ES256, EdDSA: choosing a signature algorithm

Algorithms · 7 min read

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

algKeyHashSignatureTypical use
HS256/384/512Shared secretSHA-256/384/51232/48/64 BOne service or internal API
RS256/384/512RSA 2048 bits or moreSHA-256/384/512256 BMany verifiers, existing PKI
PS256/384/512RSA with PSS paddingSHA-256/384/512256 BRSA with stronger padding
ES256/384EC P-256 or P-384SHA-256/38464/96 BMobile, IoT, compact tokens
EdDSAEd25519SHA-512 internally64 BModern stacks, deterministic signatures

How to choose

  1. Are there several verifiers? Then go asymmetric. Handing the secret to every verifier means one compromise breaks all of them.
  2. 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.
  3. Must the token be small? An ES256 signature is 64 bytes, a quarter of RS256. That matters on mobile headers.
  4. 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 decoder

Frequently 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