-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·46 lines (37 loc) · 1.39 KB
/
index.js
File metadata and controls
executable file
·46 lines (37 loc) · 1.39 KB
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
#! /usr/bin/env node
'use strict';
// Thanks to https://github.com/chris-rock/node-crypto-examples'
// And http://lollyrock.com/articles/nodejs-encryption/
// Requirements
const fs = require('fs');
const zlib = require('zlib');
const path = require('path');
const crypto = require('crypto');
// Constants
const algorithm = 'aes-256-ctr';
const userArgs = process.argv.slice(2);
// Input
const fileLocation = path.normalize(path.join(process.cwd(), userArgs[0]));
const secretPhrase = userArgs[1] || 'ProbablyNotGoingToBeSecure';
const secretMode = userArgs[2];
// Functions
function encryptAndSave() {
let zip = zlib.createGzip();
let encrypt = crypto.createCipher(algorithm, secretPhrase);
let fileToEncrypt = fs.createReadStream(fileLocation);
let outputFile = fs.createWriteStream(path.join(path.dirname(fileLocation), (path.parse(fileLocation).name + '.zip')));
fileToEncrypt.pipe(zip).pipe(encrypt).pipe(outputFile);
}
function decryptAndSave() {
let unzip = zlib.createGunzip();
let decrypt = crypto.createDecipher(algorithm, secretPhrase);
let fileToDecrypt = fs.createReadStream(fileLocation);
let outputFile = fs.createWriteStream(path.join(path.dirname(fileLocation), (path.parse(fileLocation).name + 'DELETE.txt')));
fileToDecrypt.pipe(decrypt).pipe(unzip).pipe(outputFile);
}
// Control flow
if (secretMode === 'decrypt') {
decryptAndSave();
} else {
encryptAndSave();
}