zurik 1.0.3 → 1.0.5
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/lib/crypto.js +11 -13
- package/package.json +1 -1
package/lib/crypto.js
CHANGED
|
@@ -3,9 +3,9 @@ import crypto from "crypto";
|
|
|
3
3
|
const ALGO = "aes-256-gcm";
|
|
4
4
|
const IV_LENGTH = 16;
|
|
5
5
|
|
|
6
|
-
function deriveKey(password) {
|
|
6
|
+
function deriveKey(password, salt) {
|
|
7
7
|
return new Promise((resolve, reject) => {
|
|
8
|
-
crypto.scrypt(password,
|
|
8
|
+
crypto.scrypt(password, salt, 32, (err, key) => {
|
|
9
9
|
if (err) reject(err);
|
|
10
10
|
else resolve(key);
|
|
11
11
|
});
|
|
@@ -13,29 +13,27 @@ function deriveKey(password) {
|
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
export async function encryptData(data, password) {
|
|
16
|
-
const
|
|
16
|
+
const salt = crypto.randomBytes(16);
|
|
17
|
+
const key = await deriveKey(password, salt);
|
|
17
18
|
const iv = crypto.randomBytes(IV_LENGTH);
|
|
18
19
|
const cipher = crypto.createCipheriv(ALGO, key, iv);
|
|
19
20
|
|
|
20
21
|
const encrypted = Buffer.concat([cipher.update(data), cipher.final()]);
|
|
21
22
|
const tag = cipher.getAuthTag();
|
|
22
23
|
|
|
23
|
-
return Buffer.concat([iv, tag, encrypted]).toString("base64");
|
|
24
|
+
return Buffer.concat([salt, iv, tag, encrypted]).toString("base64");
|
|
24
25
|
}
|
|
25
26
|
|
|
26
27
|
export async function decryptData(base64, password) {
|
|
27
28
|
const buffer = Buffer.from(base64, "base64");
|
|
29
|
+
const salt = buffer.subarray(0, 16);
|
|
30
|
+
const iv = buffer.subarray(16, 32);
|
|
31
|
+
const tag = buffer.subarray(32, 48);
|
|
32
|
+
const ciphertext = buffer.subarray(48);
|
|
28
33
|
|
|
29
|
-
const
|
|
30
|
-
const tag = buffer.subarray(16, 32);
|
|
31
|
-
const ciphertext = buffer.subarray(32);
|
|
32
|
-
|
|
33
|
-
const key = await deriveKey(password);
|
|
34
|
+
const key = await deriveKey(password, salt);
|
|
34
35
|
const decipher = crypto.createDecipheriv(ALGO, key, iv);
|
|
35
36
|
decipher.setAuthTag(tag);
|
|
36
37
|
|
|
37
|
-
return Buffer.concat([
|
|
38
|
-
decipher.update(ciphertext),
|
|
39
|
-
decipher.final()
|
|
40
|
-
]);
|
|
38
|
+
return Buffer.concat([decipher.update(ciphertext), decipher.final()]);
|
|
41
39
|
}
|