Skip to content

Commit 2e46919

Browse files
committed
feat(jwt): add key derivation options and restructure docs 🔑
- Add basic/pbkdf2 key derivation methods - Add comprehensive tests and validation - Add performance reference to BENCHMARK.md - Implement PBKDF2 key derivation with 50K iterations - Improve test coverage to 98.32% - Restructure README with TOC and external references - Split architecture to ARCHITECTURE.md - Update changelog for v1.4.0
1 parent 4f9c50b commit 2e46919

File tree

16 files changed

+343
-200
lines changed

16 files changed

+343
-200
lines changed

ARCHITECTURE.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
## 🏗️ Architecture
2+
3+
### 🔄 Complete Data Flow
4+
5+
> This diagram shows the complete JWT workflow from data input to output. The **Sign** process creates encrypted tokens and stores them in cache, while **Verify** operations check cache first for 1,600x performance boost. **Decode** extracts raw data without verification, and **Cache Management** ensures memory efficiency with LRU eviction and TTL expiration.
6+
7+
```mermaid
8+
graph TD
9+
A[User Data] --> B[Sign Request]
10+
B --> C[Create Payload]
11+
C --> D[Multi Algorithm Encryption]
12+
D --> E[Generate Token]
13+
E --> F[Store in Cache]
14+
F --> G[Return Token]
15+
16+
H[Token Verification] --> I{Cache Hit?}
17+
I -->|Yes| J[Return Cached Data]
18+
I -->|No| K[Decrypt Token]
19+
K --> L[Validate Integrity]
20+
L --> M[Store in Cache]
21+
M --> N[Return Decoded Data]
22+
23+
O[Token Decode] --> P[Extract Payload]
24+
P --> Q[Return Raw Data]
25+
26+
R[Cache Management] --> S[LRU Eviction]
27+
S --> T[TTL Expiration]
28+
T --> U[Memory Limit 10K]
29+
30+
style A fill:#e1f5fe,color:#000
31+
style G fill:#c8e6c9,color:#000
32+
style J fill:#c8e6c9,color:#000
33+
style N fill:#c8e6c9,color:#000
34+
style Q fill:#c8e6c9,color:#000
35+
style I fill:#fff9c4,color:#000
36+
style F fill:#e8f5e8,color:#000
37+
style M fill:#e8f5e8,color:#000
38+
style R fill:#f3e5f5,color:#000
39+
```
40+
41+
---
42+
43+
### 🔐 JWT Encoding Process
44+
45+
> This diagram details the token creation process with **multi-algorithm encryption**. Each token gets a **random IV** for uniqueness, **version-based AAD** for compatibility, and **authentication tags** for tamper detection. The **caching system** stores encrypted tokens for instant retrieval, providing massive performance improvements for repeated verifications.
46+
47+
```mermaid
48+
graph TD
49+
A[User Data] --> B[Create Payload]
50+
B --> C[Add Timestamps & Version]
51+
C --> D[JSON Stringify Payload]
52+
D --> E[Generate Random IV]
53+
E --> F[Multi Algorithm Encryption]
54+
F --> G[Create Token Structure]
55+
G --> H[Base64 Encode]
56+
H --> I[Secure JWT Token]
57+
I --> J[Store in Cache]
58+
J --> K[LRU Eviction Check]
59+
K --> L[TTL Expiration]
60+
61+
M[Secret Key] --> N[Key Preparation]
62+
N --> F
63+
O[Random Salt] --> N
64+
65+
P[Version] --> Q[Additional
66+
Authenticated Data]
67+
Q --> F
68+
69+
F --> R[Authentication Tag]
70+
R --> G
71+
72+
S[Cache Hit?] --> T[Return Cached Data]
73+
S --> U[Decrypt & Cache]
74+
U --> V[Return Decrypted Data]
75+
76+
style A fill:#e1f5fe,color:#000
77+
style I fill:#c8e6c9,color:#000
78+
style F fill:#fff3e0,color:#000
79+
style M fill:#fce4ec,color:#000
80+
style J fill:#e8f5e8,color:#000
81+
style S fill:#fff9c4,color:#000
82+
```
83+
84+
---
85+
86+
### 🛡️ Security Layers
87+
88+
> This diagram illustrates the security-focused verification process. **Cache validation** provides the first security layer, preventing DoS attacks through performance optimization. **Decryption** uses the secret key and random IV, while **integrity checks** verify authentication tags. The **caching system** acts as both a performance and security feature, ensuring fast and secure token validation.
89+
90+
```mermaid
91+
graph LR
92+
A[Input Token] --> B[Cache Validation]
93+
B --> C[Validation Layer]
94+
C --> D[Decryption Layer]
95+
D --> E[Integrity Check]
96+
E --> F[Decoded Data]
97+
98+
B --> G[Cache Hit]
99+
G --> F
100+
101+
H[Cache Miss] --> C
102+
C --> I[Store in Cache]
103+
I --> F
104+
105+
J[Secret Key] --> D
106+
K[Random IV] --> D
107+
L[Version AAD] --> D
108+
M[Auth Tag] --> E
109+
110+
style B fill:#e8f5e8,color:#000
111+
style C fill:#ffebee,color:#000
112+
style D fill:#fff3e0,color:#000
113+
style E fill:#f3e5f5,color:#000
114+
style F fill:#e1f5fe,color:#000
115+
style G fill:#c8e6c9,color:#000
116+
style I fill:#fff9c4,color:#000
117+
```

