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
- The header JSON, for example
{"alg":"HS256","typ":"JWT"}, is serialized and Base64URL-encoded. Choosing an algorithm keeps the header'salgvalue in sync automatically. - The payload JSON is serialized and Base64URL-encoded the same way, as UTF-8.
- The two are joined with a dot to form the signing input:
header.payload. - HMAC is computed over that signing input with your secret, using SHA-256, SHA-384, or SHA-512.
- 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
subidentifies the user, typically a stable user ID rather than an email address.iatandexpare NumericDates in seconds. In JavaScript,Math.floor(Date.now() / 1000)gives the current value; add 3600 for a one-hour lifetime.issandaudshould match what your API validates, or the token will be rejected even with a correct signature.- Add custom claims such as
rolesorscopeto 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.