@syldel/crypto-utils 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +69 -6
- package/dist/aes-gcm.util.d.ts +7 -0
- package/dist/aes-gcm.util.js +33 -16
- package/dist/jwt.util.d.ts +6 -0
- package/dist/jwt.util.js +6 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,8 +1,75 @@
|
|
|
1
1
|
# @syldel/crypto-utils
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A robust, high-performance cryptographic toolkit for Node.js and NestJS applications. This package prioritizes security by leveraging Node's native crypto module (OpenSSL) for maximum protection of sensitive data and private keys.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## 🚀 Key Features
|
|
6
|
+
- **Native Security**: Built on top of Node.js crypto (OpenSSL) for hardware-accelerated encryption.
|
|
7
|
+
|
|
8
|
+
- **AES-256-GCM**: Industry-standard authenticated encryption (AEAD) to ensure both confidentiality and data integrity.
|
|
9
|
+
|
|
10
|
+
- **Lightweight JWT**: Pure implementation for signing and verifying tokens without heavy dependencies.
|
|
11
|
+
|
|
12
|
+
- **Standardized Error Handling**: Harmonized error messages ("Decryption failed") for better security and predictable testing.
|
|
13
|
+
|
|
14
|
+
## 🛠 Usage
|
|
15
|
+
|
|
16
|
+
### AES-256-GCM (Authenticated Encryption)
|
|
17
|
+
Designed for high-security environments. It automatically handles Initialization Vectors (IV) and Authentication Tags.
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { AesGcmUtil } from '@syldel/crypto-utils';
|
|
21
|
+
|
|
22
|
+
const keyHex = "0123456789abcdef..."; // 64 hex characters (32 bytes)
|
|
23
|
+
const message = "Sensitive data";
|
|
24
|
+
|
|
25
|
+
// Encrypt
|
|
26
|
+
const encrypted = AesGcmUtil.encrypt(message, keyHex);
|
|
27
|
+
// Returns: { data: '...', iv: '...', tag: '...' }
|
|
28
|
+
|
|
29
|
+
// Decrypt
|
|
30
|
+
try {
|
|
31
|
+
const original = AesGcmUtil.decrypt(encrypted, keyHex);
|
|
32
|
+
} catch (error) {
|
|
33
|
+
// Throws "Decryption failed" if data is tampered or key is wrong
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Pure JWT Utilities
|
|
38
|
+
A lightweight way to handle JWTs in your NestJS guards or services.
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import { PureJwtUtil } from '@syldel/crypto-utils';
|
|
42
|
+
|
|
43
|
+
const secret = "your-secure-secret";
|
|
44
|
+
const payload = { sub: "user_123", role: "admin" };
|
|
45
|
+
|
|
46
|
+
// Sign a token
|
|
47
|
+
const token = PureJwtUtil.sign(payload, secret);
|
|
48
|
+
|
|
49
|
+
// Verify and decode
|
|
50
|
+
try {
|
|
51
|
+
const decoded = PureJwtUtil.verify<MyUserType>(token, secret);
|
|
52
|
+
} catch (err) {
|
|
53
|
+
// Handles "Token expired" or "Invalid signature"
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## 🔒 Security & Performance
|
|
58
|
+
|
|
59
|
+
- **Zero External Crypto Libs**: By using native Node.js APIs, this package minimizes the attack surface and dependency supply chain risks.
|
|
60
|
+
|
|
61
|
+
- **Side-Channel Protection**: Leverages OpenSSL's C++ implementation to protect against timing attacks.
|
|
62
|
+
|
|
63
|
+
- **GCM Integrity**: Unlike AES-CTR or CBC, AES-GCM ensures that any single-bit modification to the encrypted data will be detected during decryption.
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
## 🧪 Testing
|
|
67
|
+
|
|
68
|
+
Full test suite with 100% coverage on core crypto logic.
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
npm run test
|
|
72
|
+
```
|
|
6
73
|
|
|
7
74
|
## 👨💻 Développement
|
|
8
75
|
|
|
@@ -24,8 +91,6 @@ Le projet utilise **ESLint** pour la logique et **Prettier** pour le formatage.
|
|
|
24
91
|
npx eslint .
|
|
25
92
|
```
|
|
26
93
|
|
|
27
|
-
---
|
|
28
|
-
|
|
29
94
|
## 🔗 Utilisation en local (Development Workflow)
|
|
30
95
|
|
|
31
96
|
Pour utiliser ce package dans tes autres projets sans le publier sur NPM :
|
|
@@ -39,8 +104,6 @@ Pour utiliser ce package dans tes autres projets sans le publier sur NPM :
|
|
|
39
104
|
npm link @syldel/crypto-utils
|
|
40
105
|
```
|
|
41
106
|
|
|
42
|
-
---
|
|
43
|
-
|
|
44
107
|
## 📦 Publication
|
|
45
108
|
|
|
46
109
|
Le projet utilise des **Granular Access Tokens** pour la publication afin de contourner la double authentification (2FA) manuelle tout en maintenant une sécurité maximale.
|
package/dist/aes-gcm.util.d.ts
CHANGED
|
@@ -4,7 +4,14 @@ export interface EncryptedData {
|
|
|
4
4
|
tag: string;
|
|
5
5
|
}
|
|
6
6
|
export declare class AesGcmUtil {
|
|
7
|
+
private static readonly ALGORITHM;
|
|
7
8
|
private static validateKey;
|
|
9
|
+
/**
|
|
10
|
+
* Encrypts text using native Node.js crypto (AES-256-GCM)
|
|
11
|
+
*/
|
|
8
12
|
static encrypt(text: string, key: Buffer | string): EncryptedData;
|
|
13
|
+
/**
|
|
14
|
+
* Decrypts a payload using native Node.js crypto (AES-256-GCM)
|
|
15
|
+
*/
|
|
9
16
|
static decrypt(payload: EncryptedData, key: Buffer | string): string;
|
|
10
17
|
}
|
package/dist/aes-gcm.util.js
CHANGED
|
@@ -43,25 +43,42 @@ class AesGcmUtil {
|
|
|
43
43
|
}
|
|
44
44
|
return encryptionKey;
|
|
45
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* Encrypts text using native Node.js crypto (AES-256-GCM)
|
|
48
|
+
*/
|
|
46
49
|
static encrypt(text, key) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
50
|
+
try {
|
|
51
|
+
const encryptionKey = this.validateKey(key);
|
|
52
|
+
const iv = crypto.randomBytes(12);
|
|
53
|
+
const cipher = crypto.createCipheriv(this.ALGORITHM, encryptionKey, iv);
|
|
54
|
+
let encrypted = cipher.update(text, 'utf8', 'hex');
|
|
55
|
+
encrypted += cipher.final('hex');
|
|
56
|
+
return {
|
|
57
|
+
data: encrypted,
|
|
58
|
+
iv: iv.toString('hex'),
|
|
59
|
+
tag: cipher.getAuthTag().toString('hex'),
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
throw new Error('Encryption failed');
|
|
64
|
+
}
|
|
57
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* Decrypts a payload using native Node.js crypto (AES-256-GCM)
|
|
68
|
+
*/
|
|
58
69
|
static decrypt(payload, key) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
70
|
+
try {
|
|
71
|
+
const encryptionKey = this.validateKey(key);
|
|
72
|
+
const decipher = crypto.createDecipheriv(this.ALGORITHM, encryptionKey, Buffer.from(payload.iv, 'hex'));
|
|
73
|
+
decipher.setAuthTag(Buffer.from(payload.tag, 'hex'));
|
|
74
|
+
let decrypted = decipher.update(payload.data, 'hex', 'utf8');
|
|
75
|
+
decrypted += decipher.final('utf8');
|
|
76
|
+
return decrypted;
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
throw new Error('Decryption failed');
|
|
80
|
+
}
|
|
65
81
|
}
|
|
66
82
|
}
|
|
67
83
|
exports.AesGcmUtil = AesGcmUtil;
|
|
84
|
+
AesGcmUtil.ALGORITHM = 'aes-256-gcm';
|
package/dist/jwt.util.d.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
export declare class PureJwtUtil {
|
|
2
|
+
/**
|
|
3
|
+
* Verifies a JWT's signature and expiration, then returns the decoded payload.
|
|
4
|
+
*/
|
|
2
5
|
static verify<T extends object>(token: string, secret: string): T;
|
|
6
|
+
/**
|
|
7
|
+
* Signs a payload and returns a compact JWT string.
|
|
8
|
+
*/
|
|
3
9
|
static sign(payload: Record<string, any>, secret: string): string;
|
|
4
10
|
}
|
package/dist/jwt.util.js
CHANGED
|
@@ -36,6 +36,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
36
36
|
exports.PureJwtUtil = void 0;
|
|
37
37
|
const crypto = __importStar(require("crypto"));
|
|
38
38
|
class PureJwtUtil {
|
|
39
|
+
/**
|
|
40
|
+
* Verifies a JWT's signature and expiration, then returns the decoded payload.
|
|
41
|
+
*/
|
|
39
42
|
static verify(token, secret) {
|
|
40
43
|
const parts = token.split('.');
|
|
41
44
|
if (parts.length !== 3)
|
|
@@ -58,6 +61,9 @@ class PureJwtUtil {
|
|
|
58
61
|
}
|
|
59
62
|
return payload;
|
|
60
63
|
}
|
|
64
|
+
/**
|
|
65
|
+
* Signs a payload and returns a compact JWT string.
|
|
66
|
+
*/
|
|
61
67
|
static sign(payload, secret) {
|
|
62
68
|
const header = { alg: 'HS256', typ: 'JWT' };
|
|
63
69
|
const encode = (obj) => Buffer.from(JSON.stringify(obj)).toString('base64url');
|