
# 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`](/jws/multi) | 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`](/jwk/generate) under the algorithm you pick: the key then carries its `alg` and `kid`, and neither `sign` nor `verify` needs the `alg` option.

```ts
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.** `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](https://www.rfc-editor.org/rfc/rfc7518#section-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](https://www.rfc-editor.org/rfc/rfc7518#section-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](https://www.rfc-editor.org/rfc/rfc8037) 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`.

```ts
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](https://www.rfc-editor.org/rfc/rfc8725#section-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](/jws/verify#restrict-the-algorithms)).

## Going further

- **Generating and storing the key**: [Generate keys](/jwk/generate).
- **Keys that arrive as PEM files**: [Use PEM keys](/jwk/pem).
- **Encrypting instead of signing**: [Choose encryption algorithms](/jwe/algorithms).
