How to Create and Sign a JWT: HS256 vs RS256 vs ES256 Explained

Learn how a JSON Web Token is built and signed, when to pick HS256, RS256 or ES256, which claims to set, and how to generate test tokens safely.

Updated September 29, 2026 7 min read

Use the free JWT GeneratorCreate and sign JSON Web Tokens with HS256, RS256, ES256 and more

You are building an API, and the endpoint you are testing wants an Authorization: Bearer ... header. Or a partner has asked you to send them signed tokens, and you need to check that your server produces exactly what they expect. Either way you need a JSON Web Token (JWT) that is correctly formed, correctly signed and has sensible expiry times.

This guide explains how a JWT is put together, what the different signing algorithms mean in practice, which claims matter, and how to produce a test token in your browser or in a few lines of Node.js.

The three parts of a JWT

A JWT is three Base64url-encoded strings joined with dots:

header.payload.signature
  • Header: a small JSON object that names the signing algorithm and type, for example {"alg":"HS256","typ":"JWT"}.
  • Payload: a JSON object of claims, such as who the token is about (sub), when it was issued (iat) and when it expires (exp).
  • Signature: the result of signing the exact string header.payload with a secret or private key.

Base64url is ordinary Base64 with + and / swapped for - and _, and the trailing = padding removed, so the token is safe in URLs and headers. If you want the background on the encoding itself, see what Base64 encoding is and how it works.

The most important consequence: the payload is encoded, not encrypted. Anyone who holds the token can decode it and read every claim. The signature only proves that nobody changed the header or payload since it was signed. Never put passwords, API keys or personal data you would not show the user into a JWT.

HS256, RS256, PS256 and ES256: which one?

The alg value decides how the signature is made. The families behave very differently:

Family Examples Key type Who can verify
HMAC HS256, HS384, HS512 One shared secret Anyone who has the same secret
RSA PKCS#1 v1.5 RS256, RS384, RS512 RSA private/public pair Anyone with the public key
RSA-PSS PS256, PS384, PS512 RSA private/public pair Anyone with the public key
ECDSA ES256, ES384, ES512 Elliptic-curve pair (P-256, P-384, P-521) Anyone with the public key

HMAC (HS*) is simple and fast, but the same secret both signs and verifies. That is fine when one service issues and checks its own tokens. It becomes a problem when several services need to verify tokens, because each of them would then also be able to create them.

Asymmetric algorithms (RS*, PS*, ES*) sign with a private key and verify with a public key. You can publish the public key (often as a JWKS endpoint) and keep signing power in one place. RS256 is the most widely supported. PS256 uses the newer RSA-PSS padding. ES256 produces much shorter signatures and keys for similar security, which is handy when tokens travel in every request.

A practical rule: use HS256 for a single self-contained app, and RS256 or ES256 whenever a third party or another team needs to verify your tokens.

How long should an HMAC secret be?

RFC 7518 says the HMAC key should be at least as long as the hash output: 32 bytes for HS256, 48 for HS384 and 64 for HS512. A short word like secret works technically but can be brute-forced offline by anyone who has one token. Generate random bytes rather than inventing a phrase; a password generator set to a long length is a quick way to get one for testing.

Claims worth setting

The payload can hold any JSON, but a few registered claims have agreed meanings:

  • sub (subject): the user or entity ID the token is about.
  • iat (issued at): a Unix timestamp in seconds, not milliseconds.
  • exp (expiration): also Unix seconds. Libraries reject tokens after this time.
  • nbf (not before): the token is invalid until this time.
  • iss and aud: who issued the token and who it is for. Verifiers should check both.

The seconds-versus-milliseconds mix-up is the most common bug. Date.now() in JavaScript returns milliseconds, so a token with exp: Date.now() appears to expire tens of thousands of years from now. If you need to check or convert a value, use the Unix timestamp converter.

Generate a signed token in your browser

