This repository was archived by the owner on Jun 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathendecrypt.js
More file actions
352 lines (325 loc) · 9.81 KB
/
Copy pathendecrypt.js
File metadata and controls
352 lines (325 loc) · 9.81 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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
Copyright 2013 Daniel Wirtz <dcode@dcode.io>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var crypto = require("crypto"),
stream = require("stream"),
util = require("util"),
PSON = require("pson");
/**
* endecrypt namespace.
* @type {Object.<string.*>}
*/
var endecrypt = {};
/**
* Salt length.
* @type {number}
* @const
*/
endecrypt.SALT_LENGTH = 128/8;
/**
* Default number of PBKDF2 rounds.
* @type {number}
* @const
*/
endecrypt.DEFAULT_ROUNDS = 100000;
/**
* Prepares the endecrypt options.
* @param {*} options Given options
* @returns {{rounds: number}} Prepared options
* @private
*/
function mkopt(options) {
options = options || {};
options.rounds = options.rounds || endecrypt.DEFAULT_ROUNDS;
return options;
}
/**
* Constructs a new encrypt stream.
* @param {string} passphrase Passphrase
* @param {Object.<string,*>=} options Options
* @constructor
* @extends stream.Transform
*/
var Encrypt = function(passphrase, options) {
stream.Transform.call(this);
this.options = mkopt(options);
this.passphrase = passphrase;
this.cipher = null;
};
// Extends stream.Transform
util.inherits(Encrypt, stream.Transform);
/**
* Transforms / encrypts the next chunk.
* @param {Buffer|string} chunk Next chunk
* @param {string} encoding Encoding if chunk is a string
* @param {function()} done Callback
* @private
*/
Encrypt.prototype._transform = function(chunk, encoding, done) {
if (!Buffer.isBuffer(chunk)) {
chunk = new Buffer(chunk, encoding);
}
if (this.cipher === null) { // Initialize
crypto.randomBytes(endecrypt.SALT_LENGTH, function(err, salt) {
if (err) {
this.emit("error", err);
return;
}
crypto.pbkdf2(this.passphrase, salt, this.options.rounds, 48, function(err, keyiv) {
if (err) {
throw(err);
}
this.passphrase = null;
if (!Buffer.isBuffer(keyiv)) keyiv = new Buffer(keyiv, "binary"); // node 0.8
this.cipher = crypto.createCipheriv("aes256", keyiv.slice(0, 32), keyiv.slice(32));
this.push(salt);
this.push(this.cipher.update(chunk));
done();
}.bind(this));
}.bind(this));
} else {
this.push(this.cipher.update(chunk));
done();
}
};
/**
* Flushes all remaining data.
* @param {function()} done Callback
* @private
*/
Encrypt.prototype._flush = function(done) {
if (this.cipher !== null) {
this.push(this.cipher.final());
}
done();
};
/**
* @alias Encrypt
*/
endecrypt.Encrypt = Encrypt;
/**
* Creates a new encrypt stream.
* @param {string} passphrase Passphrase
* @param {Object.<string,*>=} options Options
* @returns {endecrypt.Encrypt}
*/
endecrypt.createEncrypt = function(passphrase, options) {
return new endecrypt.Encrypt(passphrase, options);
};
/**
* Constrcuts a new decrypt stream.
* @param {string} passphrase Passphrase
* @param {Object.<string,*>=} options Options
* @constructor
* @extends stream.Transform
*/
var Decrypt = function(passphrase, options) {
stream.Transform.call(this);
this.salt = new Buffer(0);
this.passphrase = passphrase;
this.options = mkopt(options);
this.cipher = null;
};
// Extends stream.Transform
util.inherits(Decrypt, stream.Transform);
/**
* Gathers additional salt bytes.
* @param {Buffer} chunk Next chunk
* @returns {Buffer} Remaining data or NULL if not yet enough
* @private
*/
Decrypt.prototype._gather = function(chunk) {
var remain = endecrypt.SALT_LENGTH - this.salt.length;
if (chunk.length < remain) { // Append additional salt bytes and wait for more
this.salt = Buffer.concat([this.salt, chunk]);
return null;
} // Else fill and continue
this.salt = Buffer.concat([this.salt, chunk.slice(0, remain)]);
return chunk.slice(remain);
};
/**
* Transforms / decrypts the next chunk.
* @param {Buffer|string} chunk Next chunk
* @param {string} encoding Encoding if chunk is a string
* @param {function()} done Callback
* @private
*/
Decrypt.prototype._transform = function(chunk, encoding, done) {
if (!Buffer.isBuffer(chunk)) {
chunk = new Buffer(chunk, encoding);
}
if (this.salt.length < endecrypt.SALT_LENGTH) {
if ((chunk = this._gather(chunk)) === null) {
done(); return;
}
}
if (this.cipher === null) { // Set up
// FIXME: node's crypto module uses HMAC-SHA1 so deriving a 256 bit key and 128 bit iv is suboptimal. However,
// a plain JavaScript implementation would be much slower and a C module would need a compile step. Any ideas?
crypto.pbkdf2(this.passphrase, this.salt, this.options.rounds, 48, function(err, keyiv) {
if (err) {
this.emit("error", err);
return;
}
this.passphrase = null;
if (!Buffer.isBuffer(keyiv)) keyiv = new Buffer(keyiv, "binary"); // node 0.8
this.cipher = crypto.createDecipheriv("aes256", keyiv.slice(0, 32), keyiv.slice(32));
try {
this.push(this.cipher.update(chunk));
} catch (ex) {
this.emit("error", ex);
return;
}
done();
}.bind(this));
} else {
try {
this.push(this.cipher.update(chunk));
} catch (ex) {
this.emit("error", ex);
return;
}
done();
}
};
/**
* Flushes all remaining data.
* @param {function()} done Callback
* @private
*/
Decrypt.prototype._flush = function(done) {
if (this.cipher !== null) {
try {
this.push(this.cipher.final());
} catch (ex) {
this.emit("error", ex);
return;
}
}
done();
};
/**
* @alias Decrypt
*/
endecrypt.Decrypt = Decrypt;
/**
* Creates a new decrypt stream.
* @param {string} passphrase Passphrase
* @param {Object.<string,*>} options Options
* @returns {endecrypt.Decrypt}
*/
endecrypt.createDecrypt = function(passphrase, options) {
return new endecrypt.Decrypt(passphrase, options);
};
/**
* Encrypts a buffer using the specified passphrase.
* This is a convenience wrapper around a proper stream and should not be used with large data.
* @param {Buffer} buf Buffer to encrypt
* @param {string} passphrase Passphrase
* @param {Object.<string,*>|function(Error, Buffer=)} options Options
* @param {function(Error, Buffer=)=} callback Callback
*/
endecrypt.encrypt = function(buf, passphrase, options, callback) {
if (typeof options === 'function') {
callback = options;
options = null;
}
var enc = endecrypt.createEncrypt(passphrase, options);
var out = [];
enc.on("data", function(chunk) {
out.push(chunk);
});
enc.on("end", function() {
callback(null, Buffer.concat(out));
});
enc.on("error", function(err) {
callback(err);
});
enc.end(buf);
};
/**
* Encrypts any JSON value using the specified passphrase.
* @param {*} data Data to encrypt
* @param {string} passphrase Passphrase
* @param {Object.<string,*>|function(Error, Buffer=)} options Options
* @param {function(Error, Buffer=)=} callback Callback
*/
endecrypt.encryptStore = function(data, passphrase, options, callback) {
if (typeof options === 'function') {
callback = options;
options = null;
}
var pair = new PSON.StaticPair();
try {
endecrypt.encrypt(pair.encode(data).toBuffer(), passphrase, options, callback);
} catch (err) {
process.nextTick(callback.bind(this, err));
}
};
/**
* Decrypts a buffer using the specified passphrase.
* This is a convenience wrapper around a proper stream and should not be used with large data.
* @param {Buffer} buf Buffer to decrypt
* @param {string} passphrase Passphrase
* @param {Object.<string,*>|function(Error, Buffer=)} options Options
* @param {function(Error, Buffer=)=} callback Callback
*/
endecrypt.decrypt = function(buf, passphrase, options, callback) {
if (typeof options === 'function') {
callback = options;
options = null;
}
var dec = endecrypt.createDecrypt(passphrase, options);
var out = [];
dec.on("data", function(chunk) {
out.push(chunk);
});
dec.on("end", function() {
callback(null, Buffer.concat(out));
});
dec.on("error", function(err) {
callback(err);
});
dec.end(buf);
};
/**
* Decrypts a buffer to a JSON value using the specified passphrase.
* @param {Buffer} buf Buffer to decrypt
* @param {string} passphrase Passphrase
* @param {Object.<string,*>|function(Error, *=)} options Options
* @param {function(Error, *=)=} callback Callback
*/
endecrypt.decryptStore = function(buf, passphrase, options, callback) {
if (typeof options === 'function') {
callback = options;
options = null;
}
endecrypt.decrypt(buf, passphrase, options, function(err, data) {
if (err) {
callback(err);
return;
}
var pair = new PSON.StaticPair();
try {
data = pair.decode(data);
callback(null, data);
} catch (err) {
callback(err);
}
});
};
/**
* CLI utilities.
* @type {Object.<string,*>}
*/
endecrypt.cli = require("./cli.js");
module.exports = endecrypt;