@syldel/crypto-utils 1.0.1
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/LICENSE.MD +5 -0
- package/README.md +76 -0
- package/dist/aes-gcm.util.d.ts +10 -0
- package/dist/aes-gcm.util.js +67 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +18 -0
- package/dist/jwt.util.d.ts +4 -0
- package/dist/jwt.util.js +73 -0
- package/package.json +49 -0
package/LICENSE.MD
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
Copyright (c) 2026, Syl-Studio Inc. c/o Syl. D.
|
|
2
|
+
|
|
3
|
+
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
|
|
4
|
+
|
|
5
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# @syldel/crypto-utils
|
|
2
|
+
|
|
3
|
+
Lightweight JWT and AES-256-GCM utilities
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## 👨‍💻 Développement
|
|
8
|
+
|
|
9
|
+
### Installation des dépendances
|
|
10
|
+
```bash
|
|
11
|
+
npm install
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
### Compilation
|
|
15
|
+
Génère le dossier `dist/` contenant les fichiers JavaScript et les déclarations de types (`.d.ts`).
|
|
16
|
+
```bash
|
|
17
|
+
npm run build
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
### Qualité du code
|
|
21
|
+
Le projet utilise **ESLint** pour la logique et **Prettier** pour le formatage.
|
|
22
|
+
```bash
|
|
23
|
+
# Vérifier les erreurs
|
|
24
|
+
npx eslint .
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## đź”— Utilisation en local (Development Workflow)
|
|
30
|
+
|
|
31
|
+
Pour utiliser ce package dans tes autres projets sans le publier sur NPM :
|
|
32
|
+
|
|
33
|
+
1. Dans le dossier `crypto-utils` :
|
|
34
|
+
```bash
|
|
35
|
+
npm link
|
|
36
|
+
```
|
|
37
|
+
2. Dans ton projet (ex: `my-nest-project`) :
|
|
38
|
+
```bash
|
|
39
|
+
npm link @syldel/crypto-utils
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
## 📦 Publication
|
|
45
|
+
|
|
46
|
+
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.
|
|
47
|
+
|
|
48
|
+
### Configuration du Token
|
|
49
|
+
1. Générer un token sur NPM avec les permissions `Read and Write`.
|
|
50
|
+
2. Restreindre l'accès au package `@syldel/crypto-utils` uniquement.
|
|
51
|
+
3. Utiliser l'option `Bypass 2FA` pour permettre l'automatisation.
|
|
52
|
+
|
|
53
|
+
### Commande de publication rapide
|
|
54
|
+
Si tu n'utilises pas de fichier `.npmrc`, tu peux publier en passant le token directement :
|
|
55
|
+
```bash
|
|
56
|
+
npm publish --access public --//registry.npmjs.org/:_authToken=TON_TOKEN_ICI
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Configuration de la publication
|
|
60
|
+
Créez un fichier .npmrc à la racine (ignoré par Git) pour l'authentification :
|
|
61
|
+
|
|
62
|
+
```
|
|
63
|
+
//registry.npmjs.org/:_authToken=npm_votre_token_ici
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Publier une nouvelle version
|
|
67
|
+
La commande suivante automatise le build, l'incrémentation de version et l'envoi vers NPM :
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
npm run release
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Pousse le commit ET le tag sur GitHub
|
|
74
|
+
```bash
|
|
75
|
+
git push origin main --follow-tags
|
|
76
|
+
```
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface EncryptedData {
|
|
2
|
+
data: string;
|
|
3
|
+
iv: string;
|
|
4
|
+
tag: string;
|
|
5
|
+
}
|
|
6
|
+
export declare class AesGcmUtil {
|
|
7
|
+
private static validateKey;
|
|
8
|
+
static encrypt(text: string, key: Buffer | string): EncryptedData;
|
|
9
|
+
static decrypt(payload: EncryptedData, key: Buffer | string): string;
|
|
10
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.AesGcmUtil = void 0;
|
|
37
|
+
const crypto = __importStar(require("crypto"));
|
|
38
|
+
class AesGcmUtil {
|
|
39
|
+
static validateKey(key) {
|
|
40
|
+
const encryptionKey = typeof key === 'string' ? Buffer.from(key, 'hex') : key;
|
|
41
|
+
if (encryptionKey.length !== 32) {
|
|
42
|
+
throw new Error('Invalid Key: Key must be 32 bytes (64 hex characters)');
|
|
43
|
+
}
|
|
44
|
+
return encryptionKey;
|
|
45
|
+
}
|
|
46
|
+
static encrypt(text, key) {
|
|
47
|
+
const encryptionKey = this.validateKey(key);
|
|
48
|
+
const iv = crypto.randomBytes(12);
|
|
49
|
+
const cipher = crypto.createCipheriv('aes-256-gcm', encryptionKey, iv);
|
|
50
|
+
let encrypted = cipher.update(text, 'utf8', 'hex');
|
|
51
|
+
encrypted += cipher.final('hex');
|
|
52
|
+
return {
|
|
53
|
+
data: encrypted,
|
|
54
|
+
iv: iv.toString('hex'),
|
|
55
|
+
tag: cipher.getAuthTag().toString('hex'),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
static decrypt(payload, key) {
|
|
59
|
+
const encryptionKey = this.validateKey(key);
|
|
60
|
+
const decipher = crypto.createDecipheriv('aes-256-gcm', encryptionKey, Buffer.from(payload.iv, 'hex'));
|
|
61
|
+
decipher.setAuthTag(Buffer.from(payload.tag, 'hex'));
|
|
62
|
+
let decrypted = decipher.update(payload.data, 'hex', 'utf8');
|
|
63
|
+
decrypted += decipher.final('utf8');
|
|
64
|
+
return decrypted;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
exports.AesGcmUtil = AesGcmUtil;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./jwt.util"), exports);
|
|
18
|
+
__exportStar(require("./aes-gcm.util"), exports);
|
package/dist/jwt.util.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.PureJwtUtil = void 0;
|
|
37
|
+
const crypto = __importStar(require("crypto"));
|
|
38
|
+
class PureJwtUtil {
|
|
39
|
+
static verify(token, secret) {
|
|
40
|
+
const parts = token.split('.');
|
|
41
|
+
if (parts.length !== 3)
|
|
42
|
+
throw new Error('Invalid token format');
|
|
43
|
+
const [headerB64, payloadB64, signatureB64] = parts;
|
|
44
|
+
const dataToVerify = `${headerB64}.${payloadB64}`;
|
|
45
|
+
const expectedSignature = crypto
|
|
46
|
+
.createHmac('sha256', secret)
|
|
47
|
+
.update(dataToVerify)
|
|
48
|
+
.digest('base64url');
|
|
49
|
+
if (signatureB64 !== expectedSignature) {
|
|
50
|
+
throw new Error('Invalid signature');
|
|
51
|
+
}
|
|
52
|
+
const payload = JSON.parse(Buffer.from(payloadB64, 'base64url').toString('utf8'));
|
|
53
|
+
const payloadRecord = payload;
|
|
54
|
+
if (typeof payloadRecord.exp === 'number') {
|
|
55
|
+
if (Date.now() >= payloadRecord.exp * 1000) {
|
|
56
|
+
throw new Error('Token expired');
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return payload;
|
|
60
|
+
}
|
|
61
|
+
static sign(payload, secret) {
|
|
62
|
+
const header = { alg: 'HS256', typ: 'JWT' };
|
|
63
|
+
const encode = (obj) => Buffer.from(JSON.stringify(obj)).toString('base64url');
|
|
64
|
+
const encodedHeader = encode(header);
|
|
65
|
+
const encodedPayload = encode(payload);
|
|
66
|
+
const signature = crypto
|
|
67
|
+
.createHmac('sha256', secret)
|
|
68
|
+
.update(`${encodedHeader}.${encodedPayload}`)
|
|
69
|
+
.digest('base64url');
|
|
70
|
+
return `${encodedHeader}.${encodedPayload}.${signature}`;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.PureJwtUtil = PureJwtUtil;
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@syldel/crypto-utils",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Lightweight JWT and AES-256-GCM utilities",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"lint": "eslint .",
|
|
12
|
+
"lint:fix": "eslint . --fix",
|
|
13
|
+
"clean": "rm -rf dist",
|
|
14
|
+
"build": "npm run lint && npm run clean && tsc -p tsconfig.json",
|
|
15
|
+
"test": "jest",
|
|
16
|
+
"test:watch": "jest --watch",
|
|
17
|
+
"prepublishOnly": "npm run build",
|
|
18
|
+
"release": "npm run build && npm version patch && npm publish"
|
|
19
|
+
},
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"jwt",
|
|
25
|
+
"aes",
|
|
26
|
+
"crypto",
|
|
27
|
+
"security",
|
|
28
|
+
"nestjs"
|
|
29
|
+
],
|
|
30
|
+
"author": "Syl. D.",
|
|
31
|
+
"license": "ISC",
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@eslint/js": "^10.0.1",
|
|
34
|
+
"@types/jest": "^30.0.0",
|
|
35
|
+
"eslint": "^10.0.1",
|
|
36
|
+
"eslint-config-prettier": "^10.1.8",
|
|
37
|
+
"eslint-plugin-jsonc": "^3.0.0",
|
|
38
|
+
"eslint-plugin-prettier": "^5.5.5",
|
|
39
|
+
"jest": "^30.2.0",
|
|
40
|
+
"jsonc-eslint-parser": "^3.1.0",
|
|
41
|
+
"prettier": "^3.8.1",
|
|
42
|
+
"ts-jest": "^29.4.6",
|
|
43
|
+
"typescript": "^5.9.3",
|
|
44
|
+
"typescript-eslint": "^8.56.0"
|
|
45
|
+
},
|
|
46
|
+
"overrides": {
|
|
47
|
+
"minimatch": "^10.2.1"
|
|
48
|
+
}
|
|
49
|
+
}
|