Skip to content

Commit b888820

Browse files
committed
add support for all ArrayBuffers
1 parent ab62504 commit b888820

File tree

3 files changed

+85
-11
lines changed

3 files changed

+85
-11
lines changed

lib/precondition.js

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

33
function checkType (variable, name) {
4-
// checks whether the type of 'variable' is one string or Uint8Array (including Buffer)
5-
if (typeof variable !== 'string' && Object.prototype.toString.call(variable) !== '[object Uint8Array]') {
6-
throw new TypeError(name + ' must be a Buffer, Uint8Array or string')
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')
77
}
88
}
99

test/fixtures.json

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,62 @@
182182
"sha384": "c0e14f06e49e32d73f9f52ddf1d0c5c7191609233631dadd76a567db42b78676",
183183
"ripemd160": "b725258b125e0bacb0e2307e34feb16a4d0d6aed6cb4b0eee458fc1829020428"
184184
}
185+
},
186+
{
187+
"keyInt32Array": [112, 97, 115, 115, 119, 111, 114, 100],
188+
"salt": "salt",
189+
"iterations": 1,
190+
"dkLen": 32,
191+
"results": {
192+
"sha1": "0c60c80f961f0e71f3a9b524af6012062fe037a6e0f0eb94fe8fc46bdc637164",
193+
"sha256": "120fb6cffcf8b32c43e7225256c4f837a86548c92ccc35480805987cb70be17b",
194+
"sha512": "867f70cf1ade02cff3752599a3a53dc4af34c7a669815ae5d513554e1c8cf252",
195+
"sha224": "3c198cbdb9464b7857966bd05b7bc92bc1cc4e6e63155d4e490557fd85989497",
196+
"sha384": "c0e14f06e49e32d73f9f52ddf1d0c5c7191609233631dadd76a567db42b78676",
197+
"ripemd160": "b725258b125e0bacb0e2307e34feb16a4d0d6aed6cb4b0eee458fc1829020428"
198+
}
199+
},
200+
{
201+
"key": "password",
202+
"saltInt32Array": [115, 97, 108, 116],
203+
"iterations": 1,
204+
"dkLen": 32,
205+
"results": {
206+
"sha1": "0c60c80f961f0e71f3a9b524af6012062fe037a6e0f0eb94fe8fc46bdc637164",
207+
"sha256": "120fb6cffcf8b32c43e7225256c4f837a86548c92ccc35480805987cb70be17b",
208+
"sha512": "867f70cf1ade02cff3752599a3a53dc4af34c7a669815ae5d513554e1c8cf252",
209+
"sha224": "3c198cbdb9464b7857966bd05b7bc92bc1cc4e6e63155d4e490557fd85989497",
210+
"sha384": "c0e14f06e49e32d73f9f52ddf1d0c5c7191609233631dadd76a567db42b78676",
211+
"ripemd160": "b725258b125e0bacb0e2307e34feb16a4d0d6aed6cb4b0eee458fc1829020428"
212+
}
213+
},
214+
{
215+
"keyFloat64Array": [112, 97, 115, 115, 119, 111, 114, 100],
216+
"salt": "salt",
217+
"iterations": 1,
218+
"dkLen": 32,
219+
"results": {
220+
"sha1": "0c60c80f961f0e71f3a9b524af6012062fe037a6e0f0eb94fe8fc46bdc637164",
221+
"sha256": "120fb6cffcf8b32c43e7225256c4f837a86548c92ccc35480805987cb70be17b",
222+
"sha512": "867f70cf1ade02cff3752599a3a53dc4af34c7a669815ae5d513554e1c8cf252",
223+
"sha224": "3c198cbdb9464b7857966bd05b7bc92bc1cc4e6e63155d4e490557fd85989497",
224+
"sha384": "c0e14f06e49e32d73f9f52ddf1d0c5c7191609233631dadd76a567db42b78676",
225+
"ripemd160": "b725258b125e0bacb0e2307e34feb16a4d0d6aed6cb4b0eee458fc1829020428"
226+
}
227+
},
228+
{
229+
"key": "password",
230+
"saltFloat64Array": [115, 97, 108, 116],
231+
"iterations": 1,
232+
"dkLen": 32,
233+
"results": {
234+
"sha1": "0c60c80f961f0e71f3a9b524af6012062fe037a6e0f0eb94fe8fc46bdc637164",
235+
"sha256": "120fb6cffcf8b32c43e7225256c4f837a86548c92ccc35480805987cb70be17b",
236+
"sha512": "867f70cf1ade02cff3752599a3a53dc4af34c7a669815ae5d513554e1c8cf252",
237+
"sha224": "3c198cbdb9464b7857966bd05b7bc92bc1cc4e6e63155d4e490557fd85989497",
238+
"sha384": "c0e14f06e49e32d73f9f52ddf1d0c5c7191609233631dadd76a567db42b78676",
239+
"ripemd160": "b725258b125e0bacb0e2307e34feb16a4d0d6aed6cb4b0eee458fc1829020428"
240+
}
185241
}
186242
],
187243
"invalid": [

test/index.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,50 +97,68 @@ function runTests (name, compat) {
9797
}, /No callback provided to pbkdf2/)
9898
})
9999

100-
tape(name + ' should throw if the password is not a buffer or string or Uint8Array', function (t) {
100+
tape(name + ' should throw if the password is not a string or an ArrayBuffer', function (t) {
101101
t.plan(2)
102102

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

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

112-
tape(name + ' should throw if the salt is not a buffer or string or Uint8Array', function (t) {
112+
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 Buffer, Uint8Array or string/)
117+
}, /Salt must be a string or an ArrayBuffer/)
118118

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

124124
var algos = ['sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'ripemd160']
125125
algos.forEach(function (algorithm) {
126126
fixtures.valid.forEach(function (f) {
127-
var key, salt
127+
var key, keyType, salt, saltType
128128
if (f.keyUint8Array) {
129129
key = new Uint8Array(f.keyUint8Array)
130+
keyType = 'Uint8Array'
131+
} else if (f.keyInt32Array) {
132+
key = new Int32Array(f.keyInt32Array)
133+
keyType = 'Int32Array'
134+
} else if (f.keyFloat64Array) {
135+
key = new Float64Array(f.keyFloat64Array)
136+
keyType = 'Float64Array'
130137
} else if (f.keyHex) {
131138
key = Buffer.from(f.keyHex, 'hex')
139+
keyType = 'hex'
132140
} else {
133141
key = f.key
142+
keyType = 'string'
134143
}
135144
if (f.saltUint8Array) {
136145
salt = new Uint8Array(f.saltUint8Array)
146+
saltType = 'Uint8Array'
147+
} else if (f.saltInt32Array) {
148+
salt = new Int32Array(f.saltInt32Array)
149+
saltType = 'Int32Array'
150+
} else if (f.saltFloat64Array) {
151+
salt = new Float64Array(f.saltFloat64Array)
152+
saltType = 'Float64Array'
137153
} else if (f.saltHex) {
138154
salt = Buffer.from(f.saltHex, 'hex')
155+
saltType = 'hex'
139156
} else {
140157
salt = f.salt
158+
saltType = 'string'
141159
}
142160
var expected = f.results[algorithm]
143-
var description = algorithm + ' encodes ' + key + ' (' + f.salt + ') with ' + algorithm + ' to ' + expected
161+
var description = algorithm + ' encodes "' + key + '" (' + keyType + ') with salt "' + salt + '" (' + saltType + ') with ' + algorithm + ' to ' + expected
144162

145163
tape(name + ' async w/ ' + description, function (t) {
146164
t.plan(2)

0 commit comments

Comments
 (0)