Why build a JWT by hand
Most of the time an identity provider creates your tokens. But when you test an API, a gateway rule or a library, you often need a token with exact claims: one that expired a minute ago, one with a wrong audience, one with a role your test user does not have.
Asking a real identity provider for such a token is slow or impossible. Building one yourself, signed with a test secret your service also knows, takes seconds. This tool does that with HMAC: HS256, HS384 or HS512.
How an HMAC-signed JWT is made
RFC 7515 (JSON Web Signature) defines the steps. The header and payload are serialized as JSON, encoded with Base64url, and joined with a dot. That string is the signing input. The signature is an HMAC of the signing input, keyed with the shared secret, and it is appended after a second dot.
signing_input = base64url(header_json) + "." + base64url(payload_json)
signature = HMAC-SHA256(secret, signing_input)
token = signing_input + "." + base64url(signature)Because the same secret creates and checks the signature, anyone who can verify an HS256 token can also create one. That is fine when both ends are yours, and a problem when many services verify the same tokens.
HS256, HS384 and HS512
| Algorithm | Hash | Minimum secret (RFC 7518) |
|---|---|---|
| HS256 | SHA-256 | 32 bytes |
| HS384 | SHA-384 | 48 bytes |
| HS512 | SHA-512 | 64 bytes |
RFC 7518 requires a key at least as long as the hash output. A short or guessable secret can be found offline by anyone who holds one token, so the tool warns you when yours is too short.
What this tool does
- Signs the header and payload you write with HS256, HS384 or HS512, live as you type.
- Reads the secret as text, Base64, Base64url or hex, so it matches how your service stores it.
- Sets
iatto now andexpa few minutes or an hour ahead with one click. - Warns when the secret is shorter than RFC 7518 asks for.
- Writes the JSON compactly before encoding, so the token is as short as possible.
It does not sign with RSA or EC keys, and it cannot produce a token that a Keycloak realm’s clients will accept as the realm’s own. That would need the realm’s private key, which never leaves Keycloak.
How to build a test token
- Pick HS256, HS384 or HS512. The header’s
algchanges to match. - Edit the payload. Keep the claims your service checks: usually
iss,aud,sub,expandiat. - Click the button to set
iatto now andexpa few minutes later, or type the times yourself. - Enter the same secret your service uses in its test configuration, and pick the format it is written in.
- Copy the token and send it as
Authorization: Bearer <token>.
Choosing claims for a useful test
A good test token looks like a real one in every way except the one thing you are testing. Start from a token your issuer really produced; decode it with the JWT decoder and copy its payload here.
Times
Times are Unix seconds. To test expiry handling, set exp a few seconds in the past. To test clock-skew tolerance, set nbf or iat slightly in the future. The Unix timestamp converter turns dates into seconds.
Issuer and audience
To test that your API rejects foreign tokens, change iss to another realm, or set aud to another client. A correct API rejects both even when the signature is valid.
Roles
If your API reads Keycloak-style roles, add realm_access: { roles: [...] } and resource_access: { "<client-id>": { roles: [...] } }. Removing a role is the quickest way to test a 403 path.
Where HMAC tokens are used with Keycloak
Keycloak signs access and ID tokens with RS256 by default, using the realm’s RSA key pair. Clients verify them with the public keys from /realms/<realm>/protocol/openid-connect/certs. You cannot reproduce those signatures here, and you should not need to.
HMAC does appear in Keycloak in other places. A client can authenticate to the token endpoint with “Signed JWT with Client Secret” (client_secret_jwt), which is a short JWT signed with HMAC using the client secret. The setting is on the client’s Credentials tab, under Client Authenticator.
Keycloak also keeps HMAC keys in each realm for tokens only Keycloak itself checks, such as refresh tokens. Those secrets are not shown or published, so treat such tokens as opaque.
Use cases
Unit and integration tests
Configure your service under test to accept HS256 with a test secret, then generate tokens for each case: valid, expired, wrong audience, missing role. Keep the generated tokens in fixtures, or generate them in the test with the same steps.
Checking a gateway or proxy rule
Many gateways can validate HMAC-signed JWTs with a shared secret. A hand-made token tells you quickly whether a rule reads the claim you think it reads.
Learning how JWTs work
Change one character in the payload and watch the signature change completely. Then paste the token into the signature verifier with a different secret and see it fail.
Common mistakes
- Secret format mismatch: your service may store the secret as Base64 while you typed it as text. The bytes differ, so the signature differs.
- Times in milliseconds:
Date.now()returns milliseconds. JWT times are seconds, so divide by 1000. - Pretty-printed JSON in the signing input: harmless, but the token becomes longer. This tool always signs compact JSON.
- Using
alg: noneto skip signing: a correct verifier rejects such tokens, so the test proves nothing. - Reusing a production secret for tests: anyone with the test token and the code can then forge production tokens.
Doing the same in code
For automated tests, generate tokens in code instead of copying them from a page. Every JWT library can sign with HMAC.
// Node.js, with the built-in crypto module only
const b64 = (v) => Buffer.from(JSON.stringify(v)).toString('base64url');
const input = b64({ alg: 'HS256', typ: 'JWT' }) + '.' + b64(claims);
const sig = crypto.createHmac('sha256', secret).update(input).digest('base64url');
const token = input + '.' + sig;Compare a token made in code with one made here, using the same header, claims and secret. They should be identical, which is a quick way to check your test helper.
Security notes and limits
Signing happens in your browser with Web Crypto. Nothing is sent to us. Even so, a web page is the wrong place for a production secret; use a throwaway one made for tests.
An HMAC secret should be random bytes, not a word or a phrase. Generate one with the client secret generator or the JWK generator set to a symmetric key.
Tokens made here are for testing. Production tokens should come from your identity provider.
Best practices for HMAC-signed tokens
- Use at least 32 random bytes for HS256, and more for HS384 and HS512.
- Pin the algorithm in your verifier. Never let the token’s
algdecide which algorithm to use. - Prefer RS256 or ES256 when more than one service verifies the tokens.
- Rotate secrets on a schedule, and support two at once during the change.
Keep test secrets in test configuration only, and give them names that make their purpose obvious, so nobody copies one into production by mistake.