-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathencrypt-file.js
52 lines (50 loc) · 1.41 KB
/
encrypt-file.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
const {encrypt_symmetric} = require('./modules/cryptobitgreen.js');
const { Buffer } = require('node:buffer');
const { readFileSync,writeFileSync } = require('node:fs');
// read parameters
var argv = process.argv ;
const filename=argv[2];
if(typeof filename==='undefined'){
console.log("File name parameter is missing");
show_help();
return;
}
const pwd=argv[3];
if(typeof pwd==='undefined'){
console.log("Password parameter is missing");
show_help();
return;
}
mainloop(filename,pwd);
// async main body
async function mainloop(filename,pwd){
let fc;
// read file
try {
fc=readFileSync(filename);
}catch(e){
console.log("ERROR reading file",filename,e);
return;
}
//encrypt
let encmsg= await encrypt_symmetric(fc,pwd);
//console.log("encrypted buffer: ",encmsg);
// save encrypted file adding .enc
let filenameenc=filename+'.enc';
try{
writeFileSync(filenameenc,encmsg);
}catch(e){
console.log("Error saving encrypted file",filenameenc,e);
return;
}
console.log("File:",filename,"has been encrypted in:",filenameenc);
return;
}
//functiont to show help
function show_help(){
console.log("encrypt-file.js requires 2 parameters:");
console.log("- file name to encrypt");
console.log("- password");
console.log("For example:");
console.log("node encrypt-file.js test.txt my_password")
}