-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
389 lines (287 loc) · 11.9 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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
'use strict';
var crypto = require('crypto');
var async = require('async');
var is = require('is');
// constants
var maxEncryptBlockSize = 16;
var minRandomLength = 8;
var stringEncoding = 'utf8';
// valid values
var validEncryptAlgorithms = crypto.getCiphers();
var validHashAlgorithms = crypto.getHashes();
var validBinaryEncodings = [ 'base64', 'hex' ];
// defaults for property values
var binaryEncodingDefault = 'base64';
var encryptAlgorithmDefault = 'aes-256-cfb';
var hashAlgorithmDefault = 'sha256';
var hashRoundsDefault = 10000;
var maxDataLengthDefault = 4096;
var maxRandomLengthDefault = 120;
var randomLengthDefault = 24;
var skipChecksDefault = false;
// property error messages
var binaryEncodingMessage = 'Property \'binaryEncoding\' must be one of the following: ' + validBinaryEncodings.join(', ');
var encryptAlgorithmMessage = 'Property \'encryptAlgorithm\' invalid; see require(\'crypto\').getCiphers() for a list of valid values';
var encryptKeyMessage = 'Property \'encryptKey\' must be a string with one or more characters';
var hashAlgorithmMessage = 'Property \'hashAlgorithm\' invalid; see require(\'crypto\').getHashes() for a list of valid values';
var hashRoundsMessage = 'Property \'hashRounds\' must be a number';
var hashRoundsRangeMessage = 'Property \'hashRounds\' must be an integer greater than 0';
var maxDataLengthMessage = 'Property \'maxDataLength\' must be a number';
var maxDataLengthRangeMessage = 'Property \'maxDataLength\' must be an integer greater than 0';
var maxRandomLengthMessage = 'Property \'maxRandomLength\' must be a number';
var maxRandomLengthRangeMessage = 'Property \'maxRandomLength\' must be an integer greater than ' + minRandomLength;
var randomLengthMessage = 'Property \'randomLength\' must be a number';
// constructor
function Blind(options) {
options = options || {};
if (!(this instanceof Blind)) {
return new Blind(options);
}
// set and check binaryEncoding
this.binaryEncoding = options.binaryEncoding || binaryEncodingDefault;
if (validBinaryEncodings.indexOf(this.binaryEncoding) < 0) {
throw new RangeError(binaryEncodingMessage);
}
// set and check encryptAlgorithm
this.encryptAlgorithm = options.encryptAlgorithm || encryptAlgorithmDefault;
if (validEncryptAlgorithms.indexOf(this.encryptAlgorithm) < 0) {
throw new RangeError(encryptAlgorithmMessage);
}
// set and check encryptKey
if (options.encryptKey) {
if (!is.string(options.encryptKey) || !options.encryptKey.length) {
throw new TypeError(encryptKeyMessage);
}
if (!is[this.binaryEncoding](options.encryptKey)) {
throw new TypeError('Property \'encryptKey\' must be a ' + this.binaryEncoding + ' encoded binary value');
}
this.encryptKey = options.encryptKey;
}
// set and check hashAlgorithm
this.hashAlgorithm = options.hashAlgorithm || hashAlgorithmDefault;
if (validHashAlgorithms.indexOf(this.hashAlgorithm) < 0) {
throw new RangeError(hashAlgorithmMessage);
}
// set and check hashRounds
this.hashRounds = options.hashRounds || hashRoundsDefault;
if (!is.number(this.hashRounds)) {
throw new TypeError(hashRoundsMessage);
}
if (!is.int(this.hashRounds) || this.hashRounds < 1) {
throw new RangeError(hashRoundsRangeMessage);
}
// set and check maxDataLength
this.maxDataLength = options.maxDataLength || maxDataLengthDefault;
if (!is.number(this.maxDataLength)) {
throw new TypeError(maxDataLengthMessage);
}
if (!is.int(this.maxDataLength) || this.maxDataLength < 1) {
throw new RangeError(maxDataLengthRangeMessage);
}
// set and check maxRandomLength
this.maxRandomLength = options.maxRandomLength || maxRandomLengthDefault;
if (!is.number(this.maxRandomLength)) {
throw new TypeError(maxRandomLengthMessage);
}
if (!is.int(this.maxRandomLength) || this.maxRandomLength < minRandomLength + 1) {
throw new RangeError(maxRandomLengthRangeMessage);
}
// set and check randomLength
this.randomLength = options.randomLength || randomLengthDefault;
if (!is.number(this.randomLength)) {
throw new TypeError(randomLengthMessage);
}
if (!is.int(this.randomLength) || !is.within(this.randomLength, minRandomLength, this.maxRandomLength)) {
throw new RangeError('Property \'randomLength\' must be an integer between ' + minRandomLength + ' and ' + this.maxRandomLength);
}
// set skipChecks
this.skipChecks = options.skipChecks || skipChecksDefault;
}
// methods
Blind.prototype.random = function (length) {
// check properties and arguments
if (!this.skipChecks) {
if (validBinaryEncodings.indexOf(this.binaryEncoding) < 0) {
throw new RangeError(binaryEncodingMessage);
}
if (!is.number(this.maxRandomLength)) {
throw new TypeError(maxRandomLengthMessage);
}
if (!is.int(this.maxRandomLength) || this.maxRandomLength < minRandomLength + 1) {
throw new RangeError(maxRandomLengthRangeMessage);
}
if (length === undefined) {
if (!is.number(this.randomLength)) {
throw new TypeError(randomLengthMessage);
}
if (!is.int(this.randomLength) || !is.within(this.randomLength, minRandomLength, this.maxRandomLength)) {
throw new RangeError('Property \'randomLength\' must be an integer between ' + minRandomLength + ' and ' + this.maxRandomLength);
}
}
else {
if (!is.number(length)) {
throw new TypeError('Argument \'length\' must be a number');
}
if (!is.int(length) || !is.within(length, minRandomLength, this.maxRandomLength)) {
throw new RangeError('Argument \'length\' must be an integer between ' + minRandomLength + ' and ' + this.maxRandomLength);
}
}
}
// generate the random value
return crypto.randomBytes(length || this.randomLength).toString(this.binaryEncoding);
};
Blind.prototype.encrypt = function(data, key) {
// check properties and arguments
if (!this.skipChecks) {
if (validBinaryEncodings.indexOf(this.binaryEncoding) < 0) {
throw new RangeError(binaryEncodingMessage);
}
if (validEncryptAlgorithms.indexOf(this.encryptAlgorithm) < 0) {
throw new RangeError(encryptAlgorithmMessage);
}
if (this.encryptKey) {
if (!is.string(this.encryptKey) || !this.encryptKey.length) {
throw new TypeError(encryptKeyMessage);
}
if (!is[this.binaryEncoding](this.encryptKey)) {
throw new TypeError('Property \'encryptKey\' must be a ' + this.binaryEncoding + ' encoded binary value');
}
}
if (!is.number(this.maxDataLength)) {
throw new TypeError(maxDataLengthMessage);
}
if (!is.int(this.maxDataLength) || this.maxDataLength < 1) {
throw new RangeError(maxDataLengthRangeMessage);
}
if (!is.string(data) || !is.within(data.length, 1, this.maxDataLength)) {
throw new TypeError('Argument \'data\' must be a string between 1 and ' + this.maxDataLength + ' characters long');
}
if (key !== undefined || !this.encryptKey) {
if (!is.string(key) || !key.length) {
throw new TypeError('Argument \'key\' must be a string with one or more characters');
}
if (!is[this.binaryEncoding](key)) {
throw new TypeError('Argument \'key\' must be a ' + this.binaryEncoding + ' encoded binary value');
}
}
}
// encrypt the data
key = new Buffer(key || this.encryptKey, this.binaryEncoding);
var cipher = crypto.createCipher(this.encryptAlgorithm, key);
return cipher.update(data, stringEncoding, this.binaryEncoding) + cipher.final(this.binaryEncoding);
};
Blind.prototype.decrypt = function(data, key) {
// check properties and arguments
if (!this.skipChecks) {
if (validBinaryEncodings.indexOf(this.binaryEncoding) < 0) {
throw new RangeError(binaryEncodingMessage);
}
if (validEncryptAlgorithms.indexOf(this.encryptAlgorithm) < 0) {
throw new RangeError(encryptAlgorithmMessage);
}
if (this.encryptKey) {
if (!is.string(this.encryptKey) || !this.encryptKey.length) {
throw new TypeError(encryptKeyMessage);
}
if (!is[this.binaryEncoding](this.encryptKey)) {
throw new TypeError('Property \'encryptKey\' must be a ' + this.binaryEncoding + ' encoded binary value');
}
}
if (!is.number(this.maxDataLength)) {
throw new TypeError(maxDataLengthMessage);
}
if (!is.int(this.maxDataLength) || this.maxDataLength < 1) {
throw new RangeError(maxDataLengthRangeMessage);
}
var minLength = this.binaryEncoding === 'base64' ? 4 : 2;
var encodingMultiplier = this.binaryEncoding === 'base64' ? 4 / 3 : 2;
var maxBlockLength = Math.ceil(this.maxDataLength / maxEncryptBlockSize);
var maxLength = Math.ceil(maxBlockLength * maxEncryptBlockSize * encodingMultiplier);
if (!is.string(data) || !is.within(data.length, minLength, maxLength)) {
throw new TypeError('Argument \'data\' must be a string between ' + minLength + ' and ' + maxLength + ' characters long');
}
if (!is[this.binaryEncoding](data)) {
throw new TypeError('Argument \'data\' must be a ' + this.binaryEncoding + ' encoded binary value');
}
if (key !== undefined || !this.encryptKey) {
if (!is.string(key) || !key.length) {
throw new TypeError('Argument \'key\' must be a string with one or more characters');
}
if (!is[this.binaryEncoding](key)) {
throw new TypeError('Argument \'key\' must be a ' + this.binaryEncoding + ' encoded binary value');
}
}
}
// decrypt the data
key = new Buffer(key || this.encryptKey, this.binaryEncoding);
var decipher = crypto.createDecipher(this.encryptAlgorithm, key);
return decipher.update(data, this.binaryEncoding, stringEncoding) + decipher.final(stringEncoding);
};
Blind.prototype.hash = function(data, salt, callback) {
// check properties and arguments
if (!this.skipChecks) {
if (validBinaryEncodings.indexOf(this.binaryEncoding) < 0) {
throw new RangeError(binaryEncodingMessage);
}
if (validHashAlgorithms.indexOf(this.hashAlgorithm) < 0) {
throw new RangeError(hashAlgorithmMessage);
}
if (!is.number(this.hashRounds)) {
throw new TypeError(hashRoundsMessage);
}
if (!is.int(this.hashRounds) || this.hashRounds < 1) {
throw new RangeError(hashRoundsRangeMessage);
}
if (!is.number(this.maxDataLength)) {
throw new TypeError(maxDataLengthMessage);
}
if (!is.int(this.maxDataLength) || this.maxDataLength < 1) {
throw new RangeError(maxDataLengthRangeMessage);
}
if (!is.string(data) || !is.within(data.length, 1, this.maxDataLength)) {
throw new TypeError('Argument \'data\' must be a string between 1 and ' + this.maxDataLength + ' characters long');
}
if (salt !== undefined && salt !== null) {
if (!is.string(salt) || !salt.length) {
throw new TypeError('Argument \'salt\' must be a string with one or more characters');
}
if (!is[this.binaryEncoding](salt)) {
throw new TypeError('Argument \'salt\' must be a ' + this.binaryEncoding + ' encoded binary value');
}
}
if (callback && !is.fn(callback)) {
throw new TypeError('Argument \'callback\' must be a function');
}
}
// hash the data
salt = new Buffer(salt || '', this.binaryEncoding);
data = new Buffer(data);
var buffer = Buffer.concat([ salt, data ]);
var i = 0;
if (!callback) {
for (i = 0; i < this.hashRounds; i++) {
buffer = hash(buffer, this.hashAlgorithm);
}
return buffer.toString(this.binaryEncoding);
}
else {
var self = this;
async.whilst(function () { return i < self.hashRounds; },
function (next) {
buffer = hash(buffer, self.hashAlgorithm);
i += 1;
setImmediate(next);
},
function (err) {
var result = !err ? buffer.toString(self.binaryEncoding) : undefined;
callback(err, result);
}
);
}
};
function hash(buffer, algorithm) {
var hasher = crypto.createHash(algorithm);
hasher.update(buffer);
return hasher.digest();
}
module.exports = Blind;