The JWT Generator signs tokens with the browser's Web Crypto API, so the secret or private key you type stays in the tab.

  1. Open the JWT Generator and choose an Algorithm. The list shows the key type next to each option, such as "HS256 (HMAC secret)" or "ES256 (ECDSA key)". Changing it also updates alg in the header.
  2. Edit the Header (JSON) box if you need extra fields, such as a kid key ID.
  3. Edit the Payload (JSON) box. Use the buttons above it to set iat = now, add an expiry with exp +1h, exp +1d or exp +7d, or remove it with No exp.
  4. For HS algorithms, type the Secret. Tick Secret is Base64 encoded if your secret is stored as Base64 bytes. The tool warns you when the secret is shorter than RFC 7518 recommends.
  5. For RS, PS and ES algorithms, paste a Private key (PKCS#8 PEM), or click Generate key pair to create a fresh test pair. The matching public key appears underneath so you can give it to whatever needs to verify.
  6. The Signed JWT panel updates as you type, with the header, payload and signature parts coloured separately. Click Copy token.

If you paste an older BEGIN RSA PRIVATE KEY or BEGIN EC PRIVATE KEY file, the tool tells you to convert it to PKCS#8 first:

openssl pkcs8 -topk8 -nocrypt -in key.pem -out key-pkcs8.pem

To check the result, paste the token into the JWT Decoder and confirm the claims and expiry look right.

Signing a JWT in code

In a real application you will sign tokens on the server. Most languages have a maintained library (jsonwebtoken or jose in Node.js, PyJWT in Python), and you should use one. It helps to see that there is no magic involved, though. This Node.js script builds an HS256 token with only the standard library:

import { createHmac } from "node:crypto";

const b64url = (obj) => Buffer.from(JSON.stringify(obj)).toString("base64url");

const header = { alg: "HS256", typ: "JWT" };
const now = Math.floor(Date.now() / 1000);
const payload = { sub: "user-42", role: "editor", iat: now, exp: now + 3600 };

const input = `${b64url(header)}.${b64url(payload)}`;
const signature = createHmac("sha256", process.env.JWT_SECRET ?? "change-me-to-32-plus-random-bytes")
  .update(input)
  .digest("base64url");

console.log(`${input}.${signature}`);

The same secret and payload in the browser tool will give an identical token, as long as the JSON has the same key order and spacing. That makes the tool a quick way to cross-check a server that is producing tokens a client rejects.

Common mistakes and how to avoid them

"Invalid signature" on the receiving side

Check, in order: the algorithm matches on both sides; the secret is the same bytes (a Base64 secret decoded on one side but used as text on the other is a classic mismatch); and for RSA or EC, the verifier has the public key that matches your private key.

Tokens that never expire

A token without exp is valid until the key changes. For access tokens, keep lifetimes short (minutes to hours) and use a separate refresh mechanism rather than issuing week-long bearer tokens.

Accepting the algorithm from the token

Verifiers should be configured with the algorithm they expect, not trust whatever alg the incoming header says. Older libraries that accepted alg: none, or accepted an HMAC token signed with a public RSA key used as a secret, caused real vulnerabilities.

Using production keys in test tools

Even when a tool runs locally, treat production signing keys as something that never leaves your key store. Generate a throwaway pair for testing and keep the real one on the server.

Frequently asked questions

Is a JWT encrypted?

No. A standard signed JWT (JWS) is only encoded, so anyone can read the payload. If the contents must be hidden, use an encrypted token (JWE) or keep the data on the server and put only an ID in the token.

Should I use HS256 or RS256?

Use HS256 when the same service signs and verifies. Use RS256 or ES256 when other services or partners must verify tokens, because they only need your public key and cannot mint tokens themselves.

What time format do iat and exp use?

Both are Unix timestamps in whole seconds since 1 January 1970 UTC. Divide JavaScript's Date.now() by 1000 and round down.

Why does my token look different each time with ES256?

ECDSA and RSA-PSS signatures include randomness, so signing the same input twice gives different signatures that are both valid. HS256 and RS256 are deterministic and give the same signature every time.

Can I sign a JWT with an RSA key in PKCS#1 format?

Web Crypto only imports PKCS#8 private keys, so convert the key with the openssl pkcs8 -topk8 command shown above. The key material is the same; only the wrapping changes.

Try it now: JWT Generator

Loading tool…