📚 Chapters
JWT vs Bcrypt: Understanding Authentication & Password Security
✍️ By Arun Kumar | 11/14/2025
JWT (JSON Web Token)
===================
Purpose: Authentication and secure data transfer.
-
What it is:
JWT is a compact, URL-safe token that represents a set of claims (data) between two parties — usually the client and server. -
How it works:
-
User logs in with username/password.
-
Server verifies credentials.
-
Server creates a JWT containing user info (like user ID) and signs it using a secret key.
-
JWT is sent to the client.
-
Client sends JWT with future requests (usually in the
Authorizationheader). -
Server verifies the JWT signature to authenticate the user.
-
Structure of a JWT:
JWT has three parts, separated by dots (.):
-
Header: Info about the token type and algorithm used.
-
Payload: Data (claims) like user ID, roles, etc.
-
Signature: Created by signing the header and payload with a secret key.
Example JWT flow:
Use case:
-
Logging in users without storing sessions on the server.
-
Authorization in REST APIs.
bcrypt
=====
Purpose: Password hashing (security).
-
What it is:
bcrypt is a hashing algorithm specifically designed for storing passwords securely. -
Why not plain hash (like MD5)?
Plain hashes are fast, so attackers can brute-force them quickly. bcrypt is slow and adaptive, which makes brute-force attacks harder. -
How it works:
-
When a user sets a password, bcrypt hashes it with a salt (random data).
-
The hashed password is stored in the database.
-
When the user logs in, the entered password is hashed with the same algorithm and compared to the stored hash.
-
Example in Node.js:
Use case:
-
Safely storing passwords in a database.
-
Protecting user accounts even if the database is leaked.
Key Difference
| Feature | JWT | bcrypt |
|---|---|---|
| Type | Token for authentication | Password hashing algorithm |
| Use | Verify identity & authorize requests | Securely store passwords |
| Lifespan | Short-lived or long-lived tokens | Stored permanently (hashed passwords) |
| Security | Signed, can expire | Slow hash, resistant to brute-force |
💬 Comments
Comments (0)
No comments yet. Be the first to share your thoughts!