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 situation | Pick | Why |
|---|---|---|
| One service signs and checks its own tokens (sessions, internal state) | HS256 | One secret to guard, small and fast. Never share it with a verifier you don't run. |
| An issuer signs, other services or third parties verify | ES256 or EdDSA | Publish the public key; small keys and signatures, nothing to tune. |
| A partner, a standard or a regulation requires RSA | PS256 | RSA with modern padding. Use RS256 only when the other side requires it. |
| Moving from one algorithm to another | both, with signMulti | Verifiers 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
alg | Key | Size the library requires |
|---|---|---|
HS256, HS384, HS512 | shared secret, oct JWK or bytes | at least 32, 48 or 64 bytes |
RS256, RS384, RS512 | RSA key pair, PKCS#1 v1.5 signatures | modulus of 2048 bits or more |
PS256, PS384, PS512 | RSA key pair, PSS signatures | modulus of 2048 bits or more |
ES256 | EC key pair on P-256 | fixed by the curve |
ES384 | EC key pair on P-384 | fixed by the curve |
ES512 | EC key pair on P-521 | fixed by the curve |
EdDSA, Ed25519 | OKP key pair on Ed25519 | fixed by the curve |
- The number is the hash size.
ES512uses the curveP-521; that's not a typo. - HMAC secrets shorter than the hash output are refused at signing and at verification (RFC 7518 §3.2).
generateJWKmakes 64 bytes forHS256and 128 forHS384andHS512. - RSA keys under 2048 bits are refused everywhere, generation included (RFC 7518 §3.3).
generateJWKmakes 2048-bit keys unless you passmodulusLength. EdDSAandEd25519name the same signature over the same key:EdDSAis the RFC 8037 spelling most libraries emit,Ed25519the fully specified one. A key made under either name signs and verifies under both, and the header carries the name you signed with. An explicitalgorithmslist is still exact.- A key's
algis binding: a key declaredES384doesn't sign or verifyES256.
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, bysignandverifyalike, 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"). Ed448keys, 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 withalg, refuses every otheralgthe token's header names (Verify a token).
#Going further
- Generating and storing the key: Generate keys.
- Keys that arrive as PEM files: Use PEM keys.
- Encrypting instead of signing: Choose encryption algorithms.