1 |
3a515b92
|
cagy
|
var MAX_ALLOC = Math.pow(2, 30) - 1 // default in iojs
|
2 |
|
|
|
3 |
|
|
function checkBuffer (buf, name) {
|
4 |
|
|
if (typeof buf !== 'string' && !Buffer.isBuffer(buf)) {
|
5 |
|
|
throw new TypeError(name + ' must be a buffer or string')
|
6 |
|
|
}
|
7 |
|
|
}
|
8 |
|
|
|
9 |
|
|
module.exports = function (password, salt, iterations, keylen) {
|
10 |
|
|
checkBuffer(password, 'Password')
|
11 |
|
|
checkBuffer(salt, 'Salt')
|
12 |
|
|
|
13 |
|
|
if (typeof iterations !== 'number') {
|
14 |
|
|
throw new TypeError('Iterations not a number')
|
15 |
|
|
}
|
16 |
|
|
|
17 |
|
|
if (iterations < 0) {
|
18 |
|
|
throw new TypeError('Bad iterations')
|
19 |
|
|
}
|
20 |
|
|
|
21 |
|
|
if (typeof keylen !== 'number') {
|
22 |
|
|
throw new TypeError('Key length not a number')
|
23 |
|
|
}
|
24 |
|
|
|
25 |
|
|
if (keylen < 0 || keylen > MAX_ALLOC || keylen !== keylen) { /* eslint no-self-compare: 0 */
|
26 |
|
|
throw new TypeError('Bad key length')
|
27 |
|
|
}
|
28 |
|
|
}
|