Choose a signing algorithm

Pick the alg that fits who signs and who verifies, and know which key each one needs and what the library refuses.

The algorithm decides what kind of key signs and what kind checks. With a shared secret (HS*), the same key does both, so everyone who can verify can also sign. With a key pair (ES*, EdDSA, RS*, PS*), the private key signs and the public key only verifies, so you can hand the public key to anyone. Most of the choice follows from that one question.

#Pick by who verifies

Your situationPickWhy
One service signs and checks its own tokens (sessions, internal state)HS256One secret to guard, small and fast. Never share it with a verifier you don't run.
An issuer signs, other services or third parties verifyES256 or EdDSAPublish the public key; small keys and signatures, nothing to tune.
A partner, a standard or a regulation requires RSAPS256RSA with modern padding. Use RS256 only when the other side requires it.
Moving from one algorithm to anotherboth, with signMultiVerifiers on either side accept the token during the move.

ES256 is the safe default for an issuer: every JOSE library and identity provider speaks it. EdDSA is as good where the verifiers support it. When in doubt, generate the key with generateJWK under the algorithm you pick: the key then carries its alg and kid, and neither sign nor verify needs the alg option.

const { privateKey, publicKey } = await generateJWK("ES256");
const token = await sign({ sub: "user_1a2b" }, privateKey); // alg from the key
await verify(token, publicKey); // and the accepted alg too

#Supported algorithms

algKeySize the library requires
HS256, HS384, HS512shared secret, oct JWK or bytesat least 32, 48 or 64 bytes
RS256, RS384, RS512RSA key pair, PKCS#1 v1.5 signaturesmodulus of 2048 bits or more
PS256, PS384, PS512RSA key pair, PSS signaturesmodulus of 2048 bits or more
ES256EC key pair on P-256fixed by the curve
ES384EC key pair on P-384fixed by the curve
ES512EC key pair on P-521fixed by the curve
EdDSA, Ed25519OKP key pair on Ed25519fixed by the curve
  • The number is the hash size. ES512 uses the curve P-521; that's not a typo.
  • HMAC secrets shorter than the hash output are refused at signing and at verification (RFC 7518 §3.2). generateJWK makes 64 bytes for HS256 and 128 for HS384 and HS512.
  • RSA keys under 2048 bits are refused everywhere, generation included (RFC 7518 §3.3). generateJWK makes 2048-bit keys unless you pass modulusLength.
  • EdDSA and Ed25519 name the same signature over the same key: EdDSA is the RFC 8037 spelling most libraries emit, Ed25519 the fully specified one. A key made under either name signs and verifies under both, and the header carries the name you signed with. An explicit algorithms list is still exact.
  • A key's alg is binding: a key declared ES384 doesn't sign or verify ES256.
await sign({ sub: "user_1a2b" }, new Uint8Array(32), { alg: "HS512" });
// JWTError ERR_JWK_INVALID: HS512 requires a key of at least 64 bytes (got 32)

await generateJWK("RS256", { modulusLength: 1024 });
// JWTError ERR_INVALID_OPTION: modulusLength must be at least 2048 bits

#What is refused

  • none, an unsigned token, by sign and verify alike, before any key is read (RFC 8725 §3.1): ERR_JWS_ALG_NOT_ALLOWED.
  • Any name outside the table, such as ES256K: ERR_JWS_ALG_NOT_ALLOWED ("Algorithm not supported").
  • Ed448 keys, at generation (ERR_JWK_UNSUPPORTED) and import (ERR_JWK_INVALID).
  • An algorithm you didn't allow: a verifier that passes algorithms, or holds a key with alg, refuses every other alg the token's header names (Verify a token).

#Going further