Generating a standards-compliant JWT in JWS Compact Serialization (RFC 7515 Section 3.1) transforms a JOSE header, a JSON claims set, and a symmetric secret key into a three-part string formatted as <header>.<payload>.<signature>.
1. Construct the JOSE Header and API Claims Payload
Start with an HS256 protected header and a JSON payload combining RFC 7519 registered claims (iss, sub, aud, iat, exp) with custom application claims (role):
{
"alg": "HS256",
"typ": "JWT"
}
{
"iss": "https://auth.example.com",
"sub": "usr_9481",
"aud": "api.example.com",
"iat": 1767225600,
"exp": 1767229200,
"role": "admin"
}
Here 1767225600 represents 2026-01-01T00:00:00Z and 1767229200 represents 2026-01-01T01:00:00Z (a 1-hour token lifetime).
2. Build the Unpadded Base64URL Signing Input
Each JSON object is minified to UTF-8 bytes without extra whitespace, encoded with Base64URL (- and _ alphabet, = padding stripped), and joined with an ASCII period (.):
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2F1dGguZXhhbXBsZS5jb20iLCJzdWIiOiJ1c3JfOTQ4MSIsImF1ZCI6ImFwaS5leGFtcGxlLmNvbSIsImlhdCI6MTc2NzIyNTYwMCwiZXhwIjoxNzY3MjI5MjAwLCJyb2xlIjoiYWRtaW4ifQ
3. Reproducible HMAC-SHA256 Signing Vector
Web Crypto imports your secret bytes via crypto.subtle.importKey("raw", secretBytes, { name: "HMAC", hash: "SHA-256" }, false, ["sign"]) and signs the exact ASCII <base64url-header>.<base64url-payload> bytes. You can verify deterministic parity across OpenSSL, Node.js crypto, Python PyJWT, and this generator using the canonical HS256 test vector with UTF-8 secret your-256-bit-secret:
{
"sub": "1234567890",
"name": "John Doe",
"iat": 1516239022
}
Signing the {"alg":"HS256","typ":"JWT"} header and the payload above with your-256-bit-secret always yields this exact 147-character token, ready for an HTTP Authorization: Bearer <token> header or round-trip verification in JWT Decoder & Inspector:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c