-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
52 lines (41 loc) · 1.5 KB
/
app.js
File metadata and controls
52 lines (41 loc) · 1.5 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
47
48
49
50
51
52
/* eslint-disable no-console */
'use strict';
// Simple usage of the AES-256 in CTR mode for ONE BLOCK !!!
const FileCrypto = require('./file_crypto');
const Key = require('./key');
const INPUT_ENCODING = 'utf8';
const OUTPUT_ENCODING = 'hex';
Promise.all([Key.generate(32), Key.generate(32), Key.generate(16)]).then(
// After having successfully generated the keys
keys => {
// assign them to their variables
const encryptionKey = keys[0];
const hmacKey = keys[1];
const iv = keys[2];
// init the crypto class
const fileCrypto = new FileCrypto(encryptionKey, iv);
// encrypt chunk of fata
fileCrypto.encryptChunk(Buffer.from('YELLOW_SUBMARINE', INPUT_ENCODING), hmacKey)
.then(result => {
// Resolve the promise
// Print the cipher text in hex format (the buffer can be directly stored to file in case)
console.log(result.cipherText.toString(OUTPUT_ENCODING));
// the Mac needs to be stored as well next to it.
console.log(result.mac.toString(OUTPUT_ENCODING));
// return the result to proceed with the chaining
return result;
})
.then((result) => {
// at this point, using the same keys, decrypt the file
// We encrypted only 1 block, therefore the counter is zero
fileCrypto.decryptChunk(result.cipherText, result.mac, hmacKey, 0)
.then(clear => {
console.log(clear.toString(INPUT_ENCODING));
});
})
.catch((err) => console.log(err));
},
reason => {
console.log(reason);
}
);