How to Create a JWT: Signing with HS256 Step by Step
Last updated: · 5 min read
A JWT (JSON Web Token) is a signed token made of three dot-separated parts: a header, a payload and a signature. RFC 7519 defines the format, and RFC 7515 (JWS) defines how the signature is computed. APIs use JWTs widely to carry the identity and permissions of an authenticated user. This guide walks through signing a JSON payload with HS256 and explains base64url encoding, the exp and iat claims, the difference between HS256 and RS256 and how strong the secret needs to be. The tokens in the example are real output from the JWT Generator; they are created in your browser with the Web Crypto API and the secret is never sent to a server.
Formula
header = {"alg":"HS256","typ":"JWT"}
signing input = base64url(UTF-8(header)) + "." + base64url(UTF-8(payload))
signature = HMAC-SHA-256(secret, ASCII(signing input))
JWT = signing input + "." + base64url(signature)
base64url: Base64 with - instead of + and _ instead of /, trailing = padding removed
HS256 → HMAC-SHA-256 (43-character signature), HS384 → HMAC-SHA-384 (64), HS512 → HMAC-SHA-512 (86)The header states the token type and the signing algorithm. The payload is a JSON object holding the claims. Both are base64url-encoded and joined with a dot; RFC 7515 calls the resulting string the JWS Signing Input. The signature is the HMAC of that string under the secret. The tool parses the payload as JSON and writes it out again without whitespace, so an indented payload and a one-line payload give the same token. The secret is used as the UTF-8 bytes of the characters you type.
What the inputs mean
- Payload (JSON)
- A JSON object with the claims the token should carry, for example {"sub":"42","exp":1700003600}. Arrays, numbers and bare strings are rejected, and keys must be in double quotes.
- Secret
- The shared key that creates and verifies the signature. It is read as plain text; a Base64-encoded secret is not decoded. It cannot be empty.
- Algorithm
- HS256 (default), HS384 or HS512. The choice is written to the alg header and determines the signature length. Public-key algorithms such as RS256 and ES256 are not available in this tool.
Worked example: Signing the sample payload with HS256
- Input: Payload = {"sub":"1234567890","name":"John Doe","iat":1516239022}, Secret = your-256-bit-secret, Algorithm = HS256.
- The header is written as {"alg":"HS256","typ":"JWT"} and base64url-encoded: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9. JSON objects start with {" and that is why tokens always begin with eyJ.
- The payload is re-serialised without whitespace and encoded: eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.
- The two parts are joined with a dot and the HMAC-SHA-256 of that string is computed with the secret. The 32-byte result becomes 43 base64url characters: SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c.
- The first line of the output is the token itself: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
- Below it come three lines for checking: Header: {"alg":"HS256","typ":"JWT"}, Payload: {"sub":"1234567890","name":"John Doe","iat":1516239022} and Signing algorithm: HMAC-SHA-256.
- Switch to HS512: the header part becomes eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9 and the signature grows to 86 characters: pazba9Pj009HgANP4pTCQAHpXNU7pVbjIGff_plktSzsa9rXTGzFngaawzXGEO6Q0Hx5dtGi-dMDlIadV81o3Q. The payload part stays the same.
- For a token that expires, add exp to the payload: {"sub":"42","role":"admin","iat":1700000000,"exp":1700003600}. Here iat is 14 November 2023, 22:13:20 UTC and exp is 3600 seconds (one hour) later. The HS256 signature of this payload is TzYAVkNwibCBwoBRRl1OPnClL28MdnOf_zlRm9z5oJQ.
Result: Resulting token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c (Signing algorithm: HMAC-SHA-256).
🎫 JWT Generator (HS256/384/512)
Free, no sign-up; your data stays in your browser.
Open the calculator →How to read the result
A JWT of this kind is signed, not encrypted: anyone who obtains the token can base64url-decode the header and payload and read them. The signature only shows that the content has not been changed and that it was produced by someone who knows the key. Passwords, national ID numbers, card details and API keys therefore do not belong in a payload. HS256 is symmetric, so every service able to verify a token can also mint one. When several services need to verify tokens, RS256 or ES256 is the better fit, because only the issuer holds the private key. RFC 7518 requires a key of at least 256 bits (32 bytes) for HS256; despite its name, the sample secret your-256-bit-secret is 19 characters, only 152 bits, and must not be used in a real system. The tool never transmits the secret, but make it a habit not to type production secrets into any web page and use this tool for test tokens.
Common mistakes
- Putting secrets in the payload. The payload is encoded, not encrypted, and anyone can read it.
- Using a short or guessable secret. An attacker with a single token can brute-force the key offline; RFC 8725 says human-memorable passwords must not be used directly as HMAC keys.
- Writing exp and iat in milliseconds. JWT time claims count seconds since 1970; in JavaScript, divide Date.now() by 1000 and round down.
- Leaving out exp. A token without an expiry stays valid after a leak until the key is rotated; give access tokens a short lifetime.
- Trusting the alg header on the server. The verifier must pin the algorithms it accepts, otherwise alg: none or algorithm-confusion attacks become possible.
- Expecting a Base64 secret to be decoded. Some libraries Base64-decode the secret before use; this tool takes the text literally, so signatures may not match such a library.
- Writing large numeric IDs as numbers. Integers above 2⁵³ lose precision when JSON is parsed; put long identifiers in quotes as strings.
Frequently asked questions
Is a JWT encrypted?
No. Common JWTs are JWS tokens: signed, with readable content. If the content must be hidden, the five-part JWE format is used; this tool does not create JWEs.
What is the difference between HS256 and RS256?
HS256 computes an HMAC-SHA-256 with one shared secret that both issuer and verifier know. RS256 signs with an RSA private key and is verified with the public key, so a verifier cannot issue tokens. HS256 is fine inside a single application; when several services or third parties verify tokens, prefer RS256 or ES256.
Are exp, iat and nbf required?
RFC 7519 makes none of the registered claims mandatory; the application decides which ones it needs. In practice it is good hygiene to set exp (expiry), iat (issued at) and, where needed, nbf (not before), all in seconds.
How long should an HS256 secret be?
RFC 7518 requires at least 256 bits (32 bytes) for HS256, 384 bits for HS384 and 512 bits for HS512. Generate it with a cryptographically secure random source; openssl rand -base64 32, for instance, produces 32 random bytes.
Why does another tool give a different token for the same payload and secret?
The signature depends on the exact bytes of the encoded header and payload. If another tool orders the header fields differently, keeps the whitespace in the payload or Base64-decodes the secret, the token differs, and both can still be valid.
Is my secret sent to a server?
No. The token is created in your browser with the Web Crypto API. Even so, keeping production secrets out of online tools is the safest habit.
The result is not exact or binding and is for information only; it is not an official document and cannot be used in official procedures. Confirm it with the competent authority or a professional before acting; no liability is accepted for losses arising from its use.