-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (35 loc) · 1.09 KB
/
index.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
var CryptoJS = require("crypto-js");
var iterations = 1000;
var salt = "NaCl";
var iv = "IIIIIIIIIIIIIIII";
function encrypt(msg, pass) {
var key = CryptoJS.PBKDF2(pass, CryptoJS.enc.Utf8.parse(salt), {
keySize: 256 / 32,
hasher: CryptoJS.algo.SHA256,
iterations: iterations
});
var encrypted = CryptoJS.AES.encrypt(msg, key, {
iv: CryptoJS.enc.Utf8.parse(iv),
mode: CryptoJS.mode.CTR,
padding: CryptoJS.pad.NoPadding
});
return encrypted.ciphertext.toString(CryptoJS.enc.Hex);
}
function decrypt(encryptedText, pass) {
var key = CryptoJS.PBKDF2(pass, CryptoJS.enc.Utf8.parse(salt), {
keySize: 256 / 32,
hasher: CryptoJS.algo.SHA256,
iterations: iterations
});
var cipherParams = CryptoJS.lib.CipherParams.create(
{ ciphertext: CryptoJS.enc.Hex.parse(encryptedText) }
);
var decrypted = CryptoJS.AES.decrypt(cipherParams, key, {
iv: CryptoJS.enc.Utf8.parse(iv),
mode: CryptoJS.mode.CTR,
padding: CryptoJS.pad.NoPadding
})
return decrypted.toString(CryptoJS.enc.Utf8);
}
exports.openEncrypt = encrypt
exports.openDecrypt = decrypt