CHANGELOG.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,33 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
---
99

10+
## [1.4.0] - 2025-09-06
11+
12+
### Added
13+
- **Key derivation options**: Choose between `basic` (fast) and `pbkdf2` (secure) key generation methods
14+
- **PBKDF2 key derivation**: 50K iterations with SHA-256 for enterprise-grade security
15+
- **Basic key derivation**: Fast salt + secret concatenation for high-performance applications
16+
- **Key derivation validation**: Validation for key derivation method selection
17+
- **Key derivation documentation**: Updated README with Key Derivation Options table
18+
- **Architecture documentation**: Separate ARCHITECTURE.md with detailed diagrams
19+
- **External references**: Links to ARCHITECTURE.md and BENCHMARK.md
20+
21+
### Enhanced
22+
- **Security flexibility**: Configurable key derivation methods based on use case requirements
23+
- **Performance options**: Basic key derivation for speed, PBKDF2 for maximum security
24+
- **API consistency**: New `keyDerivation` option follows existing constructor pattern
25+
- **Documentation**: Enhanced README with clear key derivation guidance and examples
26+
- **Test coverage**: Improved from 98.28% to 98.32% with key derivation tests
27+
- **Documentation structure**: Cleaner README with better navigation and external references
28+
29+
### Technical
30+
- **Algorithm abstraction**: Extended `Algorithms` class with key derivation methods
31+
- **Type safety**: Added `KeyDerivationAlgo` type for compile-time validation
32+
- **Error handling**: Error messages for invalid key derivation methods
33+
- **Backward compatibility**: Default `basic` key derivation maintains existing behavior
34+
35+
---
36+
1037
## [1.3.0] - 2025-09-06
1138

1239
### Added

README.md

