DEV Community

Cover image for How a JSON payload is converted to a JWT string
Manan Sharma
Manan Sharma

Posted on AI-assisted

How a JSON payload is converted to a JWT string

Today I spent my time understanding JSON web tokens and how they work. But the thing I found the most interesting was how these long, encrypted strings are generated.

So, I did a deep dive, and here is what I learned:

Converting raw JSON into a long JWT string comes down to two main steps: encoding and signing.

Step 1: Base64URL Encoding (Formatting)

First, the JWT needs two separate JSON objects:

  • Header: the metadata (e.g., {"alg": "HS256", "typ": "JWT"})
  • Payload: which contains your JSON user data/claims (e.g., {"userId": 123, "role": "admin"}). Both JSON strings are converted into bytes and encoded using Base64URL.

Base64URL is a standard Base64 encoding, but modified to be URL-safe. It replaces + with -, / with _, and strips out trailing = padding characters so the string can safely sit in HTTP headers or URLs without breaking.

At this stage, we have two long strings separated by a period: encodedHeader + "." + encodedPayload

Step 2: Cryptographic Signing (Security)

To ensure no one tampers with the JSON data, a signature is created. The server takes the combined string from Step 1 (encodedHeader.encodedPayload), passes it into a cryptographic hashing function along with a secret or private key, and gets back a signature hash.

Finally, that signature is also Base64URL encoded and tacked on at the end with another dot:
encodedHeader + "." + encodedPayload + "." + encodedSignature

Let's take an example:

{
  "sub": "1234567890",
  "name": "Alex",
  "admin": true
}
Enter fullscreen mode Exit fullscreen mode
  • Header JSON {"alg":"HS256","typ":"JWT"} becomes eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.

  • Payload JSON becomes eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkFsZXgiLCJhZG1pbiI6dHJ1ZX0

  • Signature created with the secret key via HMACSHA256 becomes TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ.

  • Final JWT:
    eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkFsZXgiLCJhZG1pbiI6dHJ1ZX0.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ

Now, I thought, how about converting the payload to JWT string manually? To do that, we need to first write down the ASCII/Hex values of each character in the payload, combine them and group them into 6 bit chunks, converting these 6-bit binaries into decimal and finally, finding their corresponding character in a Base64 lookup table.

Base64 Index Table Reference:
0-25 = A-Z
26-51 = a-z
52-61 = 0-9
62 = + (or - in Base64URL)
63 = / (or _ in Base64URL)

Here is a more detailed walkthrough:

Lets take {"alg":"HS256","typ":"JWT"} as an example. Converting each character to its ASCII value (in hexadecimal and 8-bit binary):

Char Hex Binary
{ 0x7B 01111011
" 0x22 00100010
a 0x61 01100001
l 0x6C 01101100
g 0x67 01100111
" 0x22 00100010

and so on...

The next step is to combine the binaries and group them in 6-bit chunks. Let's group the first few bytes to trace the prefix eyJ:

  • Byte 1 ({): 01111011
  • Byte 2 ("): 00100010
  • Byte 3 (a): 01100001

Join them together into one 24-bit stream:
011110110010001001100001

Now split that 24-bit stream into four 6-bit chunks:

Chunk 6-Bit Binary Decimal Value Base64 Table Lookup
1 011110 30 e
2 110010 50 y
3 001001 9 J
4 100001 33 h

and so on ...

This gives us the start of our string: eyJh...
Now if we process all 26 characters (208 bits) 6 bits at a time, we get 34 full chunks (204 bits), leaving 4 bits remainder at the end. In standard Base64:

  • Two zero bits (00) are padded onto the right to complete the last 6-bit chunk.
  • Two = padding characters are added at the end so the string length is a multiple of 4.

In Base64URL (which JWT requires):

  • The zero-bit padding still happens to complete the last 6-bit byte.
  • The trailing = signs are explicitly stripped out.

That's why the encoded header ends clean without any = signs:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9

What about the encryption?

The actual HS256 (HMAC-SHA256) algorithm comes into play only for generating the 3rd part of the JWT (the signature). To trace HMAC-SHA256 manually:

  • Take the string header.payload
  • Key Padding (IPAD / OPAD): If the secret key shorter than 64 bytes, pad it with zeros. XOR the key with 0x36 (Inner Pad) and 0x5C (Outer Pad).
  • Inner Hash: Concatenate (Key XOR IPAD) + header.payload and run it through the standard SHA-256 compression function (which operates on 512-bit blocks using bitwise rotations, AND, XOR, and modular addition).
  • Outer Hash: Take the 256-bit result of Step 3, concatenate it to (Key XOR OPAD), and run it through SHA-256 again.
  • Base64URL encode the resulting 32-byte binary output to get the signature!

One last thing: Security

In general a longer hash output usually means a more secure algorithm — but not only because there are more characters. The length of the output is a result of a larger internal state space, which directly protects against two main cryptographic attacks:

  1. Collision Attacks (Birthday paradox): It happens when two completely different inputs produce the exact same hash output. Because of the Birthday Attack math, you only need to try roughly 2^(N/2) random inputs to find a collision in an N-bit hash function. So for SHA-256 (256-bit / 64 hex chars) we need around 2^(128) attempts to force a collision. Since this number is very, very huge, even if we combine all the computers, it would take them billions of years to find a collision.

  2. Preimage Resistance: If an attacker has a target hash and wants to find any input that yields that hash (brute-forcing), they have to guess through the output space. 2^(256) possible outputs for SHA-256.

That's everything I learned today about JWTs and encoding algorithms. I accept that I was too lazy to perform HMAC-SHA256 by hand. But, if you try it, let me know what you end up with (and do not use jwt.io).

Top comments (0)