-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathencryption.js
94 lines (78 loc) · 2.62 KB
/
encryption.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
/*
Dependencies:
npm install @peculiar/webcrypto
npm install text-encoder-lite
*/
const fs = require('fs');
const { Crypto } = require("@peculiar/webcrypto");
const crypto = new Crypto();
const TextEncoder = require('text-encoder-lite').TextEncoderLite;
const TextDecoder = require('text-encoder-lite').TextDecoderLite;
// Arguments
const args = process.argv.splice(process.execArgv.length + 2);
const pass = args[0] || "testing";
const source = args[1] || "source.tmp";
const target = args[2] || source + ".encrypted"
function salt() {
var fixed = [ 195, 78, 249, 203, 63, 36, 76, 84, 86, 202, 30, 92, 92, 89, 101, 53 ];
var salt = new Uint8Array(fixed.length ? fixed : 16);
if (!fixed.length) salt = crypto.getRandomValues(salt);
return salt;
}
function ab2str(buf) {
return new TextDecoder().decode(new Uint8Array(buf));
}
function str2ab(str) {
return new TextEncoder("utf-8").encode(str);
}
function encrypt (data, pass, cb) {
var vector = new Uint8Array(salt());
crypto.subtle.digest({name: 'SHA-256'}, str2ab(pass)).then((res) => {
crypto.subtle.importKey('raw', res, {name: 'AES-CBC'}, false, ['encrypt', 'decrypt']).then((key) => {
crypto.subtle.encrypt({name: 'AES-CBC', iv: vector}, key, str2ab(data)).then((encrypted) => {
cb(encrypted, vector);
}).catch((err) => {
console.error(err);
cb(null);
});
})
})
}
function decrypt (cypher, vector, pass, cb) {
var cypher = new Uint8Array(cypher);
var vector = new Uint8Array(vector);
crypto.subtle.digest({name: 'SHA-256'}, str2ab(pass)).then((res) => {
crypto.subtle.importKey('raw', res, {name: 'AES-CBC'}, false, ['encrypt', 'decrypt']).then((key) => {
crypto.subtle.decrypt({name: 'AES-CBC', iv: vector}, key, cypher).then((decrypted) => {
cb(decrypted);
}).catch((err) => {
console.error(err);
cb(null);
})
})
})
}
fs.readFile(source, null, function read(err, data) {
if (err) {
return console.log(err);
}
encrypt(ab2str(data), pass, function(cypher, vector) {
let buffer = Buffer.concat([vector, new Uint8Array(cypher)]);
fs.writeFile(target, buffer, function(err) {
if (err) {
return console.log(err);
}
console.log('Written encrypted file to: ', target);
// fs.readFile(target, null, function read(err, data) {
// if (err) {
// return console.log(err);
// }
// cypher = data.slice(16);
// vector = data.slice(0, 16);
// decrypt (cypher, vector, pass, function(decrypted) {
// console.log(ab2str(decrypted));
// });
// });
});
});
});