-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
29 lines (24 loc) · 812 Bytes
/
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
const crypto = require('crypto');
const through = require('through2');
module.exports = function ({password = 'password', decrypt = false} = {}) {
return decrypt
? through.obj((vinylFile, encoding, callback) => {
callback(null, encryptor(vinylFile, password, decrypt));
})
: through.obj((vinylFile, encoding, callback) => {
callback(null, encryptor(vinylFile, password, decrypt));
});
};
function encryptor(vinylFile, password, decrypt) {
if (vinylFile._contents) {
let encrypt = crypto.createCipher('aes-256-ctr', password);
if (decrypt) {
encrypt = crypto.createDecipher('aes-256-ctr', password);
}
vinylFile._contents = Buffer.concat(
[encrypt.update(vinylFile._contents), encrypt.final()]
);
return vinylFile;
}
return vinylFile;
}