-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.js
47 lines (37 loc) · 1.14 KB
/
node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"use strict";
let crypto = require("crypto");
let sharedSecret;
function bespokeEncrypt(plaintext) {
let initializationVector = crypto.randomBytes(16); // IV is always 8-bytes
let encrypted = "";
let cipher = crypto.createCipheriv(
"aes-128-cbc",
sharedSecret,
initializationVector
);
encrypted += cipher.update(plaintext, "utf8", "base64");
encrypted += cipher.final("base64");
return (
toWeb64(encrypted) + ":" + toWeb64(initializationVector.toString("base64"))
);
}
function bespokeDecrypt(parts) {
let [encrypted, initializationVector] = parts.split(":");
let plaintext = "";
let cipher = crypto.createDecipheriv(
"aes-128-cbc",
sharedSecret,
Buffer.from(initializationVector, "base64")
);
plaintext += cipher.update(encrypted, "base64", "utf8");
plaintext += cipher.final("utf8");
return plaintext;
}
function toWeb64(x) {
return x.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
}
module.exports.bespokeEncrypt = bespokeEncrypt;
module.exports.bespokeDecrypt = bespokeDecrypt;
module.exports.init = function (_sharedSecret) {
sharedSecret = Buffer.from(_sharedSecret, "base64");
};