Skip to content

Commit 93ffd51

Browse files
committed
move checkParameters to separate file
1 parent 931b7d5 commit 93ffd51

File tree

4 files changed

+25
-23
lines changed

4 files changed

+25
-23
lines changed

browser.js

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,13 @@
11
var createHmac = require('create-hmac')
2-
3-
var MAX_ALLOC = Math.pow(2, 30) - 1 // default in iojs
4-
exports._checkParameters = function (iterations, keylen) {
5-
if (typeof iterations !== 'number') {
6-
throw new TypeError('Iterations not a number')
7-
}
8-
9-
if (iterations < 0) {
10-
throw new TypeError('Bad iterations')
11-
}
12-
13-
if (typeof keylen !== 'number') {
14-
throw new TypeError('Key length not a number')
15-
}
16-
17-
if (keylen < 0 || keylen > MAX_ALLOC || keylen !== keylen) { /* eslint no-self-compare: 0 */
18-
throw new TypeError('Bad key length')
19-
}
20-
}
2+
var checkParameters = require('./precondition')
213

224
exports.pbkdf2 = function (password, salt, iterations, keylen, digest, callback) {
235
if (typeof digest === 'function') {
246
callback = digest
257
digest = undefined
268
}
279

28-
exports._checkParameters(iterations, keylen)
10+
checkParameters(iterations, keylen)
2911
if (typeof callback !== 'function') throw new Error('No callback provided to pbkdf2')
3012

3113
setTimeout(function () {
@@ -37,7 +19,7 @@ exports.pbkdf2Sync = function (password, salt, iterations, keylen, digest) {
3719
if (!Buffer.isBuffer(password)) password = new Buffer(password, 'binary')
3820
if (!Buffer.isBuffer(salt)) salt = new Buffer(salt, 'binary')
3921

40-
exports._checkParameters(iterations, keylen)
22+
checkParameters(iterations, keylen)
4123

4224
digest = digest || 'sha1'
4325

node-shim.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var crypto = require('crypto')
22
var fork = require('child_process').fork
33
var path = require('path')
44
var browser = require('./browser')
5+
var checkParameters = require('./precondition')
56

67
var isShimRequired = null
78
exports.isShimRequired = function () {
@@ -30,7 +31,7 @@ exports.pbkdf2 = function pbkdf2 (password, salt, iterations, keylen, digest, ca
3031
if (!Buffer.isBuffer(password)) password = new Buffer(password, 'binary')
3132
if (!Buffer.isBuffer(salt)) salt = new Buffer(salt, 'binary')
3233

33-
browser._checkParameters(iterations, keylen)
34+
checkParameters(iterations, keylen)
3435
var child = fork(path.resolve(__dirname, 'async-shim.js'))
3536

3637
child.on('message', function (result) {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"async-shim.js",
88
"browser.js",
99
"index.js",
10-
"node-shim.js"
10+
"node-shim.js",
11+
"precondition.js"
1112
],
1213
"browser": "./browser.js",
1314
"keywords": [

precondition.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var MAX_ALLOC = Math.pow(2, 30) - 1 // default in iojs
2+
module.exports = function (iterations, keylen) {
3+
if (typeof iterations !== 'number') {
4+
throw new TypeError('Iterations not a number')
5+
}
6+
7+
if (iterations < 0) {
8+
throw new TypeError('Bad iterations')
9+
}
10+
11+
if (typeof keylen !== 'number') {
12+
throw new TypeError('Key length not a number')
13+
}
14+
15+
if (keylen < 0 || keylen > MAX_ALLOC || keylen !== keylen) { /* eslint no-self-compare: 0 */
16+
throw new TypeError('Bad key length')
17+
}
18+
}

0 commit comments

Comments
 (0)