Skip to content

Commit 0b77ea9

Browse files
committed
Build type check into toBuffer
1 parent 4a4deed commit 0b77ea9

File tree

7 files changed

+37
-39
lines changed

7 files changed

+37
-39
lines changed

index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
var checkParameters = require('./lib/precondition')
21
var native = require('crypto')
32

3+
var checkParameters = require('./lib/precondition')
4+
var defaultEncoding = require('./lib/default-encoding')
5+
var toBuffer = require('./lib/to-buffer')
6+
47
function nativePBKDF2 (password, salt, iterations, keylen, digest, callback) {
5-
checkParameters(password, salt, iterations, keylen)
8+
checkParameters(iterations, keylen)
9+
password = toBuffer(password, defaultEncoding, 'Password')
10+
salt = toBuffer(salt, defaultEncoding, 'Salt')
611

712
if (typeof digest === 'function') {
813
callback = digest
@@ -14,7 +19,9 @@ function nativePBKDF2 (password, salt, iterations, keylen, digest, callback) {
1419
}
1520

1621
function nativePBKDF2Sync (password, salt, iterations, keylen, digest) {
17-
checkParameters(password, salt, iterations, keylen)
22+
checkParameters(iterations, keylen)
23+
password = toBuffer(password, defaultEncoding, 'Password')
24+
salt = toBuffer(salt, defaultEncoding, 'Salt')
1825
digest = digest || 'sha1'
1926
return native.pbkdf2Sync(password, salt, iterations, keylen, digest)
2027
}

lib/async.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
var Buffer = require('safe-buffer').Buffer
2+
13
var checkParameters = require('./precondition')
24
var defaultEncoding = require('./default-encoding')
35
var sync = require('./sync')
46
var toBuffer = require('./to-buffer')
5-
var Buffer = require('safe-buffer').Buffer
67

78
var ZERO_BUF
89
var subtle = global.crypto && global.crypto.subtle
@@ -88,10 +89,10 @@ module.exports = function (password, salt, iterations, keylen, digest, callback)
8889
})
8990
}
9091

91-
checkParameters(password, salt, iterations, keylen)
92+
checkParameters(iterations, keylen)
93+
password = toBuffer(password, defaultEncoding, 'Password')
94+
salt = toBuffer(salt, defaultEncoding, 'Salt')
9295
if (typeof callback !== 'function') throw new Error('No callback provided to pbkdf2')
93-
password = toBuffer(password, defaultEncoding)
94-
salt = toBuffer(salt, defaultEncoding)
9596

9697
resolvePromise(checkNative(algo).then(function (resp) {
9798
if (resp) return browserPbkdf2(password, salt, iterations, keylen, algo)

lib/precondition.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
var MAX_ALLOC = Math.pow(2, 30) - 1 // default in iojs
22

3-
function checkType (variable, name) {
4-
// checks if the variable is a string or a typed array
5-
if (typeof variable !== 'string' && !ArrayBuffer.isView(variable)) {
6-
throw new TypeError(name + ' must be a string or an ArrayBuffer')
7-
}
8-
}
9-
10-
module.exports = function (password, salt, iterations, keylen) {
11-
checkType(password, 'Password')
12-
checkType(salt, 'Salt')
13-
3+
module.exports = function (iterations, keylen) {
144
if (typeof iterations !== 'number') {
155
throw new TypeError('Iterations not a number')
166
}

lib/sync-browser.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
var md5 = require('create-hash/md5')
22
var RIPEMD160 = require('ripemd160')
33
var sha = require('sha.js')
4-
var toBuffer = require('./to-buffer')
4+
var Buffer = require('safe-buffer').Buffer
55

66
var checkParameters = require('./precondition')
77
var defaultEncoding = require('./default-encoding')
8-
var Buffer = require('safe-buffer').Buffer
8+
var toBuffer = require('./to-buffer')
9+
910
var ZEROS = Buffer.alloc(128)
1011
var sizes = {
1112
md5: 16,
@@ -67,10 +68,9 @@ function getDigest (alg) {
6768
}
6869

6970
function pbkdf2 (password, salt, iterations, keylen, digest) {
70-
checkParameters(password, salt, iterations, keylen)
71-
72-
password = toBuffer(password, defaultEncoding)
73-
salt = toBuffer(salt, defaultEncoding)
71+
checkParameters(iterations, keylen)
72+
password = toBuffer(password, defaultEncoding, 'Password')
73+
salt = toBuffer(salt, defaultEncoding, 'Salt')
7474

7575
digest = digest || 'sha1'
7676

lib/sync.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ var sizes = {
1010
}
1111

1212
var createHmac = require('create-hmac')
13-
var checkParameters = require('../lib/precondition')
14-
var defaultEncoding = require('../lib/default-encoding')
1513
var Buffer = require('safe-buffer').Buffer
14+
15+
var checkParameters = require('./precondition')
16+
var defaultEncoding = require('./default-encoding')
1617
var toBuffer = require('./to-buffer')
1718

1819
function pbkdf2 (password, salt, iterations, keylen, digest) {
19-
checkParameters(password, salt, iterations, keylen)
20-
21-
password = toBuffer(password, defaultEncoding)
22-
salt = toBuffer(salt, defaultEncoding)
20+
checkParameters(iterations, keylen)
21+
password = toBuffer(password, defaultEncoding, 'Password')
22+
salt = toBuffer(salt, defaultEncoding, 'Salt')
2323

2424
digest = digest || 'sha1'
2525

lib/to-buffer.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
var Buffer = require('safe-buffer').Buffer
22

3-
module.exports = (thing, encoding) => {
3+
module.exports = (thing, encoding, name) => {
44
if (Buffer.isBuffer(thing)) {
55
return thing
6-
}
7-
if (typeof thing === 'string') {
6+
} else if (typeof thing === 'string') {
87
return Buffer.from(thing, encoding)
9-
}
10-
if (ArrayBuffer.isView(thing)) {
8+
} else if (ArrayBuffer.isView(thing)) {
119
return Buffer.from(thing.buffer)
10+
} else {
11+
throw new TypeError(name + ' must be a string, a Buffer, a typed array or a DataView')
1212
}
1313
}

test/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,23 +102,23 @@ function runTests (name, compat) {
102102

103103
t.throws(function () {
104104
compat.pbkdf2(['a'], 'salt', 1, 32, 'sha1', function () {})
105-
}, /Password must be a string or an ArrayBuffer/)
105+
}, /Password must be a string, a Buffer, a typed array or a DataView/)
106106

107107
t.throws(function () {
108108
compat.pbkdf2Sync(['a'], 'salt', 1, 32, 'sha1')
109-
}, /Password must be a string or an ArrayBuffer/)
109+
}, /Password must be a string, a Buffer, a typed array or a DataView/)
110110
})
111111

112112
tape(name + ' should throw if the salt is not a string or an ArrayBuffer', function (t) {
113113
t.plan(2)
114114

115115
t.throws(function () {
116116
compat.pbkdf2('pass', ['salt'], 1, 32, 'sha1')
117-
}, /Salt must be a string or an ArrayBuffer/)
117+
}, /Salt must be a string, a Buffer, a typed array or a DataView/)
118118

119119
t.throws(function () {
120120
compat.pbkdf2Sync('pass', ['salt'], 1, 32, 'sha1')
121-
}, /Salt must be a string or an ArrayBuffer/)
121+
}, /Salt must be a string, a Buffer, a typed array or a DataView/)
122122
})
123123

124124
var algos = ['sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'ripemd160']

0 commit comments

Comments
 (0)