jsonauthtoken 3.0.2 → 3.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/dist/runtime/node.runtime.js +12 -16
- package/package.json +1 -1
|
@@ -32,12 +32,8 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
32
32
|
return result;
|
|
33
33
|
};
|
|
34
34
|
})();
|
|
35
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
-
};
|
|
38
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
36
|
exports.NodeCrypto = void 0;
|
|
40
|
-
const crypto_1 = __importDefault(require("crypto"));
|
|
41
37
|
const functions_lib_1 = require("../lib/functions.lib");
|
|
42
38
|
class NodeCrypto {
|
|
43
39
|
crypto;
|
|
@@ -47,8 +43,8 @@ class NodeCrypto {
|
|
|
47
43
|
}
|
|
48
44
|
}
|
|
49
45
|
_encrypt(algorithm, key, payload) {
|
|
50
|
-
const iv =
|
|
51
|
-
const cipher =
|
|
46
|
+
const iv = this.crypto.randomBytes(12);
|
|
47
|
+
const cipher = this.crypto.createCipheriv(algorithm, key, iv);
|
|
52
48
|
const data = JSON.stringify(payload);
|
|
53
49
|
let encrypted = cipher.update(data, 'utf8', 'base64');
|
|
54
50
|
encrypted += cipher.final('base64');
|
|
@@ -61,23 +57,23 @@ class NodeCrypto {
|
|
|
61
57
|
}
|
|
62
58
|
_decrypt(algorithm, key, encryptedData) {
|
|
63
59
|
const { iv, encrypted, tag } = encryptedData;
|
|
64
|
-
const decipher =
|
|
60
|
+
const decipher = this.crypto.createDecipheriv(algorithm, key, Buffer.from(iv, 'base64'));
|
|
65
61
|
decipher.setAuthTag(Buffer.from(tag, 'base64'));
|
|
66
62
|
let decrypted = decipher.update(encrypted, 'base64', 'utf8');
|
|
67
63
|
decrypted += decipher.final('utf8');
|
|
68
64
|
return JSON.parse(decrypted);
|
|
69
65
|
}
|
|
70
66
|
_rsaPublicKeyGeneration(privateKeyPem) {
|
|
71
|
-
const privateKeyObject =
|
|
67
|
+
const privateKeyObject = this.crypto.createPrivateKey({
|
|
72
68
|
key: privateKeyPem.replace(/\\n/g, '\n'),
|
|
73
69
|
format: 'pem',
|
|
74
70
|
type: 'pkcs8'
|
|
75
71
|
});
|
|
76
|
-
const publicKeyObject =
|
|
72
|
+
const publicKeyObject = this.crypto.createPublicKey(privateKeyObject);
|
|
77
73
|
return publicKeyObject.export({ type: 'spki', format: 'pem' });
|
|
78
74
|
}
|
|
79
75
|
_rsaPrivatePublicKeyGeneration() {
|
|
80
|
-
const { publicKey, privateKey } =
|
|
76
|
+
const { publicKey, privateKey } = this.crypto.generateKeyPairSync('rsa', {
|
|
81
77
|
modulusLength: 2048,
|
|
82
78
|
publicKeyEncoding: {
|
|
83
79
|
type: 'spki',
|
|
@@ -106,17 +102,17 @@ class NodeCrypto {
|
|
|
106
102
|
}
|
|
107
103
|
async decrypt(algo, key, encryptedData) {
|
|
108
104
|
await this.__init();
|
|
109
|
-
const keyHash =
|
|
105
|
+
const keyHash = this.crypto.createHash('sha256').update(key).digest();
|
|
110
106
|
return this._decrypt(algo, keyHash, encryptedData);
|
|
111
107
|
}
|
|
112
108
|
async encryptRSA(payload, publicKey, exp) {
|
|
113
109
|
await this.__init();
|
|
114
|
-
const symmetricKey =
|
|
110
|
+
const symmetricKey = this.crypto.randomBytes(32);
|
|
115
111
|
const newPayload = { payload: payload, exp: exp };
|
|
116
112
|
const { iv, encrypted, tag } = this._encrypt('aes-256-gcm', symmetricKey, newPayload);
|
|
117
|
-
const encryptedSymmetricKey =
|
|
113
|
+
const encryptedSymmetricKey = this.crypto.publicEncrypt({
|
|
118
114
|
key: publicKey,
|
|
119
|
-
padding:
|
|
115
|
+
padding: this.crypto.constants.RSA_PKCS1_OAEP_PADDING,
|
|
120
116
|
oaepHash: 'sha256'
|
|
121
117
|
}, symmetricKey);
|
|
122
118
|
return (0, functions_lib_1.tokenFormatCreate)({
|
|
@@ -131,9 +127,9 @@ class NodeCrypto {
|
|
|
131
127
|
}
|
|
132
128
|
async decryptRSA(privateKey, encryptedKey, encryptedData) {
|
|
133
129
|
await this.__init();
|
|
134
|
-
const decryptedSymmetricKey =
|
|
130
|
+
const decryptedSymmetricKey = this.crypto.privateDecrypt({
|
|
135
131
|
key: privateKey,
|
|
136
|
-
padding:
|
|
132
|
+
padding: this.crypto.constants.RSA_PKCS1_OAEP_PADDING,
|
|
137
133
|
oaepHash: 'sha256'
|
|
138
134
|
}, Buffer.from(encryptedKey, 'base64'));
|
|
139
135
|
return this._decrypt('aes-256-gcm', decryptedSymmetricKey, encryptedData);
|