Skip to content

Commit f61abc7

Browse files
committed
minor fixes for Uint8Array support
1 parent bca4182 commit f61abc7

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-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 of string, Buffer or Uint8Array
5-
if (typeof variable !== 'string' && !Buffer.isBuffer(variable) && Object.prototype.toString.call(variable) !== '[object Uint8Array]') {
6-
throw new TypeError(name + ' must be a buffer or string')
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')
77
}
88
}
99

test/fixtures.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@
156156
}
157157
},
158158
{
159-
"keyuint8": [112, 97, 115, 115, 119, 111, 114, 100],
159+
"keyUint8Array": [112, 97, 115, 115, 119, 111, 114, 100],
160160
"salt": "salt",
161161
"iterations": 1,
162162
"dkLen": 32,
@@ -170,7 +170,7 @@
170170
},
171171
{
172172
"key": "password",
173-
"saltuint8": [115, 97, 108, 116],
173+
"saltUint8Array": [115, 97, 108, 116],
174174
"iterations": 1,
175175
"dkLen": 32,
176176
"results": {

test/index.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,20 @@ function runTests (name, compat) {
124124
var algos = ['sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'ripemd160']
125125
algos.forEach(function (algorithm) {
126126
fixtures.valid.forEach(function (f) {
127-
if (f.keyuint8) {
128-
var key = new Uint8Array(f.keyuint8)
127+
var key, salt
128+
if (f.keyUint8Array) {
129+
key = new Uint8Array(f.keyuint8)
130+
} else if (f.keyHex) {
131+
key = Buffer.from(f.keyHex, 'hex')
129132
} else {
130-
var key = f.key || Buffer.from(f.keyHex, 'hex')
133+
key = f.key
131134
}
132-
if (f.saltuint8) {
133-
var salt = new Uint8Array(f.saltuint8)
135+
if (f.saltUint8Array) {
136+
salt = new Uint8Array(f.saltuint8)
137+
} else if (f.saltHex) {
138+
salt = Buffer.from(f.saltHex, 'hex')
134139
} else {
135-
var salt = f.salt || Buffer.from(f.saltHex, 'hex')
140+
salt = f.salt
136141
}
137142
var expected = f.results[algorithm]
138143
var description = algorithm + ' encodes ' + key + ' (' + f.salt + ') with ' + algorithm + ' to ' + expected

0 commit comments

Comments
 (0)