-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencrypt.js
62 lines (46 loc) · 1.34 KB
/
encrypt.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
// usage:
// node encode.js plainTextFile encryptedFile password
function zaz()
{
const {
createReadStream,
createWriteStream,
} = require('fs');
const {
pipeline
} = require ('stream');
const {
scrypt,
randomFill,
randomBytes,
createCipheriv,
} = require('crypto');
const _algorithm = 'aes-192-cbc';
const _plainFile = process.argv[2];
const _encryptedFile = process.argv[3];
const _password = process.argv[4];
const saltBytes = randomBytes(4);
const salt = saltBytes.toString('hex');
// First, we'll generate the key. The key length is dependent on the algorithm.
// In this case for aes192, it is 24 bytes (192 bits).
scrypt(_password, salt, 24, (err, key) => {
if(err)
throw err;
const iv = randomBytes(16);
var cipher = createCipheriv(_algorithm, key, iv);
//console.log('pass: %s\nsalt: %s\nkey: %s\n', password, saltBytes.toString('hex'), key.toString('hex'));
const input = createReadStream(_plainFile);
const output = createWriteStream(_encryptedFile);
output.write(saltBytes);
output.write(iv);
pipeline(input, cipher, output, (err) => {
if(err)
throw err;
});
});
}
const _plainFile = process.argv[2];
const _encryptedFile = process.argv[3];
const _password = process.argv[4];
const cipher = require('./lib/cipher.js');
cipher.Encrypt(_plainFile, _encryptedFile, _password);