-
Notifications
You must be signed in to change notification settings - Fork 88
/
index.js
386 lines (326 loc) · 10.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
"use strict";
var scryptNative = require("./build/Release/scrypt")
, Crypto = require("crypto")
, Os = require("os");
var checkNumberOfArguments = function(args, message, numberOfArguments) {
if (message === undefined) message = "No arguments present";
if (numberOfArguments === undefined) numberOfArguments = 1;
if (args.length < numberOfArguments) {
var error = new SyntaxError(message);
throw error;
}
}
//
// Checks async arguments. Will throw error if callback does not exist and
// promises are not available
//
var checkAsyncArguments = function(args, callback_least_needed_pos, message) {
checkNumberOfArguments(args);
var callback_index = (function(){
for (var i=0; i < args.length; i++) {
if (typeof args[i] === "function") {
return i;
}
}
})();
if (callback_index === undefined) {
if (typeof Promise !== "undefined")
return undefined; // if promises are available, don't worry about call backs
var error = new SyntaxError("No callback function present, and Promises are not available");
throw error;
}
if (callback_index < callback_least_needed_pos) {
var error = new SyntaxError(message);
throw error;
}
return callback_index;
}
//
// Checks the scrypt parameters object
//
var checkScryptParametersObject = function(params) {
var error = undefined;
if (typeof params !== "object") {
var error = new TypeError("Scrypt parameters type is incorrect: It must be a JSON object");
}
if (!error && !params.hasOwnProperty("N")) {
var error = new TypeError("Scrypt params object does not have 'N' property present");
}
if (!error && params.N !== parseInt(params.N)) {
var error = new TypeError("Scrypt params object 'N' property is not an integer");
}
if (!error && !params.hasOwnProperty("r")) {
var error = new TypeError("Scrypt params object does not have 'r' property present");
}
if (!error && params.r !== parseInt(params.r)) {
var error = new TypeError("Scrypt params object 'r' property is not an integer");
}
if (!error && !params.hasOwnProperty("p")) {
var error = new TypeError("Scrypt params object does not have 'p' property present");
}
if (!error && params.p !== parseInt(params.p)) {
var error = new TypeError("Scrypt params object 'p' property is not an integer");
}
if (error) {
error.propertyName = "Scrypt parameters object";
error.propertyValue = params;
throw error;
}
}
var processParamsArguments = function(args) {
var error = undefined;
checkNumberOfArguments(args, "At least one argument is needed - the maxtime", 1);
// Set defaults (if necessary)
if (args[1] === undefined) args[1] = 0; //maxmem default to 0
if (args[2] === undefined) args[2] = 0.5; //max_memfrac default to 0.5
for(var i=0; i < Math.min(3, args.length); i++) {
var propertyName = (function() {
if (i === 0) return "maxtime";
if (i === 1) return "maxmem";
if (i === 2) return "max_memfrac";
})();
// All args must be of type number
if (!error && typeof args[i] !== "number") {
error = new TypeError(propertyName + " must be a number");
}
// Specific argument checks
if (!error) {
switch (i) {
case 0: //maxtime
if (args[0] <= 0) {
error = new RangeError(propertyName + " must be greater than 0");
}
break;
case 1: //maxmem
if (args[1] !== parseInt(args[1], 10)) {
error = new TypeError(propertyName + " must be an integer");
}
if (!error && args[1] < 0) {
error = new RangeError(propertyName + " must be greater than or equal to 0")
}
break;
case 2: //max_memfrac
if (args[2] < 0.0 || args[2] > 1.0) {
error = new RangeError(propertyName + " must be between 0.0 and 1.0 inclusive")
}
break;
}
}
// Throw error if necessary
if (error) {
error.propertyName = propertyName;
error.propertyValue = args[i];
throw error;
}
}
return args;
}
var processKDFArguments = function(args) {
checkNumberOfArguments(args, "At least two arguments are needed - the key and the Scrypt paramaters object", 2)
//
// Check key argument
//
if (typeof args[0] === "string")
// Convert string to buffer (if necessary)
args[0] = new Buffer(args[0]);
else if (!Buffer.isBuffer(args[0])) {
var error = new TypeError("Key type is incorrect: It can only be of type string or Buffer");
error.propertyName = "key";
error.propertyValue = args[0];
throw error;
}
//
// Check Scrypt Parameters object
//
checkScryptParametersObject(args[1])
return args;
}
var processVerifyArguments = function(args) {
checkNumberOfArguments(args, "At least two arguments are needed - the KDF and the key", 2);
//
// Check KDF
//
if (typeof args[0] === "string")
// Convert string to buffer (if necessary)
args[0] = new Buffer(args[0]);
else if (!Buffer.isBuffer(args[0])) {
var error = new TypeError("KDF type is incorrect: It can only be of type string or Buffer");
error.propertyName = "KDF";
error.propertyValue = args[0];
throw error;
}
//
// Check Key
//
if (typeof args[1] === "string")
// Convert string to buffer (if necessary)
args[1] = new Buffer(args[1]);
else if (!Buffer.isBuffer(args[1])) {
var error = new TypeError("Key type is incorrect: It can only be of type string or Buffer");
error.propertyName = "key";
error.propertyValue = args[1];
throw error;
}
return args;
}
var processHashArguments = function(args) {
checkNumberOfArguments(args, "At least four arguments are needed - the key to hash, the scrypt params object, the output length of the hash and the salt", 4);
//
// Check Key
//
if (typeof args[0] === "string")
// Convert string to buffer (if necessary)
args[0] = new Buffer(args[0]);
else if (!Buffer.isBuffer(args[0])) {
var error = new TypeError("Key type is incorrect: It can only be of type string or Buffer");
error.propertyName = "KDF";
error.propertyValue = args[0];
throw error;
}
//
// Check Scrypt Parameters object
//
checkScryptParametersObject(args[1])
//
// Check the hash output length
//
if (typeof args[2] !== "number" || args[2] !== parseInt(args[2],10)) {
error = new TypeError("Hash length must be an integer");
throw error;
}
//
// Check Salt
//
if (typeof args[3] === "string")
// Convert string to buffer (if necessary)
args[3] = new Buffer(args[3]);
else if (!Buffer.isBuffer(args[3])) {
var error = new TypeError("Salt type is incorrect: It can only be of type string or Buffer");
error.propertyName = "salt";
error.propertyValue = args[3];
throw error;
}
return args;
}
//
// Scrypt Object
//
var scrypt = {
paramsSync: function() {
var args = processParamsArguments(arguments);
return scryptNative.paramsSync(args[0], args[1], args[2], Os.totalmem());
},
params: function() {
var args = arguments
, callback_index = checkAsyncArguments(args, 1, "At least one argument is needed before the callback - the maxtime");
if (callback_index === undefined) {
// Promise
return new Promise(function(resolve, reject) {
args = processParamsArguments(args);
scryptNative.params(args[0], args[1], args[2], Os.totalmem(), function(err, params) {
if (err) {
reject(err);
} else {
resolve(params);
}
});
})
} else {
// Normal async with callback
// If not using promise (so using callback),
// remove callback function from args and
// put it in it's own variable. This allows
// sync check to be used (DRY)
var callback = args[callback_index];
delete args[callback_index];
args = processParamsArguments(args);
args[3] = callback;
scryptNative.params(args[0], args[1], args[2], Os.totalmem(), args[3]);
}
},
kdfSync: function() {
var args = processKDFArguments(arguments);
return scryptNative.kdfSync(args[0], args[1], Crypto.randomBytes(256));
},
kdf: function() {
var args = arguments
, callback_index = checkAsyncArguments(args, 2, "At least two arguments are needed before the call back function - the key and the Scrypt parameters object")
, that = this;
args = processKDFArguments(args);
if (callback_index === undefined) { // promise
return new Promise(function(resolve, reject) {
// Get some async salt
Crypto.randomBytes(256, function(err, salt) {
if (err) reject(err);
else {
scryptNative.kdf(args[0], args[1], salt, function(err, kdfResult) {
if (err) {
reject(err);
} else {
resolve(kdfResult);
}
});
}
});
});
} else { // Normal async with callback
Crypto.randomBytes(256, function(err, salt) {
// Normal async with callback
if (err) // Crypto.randomBytes err
args[2](err); // call callback with error
else
scryptNative.kdf(args[0], args[1], salt, args[2]);
});
}
},
verifyKdfSync: function() {
var args = processVerifyArguments(arguments);
return scryptNative.verifySync(args[0], args[1]);
},
verifyKdf: function() {
var args = arguments
, callback_index = checkAsyncArguments(args, 2, "At least two arguments are needed before the callback function - the KDF and the key");
if (callback_index === undefined) {
// Promise
return new Promise(function(resolve, reject) {
args = processVerifyArguments(args);
scryptNative.verify(args[0], args[1], function(err, match) {
if (err) {
reject(err);
} else {
resolve(match);
}
});
})
} else {
// Normal async with callback
args = processVerifyArguments(args);
scryptNative.verify(args[0], args[1], args[2]);
}
},
hashSync: function() {
var args = processHashArguments(arguments);
return scryptNative.hashSync(args[0], args[1], args[2], args[3]);
},
hash: function() {
var args = arguments
, callback_index = checkAsyncArguments(args, 4, "At least four arguments are needed before the callback - the key to hash, the scrypt params object, the output length of the hash and the salt");
args = processHashArguments(args);
if (callback_index === undefined) {
//Promise
return new Promise(function(resolve, reject) {
scryptNative.hash(args[0], args[1], args[2], args[3], function(err, hash) {
if (err) {
reject(err);
} else {
resolve(hash);
}
});
});
} else {
// Normal async with callback
scryptNative.hash(args[0], args[1], args[2], args[3], args[4]);
}
}
};
module.exports = scrypt;