-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
49 lines (42 loc) · 1.28 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
import { createReadStream, createWriteStream } from 'node:fs';
import { randomKey, encryptTheKey } from './flow.js';
import { createCipher } from './syme.js';
import { getFiles, initFilesOut } from './util.js';
import zlib from 'node:zlib';
import { channel } from './receiver.js';
// step 1: prepare variables, and files.
initFilesOut(); // initialize 'files.out' directory.
const key = randomKey();
// step 2: encrypt the key.
const keyEnc = await encryptTheKey(key);
// step 2.5: read files.
const files = await getFiles();
// step 3: encrypt.
for (const file of files) {
// symetric encryption..
const stream = createReadStream(file, {
'autoClose': true,
});
const writeFileStream = createWriteStream(
file.replace('files', 'files.out'),
{
'autoClose': true,
},
);
const c = createCipher(key);
const compressor = zlib.createDeflateRaw({
level: zlib.constants.Z_BEST_SPEED,
flush: zlib.constants.Z_SYNC_FLUSH,
});
stream.pipe(compressor).pipe(c.cipher).pipe(writeFileStream);
stream.on('close', () => {
console.log(file, 'encrypted and compressed');
writeFileStream.close();
// send to client/receiver to decrypt it.
channel.emit('file', {
'key': keyEnc.toString('hex'),
'iv': c.iv.toString('hex'),
'filePath': file.replace('files', 'files.out'),
});
});
}