HS256 vs RS256: Which JWT Signing Algorithm Should You Use?

Compare HMAC (HS256) and RSA (RS256) JWT signatures, plus ES256 and EdDSA: who can create tokens, key sizes, performance and how to verify each one.

The short answer

Use HS256 when the same service both issues and verifies tokens, such as a single backend that signs its own session tokens. Use RS256, ES256 or EdDSA when other services, clients or third parties must verify tokens, which covers OAuth 2.0 and OpenID Connect providers and most microservice setups.

The difference is not strength, since all of them are secure with good keys. It is who holds the key that can create tokens.

HS256: one shared secret

HS256 is HMAC with SHA-256. The same secret both signs and verifies, so every service that can check a token can also mint one. If any verifier leaks the secret, an attacker can create valid tokens for every service.

The secret must be random and at least 32 bytes. Short or copied secrets such as secret or your-256-bit-secret can be guessed offline from a single captured token. Paste an HS256 token into the JWT decoder and run Check secret strength to test it against well-known weak secrets.

header:    {"alg":"HS256","typ":"JWT"}
signature: HMAC-SHA256(base64url(header) + "." + base64url(payload), secret)
verify:    same secret

RS256: private key signs, public key verifies

RS256 is RSASSA-PKCS1-v1_5 with SHA-256. Only the issuer holds the private key; everyone else verifies with the public key, usually downloaded from a JWKS endpoint. A leaked public key is harmless, and keys can be rotated by publishing a new kid.

RSA keys should be at least 2048 bits, which makes RS256 signatures 256 bytes, the largest of the common algorithms. PS256 uses the same keys with the more modern PSS padding.

header:    {"alg":"RS256","kid":"2024-06"}
signature: RSASSA-PKCS1-v1_5-SHA256(signing input, private key)
verify:    public key from https://issuer/.well-known/jwks.json

ES256 and EdDSA: smaller asymmetric signatures

ES256 (ECDSA on P-256) and EdDSA (Ed25519) give the same public-key model as RS256 with much smaller keys and 64-byte signatures, which keeps tokens short. EdDSA is fast and deterministic; ES256 has the broadest library and hardware support.

Check that every library and gateway in your path supports the algorithm before switching. RS256 remains the most widely supported default.

Comparison at a glance

Signature sizes and key sizes below are for typical secure settings. All four are safe when keys are generated randomly and verifiers enforce an explicit algorithm allowlist.

Algorithm  Key type          Who can mint     Signature  Typical use
HS256      shared secret     every verifier   32 bytes   single service, sessions
RS256      RSA 2048+ bits    issuer only      256 bytes  OAuth/OIDC providers
ES256      EC P-256          issuer only      64 bytes   mobile, short tokens
EdDSA      Ed25519           issuer only      64 bytes   modern services

Avoid algorithm confusion

Never let the token choose how it is verified. A verifier that accepts whatever alg the header names can be tricked into treating an RSA public key as an HMAC secret, or into accepting alg none. Configure the exact algorithms you expect, and look keys up by kid from your own trusted key set.

To try each algorithm, open the JWT decoder's Encode & sign mode, generate a secret or key pair, and decode the result to verify it. Keys & JWKS converts the generated keys to PEM, JWK and JWKS.

Reference: RFC 7518: JSON Web Algorithms (JWA).