Saturday 5th December 2026
Ho Chi Minh, Vietnam

1. The need of JWT signatures

JWT (JSON Web Token) is a compact, URL-safe means of representing claims to be transferred between two parties (we can call them are client and server). In today’s landscape, with the advancement of microservices, there can be multiple services on the server side, an identity service issues JWT and other services use this JWT to perform authentication/authorization.

A JWT consists of three parts:

  1. Header: Specifies the token type (JWT) and the signing algorithm (e.g., HS256).
  2. Payload: Contains the claims or assertions about an entity (e.g., user data).
  3. Signature: Verifies the token’s integrity and authenticity.

When a JWT is created, it is signed. But why do we need this signature? Let’s consider an example about a file management system with two services: Identity Service – handle user login and issue JWT token, File Management Service – perform all operations about files (read, write, delete, share) authorized based on the JWT sent by client.

We use a “permissions” claim in JWT payload to store user’s permission. Let’s have a look at a sample JWT which is decoded by jwt.io.

The payload are encoded so they can be created easily, the hacker can easily add “share” permission into this claim then create another JWT token to send malicious requests. So we need a mechanism overcome this scenario, it is signature.

2. How does JWT signature work?

When creating a JWT, the signature is then created by hashing header and payload with a secret key using a specified algorithm e.g. HS256.

When verifying a JWT, the server will also create a signature from header and payload then compare with the signature in the JWT, the JWT is verified only these signature are the same. This solution ensures that the content of the JWT (the header and payload) has not been tampered with.

If any part of the token is altered after it has been signed, the signature will not match when the token is verified (with our file management sample above, the file management service will reject the JWT which was modified to add “share” permission into).

3. RS256 and HS256 algorithms

RS256 and HS256 are commonly used algorithms for signing and verifying JSON Web Tokens (JWTs). They serve different purposes depending on the use case and security requirements.

HS256 (HMAC with SHA-256) is a symmetric algorithm, meaning it uses the same secret key for both signing the token and verifying its signature. This approach requires the key to be securely shared between the parties involved, which can be a potential security risk if not managed properly.

On the other hand, RS256 (RSA with SHA-256) is an asymmetric algorithm that uses a pair of keys: a private key for signing the token and a public key for verifying it. The private key remains confidential, while the public key can be distributed openly. This method is more secure in scenarios where the verifier and the signer are different entities, as the public key can be shared without compromising the security of the private key.

We will create a Java application to demonstrate how they work using jjwt library. This application uses the hardcore keys but in real work, we must store them in environment variables or configuration files. You can find the source code of this application including unit tests here.

3.1. HS256

3.2. RS256

4. Conclusion

In conclusion, understanding JWT signing algorithms is essential for secure authentication in web applications. HS256, a symmetric algorithm, uses a single secret key for both signing and verification, making it suitable for scenarios where the token issuer and verifier are the same entity. In contrast, RS256, an asymmetric algorithm, uses a private key for signing and a public key for verification, ideal for distributed systems where multiple parties need to verify tokens.

Choosing the right algorithm depends on your application’s architecture and security requirements, ensuring the integrity and authenticity of your tokens.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top