Skip to content
ToolkitHub

JWT Encoder

Create JWT tokens

Guide

Creating and signing a JWT with HMAC

By Logappradeep · ServiceNow developer

During development you often need a token that looks exactly like what your identity provider would issue: to test an API locally, mock a logged-in user in a frontend, reproduce a bug tied to a specific role, or write integration tests. This encoder builds a signed JWT from a header, a payload, and a shared secret using the HMAC algorithms defined in RFC 7518: HS256, HS384, and HS512. The token regenerates as you type.

How the token is built

  1. The header JSON, for example {"alg":"HS256","typ":"JWT"}, is serialized and Base64URL-encoded. Choosing an algorithm keeps the header's alg value in sync automatically.
  2. The payload JSON is serialized and Base64URL-encoded the same way, as UTF-8.
  3. The two are joined with a dot to form the signing input: header.payload.
  4. HMAC is computed over that signing input with your secret, using SHA-256, SHA-384, or SHA-512.
  5. The result is Base64URL-encoded without padding and appended after a second dot.

Change one character in the payload and the signature changes completely, which is how a receiving server detects tampering.

Choosing claims for a realistic test token

  • sub identifies the user, typically a stable user ID rather than an email address.
  • iat and exp are NumericDates in seconds. In JavaScript, Math.floor(Date.now() / 1000) gives the current value; add 3600 for a one-hour lifetime.
  • iss and aud should match what your API validates, or the token will be rejected even with a correct signature.
  • Add custom claims such as roles or scope to test authorization paths.

Choosing a strong secret

RFC 7518 requires an HMAC key at least as long as the hash output: 256 bits (32 bytes) for HS256, 384 bits for HS384, and 512 bits for HS512. A short or guessable secret is a serious weakness, because anyone holding a single token can try candidate secrets offline at very high speed until the signature matches. Generate secrets with a cryptographically secure random generator and keep them out of source control.

Gotchas

  • Secret format mismatches. By default the secret is used exactly as typed, as UTF-8 text. Many identity providers and libraries store secrets Base64-encoded and decode them before signing. If that applies, tick Secret is Base64/Base64url encoded, or the signatures will not match your server's.
  • Symmetric means shared. With HMAC, every service that can verify a token can also mint one. When many services verify tokens issued by one authority, asymmetric algorithms such as RS256 or ES256 are usually the better design; their tokens are issued with a private key that should stay on your server.
  • Test tokens only. Production tokens should be issued by your backend or identity provider, not created by hand.

Signing uses the browser's Web Crypto API, so neither the secret nor the payload leaves your device.

Frequently asked questions

Which algorithms are supported?

HS256, HS384, and HS512, which all use a shared secret. Asymmetric algorithms such as RS256 require a private key and are best used from server-side code.

How long should my secret be?

At least as long as the hash: 32 random bytes for HS256, 48 for HS384, and 64 for HS512. Short human-chosen passwords can be brute-forced from a single token.

Why does my server reject the token?

Check that the secret matches exactly, including whether your server expects it Base64-encoded, that exp is in the future and in seconds, and that iss and aud match what the server validates.

Is the payload encrypted?

No. A signed JWT payload is only encoded, so anyone with the token can read it. Do not put sensitive data in it.