-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
198 lines (186 loc) · 7.74 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
var fs = require("fs");
var recursive = require("recursive-readdir");
var crypto = require('crypto');
var pathModule = require("path");
var readlineSync = require('readline-sync');
async function main() {
const choices = ['Encrypt Folder', 'Decrypt Folder', 'Encrypt Folder with max file size limit', 'Encrypt File', 'Decrypt File'];
var choice = readlineSync.keyInSelect(choices, 'Select your choice: ');
const file_path = readlineSync.questionPath('Enter Relative Path: ');
if (!fs.existsSync(file_path)) {
console.log("Item doesn't exist on disk");
readlineSync.keyInPause();
process.exit(-1)
}
var password = undefined
if (choice != 1 && choice != 4) {
var password2 = undefined
while (!password || (password != password2 || password == '')) {
password = readlineSync.question('Enter Password: ', { hideEchoBack: true });
password2 = readlineSync.question('Enter Password again: ', { hideEchoBack: true });
if (password != password2)
console.log("Passwords do not match")
else if (password == '')
console.log('Empty passwords not allowed')
}
} else {
password = readlineSync.question('Enter Password: ', { hideEchoBack: true });
}
console.log("Path: " + file_path)
if (!readlineSync.keyInYNStrict('Proceed..? Y/N'))
process.exit(0)
var start = Date.now()
switch (choice) {
case 0:
await encryptFolder(file_path, password)
console.log("Encrypted!")
break
case 1:
await decryptFolder(file_path, password)
console.log("Decrypted!")
break
case 2:
await encryptFolder(file_path, password, readlineSync.questionFloat("Max Filesize (in MB)"))
console.log("Encrypted!")
break
case 3:
try {
await encryptFile(file_path, password)
console.log("Encrypted!")
} catch (e) {
console.log("Error Encrypting!")
}
break
case 4:
try {
await decryptFile(file_path, password)
console.log("Decrypted!")
} catch (e) {
console.log("Error Decrypting!")
}
break
}
console.log("Time taken: " + ((Date.now() - start) / 1000) + "s")
readlineSync.keyInPause();
process.exit(0)
}
function encryptFileI(inputPath, outputPath, key, callback) {
var inputStream = fs.createReadStream(inputPath);
var outputStream = fs.createWriteStream(outputPath);
var cipher = crypto.createCipheriv('aes-256-cbc', key, Buffer.from('fda2f5ddca990c8719f20b158ff9ee00', 'hex'));
inputStream.pipe(cipher).pipe(outputStream);
outputStream.on('finish', function () {
return callback();
});
}
function encryptText(text, key) {
var cipher = crypto.createCipheriv('aes-256-cbc', crypto.pbkdf2Sync(key, Buffer.from('ba6dfaa9a6db69f0f2d50745a3516f1cb080e637ceadea4f5e6d9edff3c079602130694585e1df1a3a3176b20fcd10b0af87f6dc766fafd26b0928e7c49ebcdf', 'hex'), 2145, 32, 'sha512'), Buffer.from('fda2f5ddca990c8719f20b158ff9ee00', 'hex'));
return cipher.update(text, 'utf8', 'hex') + cipher.final('hex');
}
function decryptText(text, key) {
try {
var decipher = crypto.createDecipheriv('aes-256-cbc', crypto.pbkdf2Sync(key, Buffer.from('ba6dfaa9a6db69f0f2d50745a3516f1cb080e637ceadea4f5e6d9edff3c079602130694585e1df1a3a3176b20fcd10b0af87f6dc766fafd26b0928e7c49ebcdf', 'hex'), 2145, 32, 'sha512'), Buffer.from('fda2f5ddca990c8719f20b158ff9ee00', 'hex'));
return decipher.update(text, 'hex', 'utf8') + decipher.final('utf8');
}
catch (e) {
console.log("Wrong Key!")
readlineSync.keyInPause()
process.exit(0)
}
}
function decryptFileI(inputPath, outputPath, key, callback) {
var inputStream = fs.createReadStream(inputPath);
var outputStream = fs.createWriteStream(outputPath);
var cipher = crypto.createDecipheriv('aes-256-cbc', key, Buffer.from('fda2f5ddca990c8719f20b158ff9ee00', 'hex'));
inputStream.pipe(cipher).pipe(outputStream);
outputStream.on('finish', function () {
return callback();
});
}
async function encryptFile(inputFile, key = "1234") {
return new Promise((resolve, reject) => {
if (key == "1234") {
return reject("Set a good key");
}
fs.stat(inputFile, function (err) {
if (err)
return reject("No such file");
if (inputFile.endsWith(".enc"))
return resolve("Already Encrypted");
encryptFileI(inputFile, pathModule.dirname(inputFile) + "/" + encryptText(pathModule.basename(inputFile), key) + ".enc", key, function (err) {
if (err)
return reject(err);
else
fs.unlink(inputFile, function (err) {
if (err)
return reject();
else
return resolve();
});
});
});
});
}
function decryptFile(inputFile, key = "1234") {
return new Promise((resolve, reject) => {
if (key == "1234") {
return reject("Set a good key");
}
fs.stat(inputFile, function (err) {
if (err)
return reject("No such file");
if (!inputFile.endsWith(".enc"))
return resolve("Not Encrypted");
decryptFileI(inputFile, pathModule.dirname(inputFile) + "/" + decryptText(pathModule.basename(inputFile).split(".enc")[0], key), key, function (err) {
if (err)
return reject(err);
else
fs.unlink(inputFile, function (err) {
if (err)
return reject();
else
return resolve();
});
});
});
});
}
async function encryptFolder(path, key, maxFileSize = 0) {
return new Promise(async (resolve, reject) => {
key = Buffer.from(key.split('').reverse().join('')).toString('base64');
key = crypto.pbkdf2Sync(key, Buffer.from('ba6dfaa9a6db69f0f2d50745a3516f1cb080e637ceadea4f5e6d9edff3c079602130694585e1df1a3a3176b20fcd10b0af87f6dc766fafd26b0928e7c49ebcdf', 'hex'), 2145, 32, 'sha512');
var files = await recursive(path, ["node_modules", "*.enc"]);
var promises = []
for (let i = 0; i < files.length; i++) {
var file = files[i];
if (maxFileSize != 0)
if (((fs.statSync(file).size) / 1000000.0) > maxFileSize)
continue
promises.push(encryptFile(file, key));
}
Promise.all(promises).then(() => { resolve() }).catch((err) => {
if (err) {
throw new Error(err);
}
});
})
}
async function decryptFolder(path, key) {
return new Promise(async (resolve, reject) => {
key = Buffer.from(key.split('').reverse().join('')).toString('base64');
key = crypto.pbkdf2Sync(key, Buffer.from('ba6dfaa9a6db69f0f2d50745a3516f1cb080e637ceadea4f5e6d9edff3c079602130694585e1df1a3a3176b20fcd10b0af87f6dc766fafd26b0928e7c49ebcdf', 'hex'), 2145, 32, 'sha512');
var files = await recursive(path, ["node_modules"]);
var promises = []
for (let i = 0; i < files.length; i++) {
var file = files[i];
promises.push(decryptFile(file, key))
}
Promise.all(promises).then(() => resolve()).catch((err) => {
if (err) {
fs.unlinkSync(file.split(".enc")[0]);
throw new Error(err);
}
});
})
}
main()