forked from OpenApi-5p/5paisa-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
42 lines (30 loc) · 908 Bytes
/
utils.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
const crypto = require("crypto");
const iv = new Buffer.from([83, 71, 26, 58, 54, 35, 22, 11,
83, 71, 26, 58, 54, 35, 22, 11]);
const AES256Encrypt = (key, text) => {
const keyLen = 256;
const IVLen = 128;
try {
const keyGen = crypto.pbkdf2Sync(
Buffer.from(key),
Uint8Array.from(iv),
1000,
keyLen + IVLen,
"sha1"
);
const aesKey = Buffer.allocUnsafe(32);
const aesIV = Buffer.allocUnsafe(16);
// you need to limit the buffer copy to 16 for aesIV.
keyGen.copy(aesIV, 0, 0, 16);
keyGen.copy(aesKey, 0, 16);
const cipher = crypto.createCipheriv("aes-256-cbc", aesKey, aesIV);
let encrypted = cipher.update(text, "utf8", "base64");
encrypted += cipher.final("base64");
return encrypted;
} catch (error) {
console.log(error, "error in encrypt");
}
};
module.exports = {
AES256Encrypt: AES256Encrypt,
};