Lines changed: 18 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
![npm version](https://img.shields.io/npm/v/@neabyte/secure-jwt)
44
![node version](https://img.shields.io/node/v/@neabyte/secure-jwt)
55
![typescript version](https://img.shields.io/badge/typeScript-5.9.2-blue.svg)
6-
![coverage](https://img.shields.io/badge/coverage-98.28%25-brightgreen)
6+
![coverage](https://img.shields.io/badge/coverage-98.32%25-brightgreen)
77
![license](https://img.shields.io/npm/l/@neabyte/secure-jwt.svg)
88

99
A secure JWT implementation with **AES-256-GCM** & **ChaCha20-Poly1305** algorithms for Node.js applications.
@@ -12,6 +12,7 @@ A secure JWT implementation with **AES-256-GCM** & **ChaCha20-Poly1305** algorit
1212

1313
- 🔒 **Multi algorithms** - AES-256-GCM & ChaCha20-Poly1305
1414
- ⚙️ **Algorithm selection** - Choose the best encryption for your use case
15+
- 🔑 **Key derivation options** - Basic (fast) or PBKDF2 (secure) key generation
1516
- 🛡️ **Tamper detection** - Authentication tags prevent modification
1617
-**Automatic expiration** - Built-in token lifecycle management
1718
- 🔄 **Version compatibility** - Prevents downgrade attacks
@@ -110,7 +111,8 @@ const arrayToken: string = jwt.sign([1, 2, 3])
110111

111112
```javascript
112113
const jwt = new SecureJWT({
113-
algorithm: 'aes-256-gcm', // Optional: default: 'aes-256-gcm'
114+
algorithm: 'aes-256-gcm', // Optional: Default 'aes-256-gcm'
115+
keyDerivation: 'basic', // Optional: Default 'basic'
114116
secret: 'your-secret-key', // Required: 8-255 characters
115117
expireIn: '1h', // Required: Time string
116118
version: '1.0.0', // Optional: Default '1.0.0'
@@ -120,29 +122,19 @@ const jwt = new SecureJWT({
120122

121123
### 🔧 Algorithm Options
122124

123-
Choose the encryption algorithm that best fits your needs:
125+
| Value | Description |
126+
|-------|-------------|
127+
| `aes-256-gcm` | Hardware accelerated, industry standard, perfect for general purpose and enterprise applications |
128+
| `chacha20-poly1305` | Software optimized, 2-3x faster than AES, ideal for high-throughput and mobile applications |
124129

125-
```javascript
126-
// AES-256-GCM (default) - Hardware accelerated, industry standard
127-
const jwtAES = new SecureJWT({
128-
algorithm: 'aes-256-gcm',
129-
secret: 'key',
130-
expireIn: '1h'
131-
})
130+
### 🔑 Key Derivation Options
132131

133-
// ChaCha20-Poly1305 - Maximum performance, 2-3x faster
134-
const jwtChaCha = new SecureJWT({
135-
algorithm: 'chacha20-poly1305',
136-
secret: 'key',
137-
expireIn: '1h'
138-
})
139-
```
132+
| Value | Description |
133+
|-------|-------------|
134+
| `basic` | Fast salt + secret concatenation, perfect for high-performance applications |
135+
| `pbkdf2` | Secure 50K iterations with SHA-256, ideal for enterprise and high-security applications |
140136

141-
**Algorithm Comparison:**
142-
- **AES-256-GCM**: Hardware accelerated, widely supported, industry standard
143-
- **ChaCha20-Poly1305**: Software optimized, 2-3x faster, perfect for high-throughput applications
144-
145-
### Time Format
137+
### ⏰ Time Format
146138

147139
```javascript
148140
// Supported formats
@@ -164,12 +156,10 @@ const jwtChaCha = new SecureJWT({
164156
```javascript
165157
new SecureJWT(options: JWTOptions)
166158
```
167-
**Available Algorithm:**
168-
- `aes-256-gcm`
169-
- `chacha20-poly1305`
170159

171160
**Options:**
172161
- `algorithm?:` - Encryption algorithm (default: 'aes-256-gcm')
162+
- `keyDerivation?:` - Key derivation method (default: 'basic')
173163
- `secret: string` - Secret key (8-255 chars, required for security)
174164
- `expireIn: string` - Token expiration time (required for security)
175165
- `version?: string` - Token version (default: '1.0.0')
@@ -218,122 +208,10 @@ try {
218208
---
219209

220210
## 🏗️ Architecture
211+
For detailed architecture diagrams and technical implementation details, see [ARCHITECTURE.md](ARCHITECTURE.md).
221212

222-
### 🔄 Complete Data Flow
223-
224-
> This diagram shows the complete JWT workflow from data input to output. The **Sign** process creates encrypted tokens and stores them in cache, while **Verify** operations check cache first for 1,600x performance boost. **Decode** extracts raw data without verification, and **Cache Management** ensures memory efficiency with LRU eviction and TTL expiration.
225-
226-
```mermaid
227-
graph TD
228-
A[User Data] --> B[Sign Request]
229-
B --> C[Create Payload]
230-
C --> D[Multi Algorithm Encryption]
231-
D --> E[Generate Token]
232-
E --> F[Store in Cache]
233-
F --> G[Return Token]
234-
235-
H[Token Verification] --> I{Cache Hit?}
236-
I -->|Yes| J[Return Cached Data]
237-
I -->|No| K[Decrypt Token]
238-
K --> L[Validate Integrity]
239-
L --> M[Store in Cache]
240-
M --> N[Return Decoded Data]
241-
242-
O[Token Decode] --> P[Extract Payload]
243-
P --> Q[Return Raw Data]
244-
245-
R[Cache Management] --> S[LRU Eviction]
246-
S --> T[TTL Expiration]
247-
T --> U[Memory Limit 10K]
248-
249-
style A fill:#e1f5fe,color:#000
250-
style G fill:#c8e6c9,color:#000
251-
style J fill:#c8e6c9,color:#000
252-
style N fill:#c8e6c9,color:#000
253-
style Q fill:#c8e6c9,color:#000
254-
style I fill:#fff9c4,color:#000
255-
style F fill:#e8f5e8,color:#000
256-
style M fill:#e8f5e8,color:#000
257-
style R fill:#f3e5f5,color:#000
258-
```
259-
260-
---
261-
262-
### 🔐 JWT Encoding Process
263-
264-
> This diagram details the token creation process with **multi-algorithm encryption**. Each token gets a **random IV** for uniqueness, **version-based AAD** for compatibility, and **authentication tags** for tamper detection. The **caching system** stores encrypted tokens for instant retrieval, providing massive performance improvements for repeated verifications.
265-
266-
```mermaid
267-
graph TD
268-
A[User Data] --> B[Create Payload]
269-
B --> C[Add Timestamps & Version]
270-
C --> D[JSON Stringify Payload]
271-
D --> E[Generate Random IV]
272-
E --> F[Multi Algorithm Encryption]
273-
F --> G[Create Token Structure]
274-
G --> H[Base64 Encode]
275-
H --> I[Secure JWT Token]
276-
I --> J[Store in Cache]
277-
J --> K[LRU Eviction Check]
278-
K --> L[TTL Expiration]
279-
280-
M[Secret Key] --> N[Key Preparation]
281-
N --> F
282-
O[Random Salt] --> N
283-
284-
P[Version] --> Q[Additional
285-
Authenticated Data]
286-
Q --> F
287-
288-
F --> R[Authentication Tag]
289-
R --> G
290-
291-
S[Cache Hit?] --> T[Return Cached Data]
292-
S --> U[Decrypt & Cache]
293-
U --> V[Return Decrypted Data]
294-
295-
style A fill:#e1f5fe,color:#000
296-
style I fill:#c8e6c9,color:#000
297-
style F fill:#fff3e0,color:#000
298-
style M fill:#fce4ec,color:#000
299-
style J fill:#e8f5e8,color:#000
300-
style S fill:#fff9c4,color:#000
301-
```
302-
303-
---
304-
305-
### 🛡️ Security Layers
306-
307-
> This diagram illustrates the security-focused verification process. **Cache validation** provides the first security layer, preventing DoS attacks through performance optimization. **Decryption** uses the secret key and random IV, while **integrity checks** verify authentication tags. The **caching system** acts as both a performance and security feature, ensuring fast and secure token validation.
308-
309-
```mermaid
310-
graph LR
311-
A[Input Token] --> B[Cache Validation]
312-
B --> C[Validation Layer]
313-
C --> D[Decryption Layer]
314-
D --> E[Integrity Check]
315-
E --> F[Decoded Data]
316-
317-
B --> G[Cache Hit]
318-
G --> F
319-
320-
H[Cache Miss] --> C
321-
C --> I[Store in Cache]
322-
I --> F
323-
324-
J[Secret Key] --> D
325-
K[Random IV] --> D
326-
L[Version AAD] --> D
327-
M[Auth Tag] --> E
328-
329-
style B fill:#e8f5e8,color:#000
330-
style C fill:#ffebee,color:#000
331-
style D fill:#fff3e0,color:#000
332-
style E fill:#f3e5f5,color:#000
333-
style F fill:#e1f5fe,color:#000
334-
style G fill:#c8e6c9,color:#000
335-
style I fill:#fff9c4,color:#000
336-
```
213+
## ⚡ Performance
214+
For detailed benchmark results and performance metrics, see [BENCHMARK.md](BENCHMARK.md).
337215

338216
---
339217

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@neabyte/secure-jwt",
33
"description": "Secure JWT with AES-256-GCM & ChaCha20-Poly1305 encryption, built-in caching, tamper detection, and TypeScript support",
4-
"version": "1.3.0",
4+
"version": "1.4.0",
55
"type": "module",
66
"main": "./dist/index.js",
77
"types": "./dist/index.d.ts",

src/algorithms/AES256.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { TokenEncrypted, EncryptionAlgo, IEncryptionAlgo } from '@interface
44

55
/**
66
* AES-256-GCM encryption class
7-
* Provides encryption and decryption using AES-256-GCM algorithm
7+
* Handles encryption and decryption using AES-256-GCM algorithm
88
*/
99
export default class AES256 implements IEncryptionAlgo {
1010
/** Algorithm name constant */

src/algorithms/ChaCha20.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { TokenEncrypted, EncryptionAlgo, IEncryptionAlgo } from '@interface
44

55
/**
66
* ChaCha20-Poly1305 encryption class
7-
* Provides encryption and decryption using ChaCha20-Poly1305 algorithm
7+
* Handles encryption and decryption using ChaCha20-Poly1305 algorithm
88
*/
99
export default class ChaCha20 implements IEncryptionAlgo {
1010
/** Algorithm name constant */

0 commit comments

Comments
 (0)