|
6 | 6 | // https://stackoverflow.com/questions/15593184/pbkdf2-hmac-sha-512-test-vectors
|
7 | 7 | var fixtures = require('./fixtures');
|
8 | 8 | var tape = require('tape');
|
| 9 | +var satisfies = require('semver').satisfies; |
9 | 10 | var Buffer = require('safe-buffer').Buffer;
|
10 | 11 |
|
11 | 12 | var pVersionMajor = parseInt(process.version.split('.')[0].slice(1), 10);
|
@@ -125,80 +126,89 @@ function runTests(name, compat) {
|
125 | 126 |
|
126 | 127 | var algos = ['sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'ripemd160'];
|
127 | 128 | algos.forEach(function (algorithm) {
|
128 |
| - fixtures.valid.forEach(function (f) { |
129 |
| - var key, keyType, salt, saltType; |
130 |
| - if (f.keyUint8Array) { |
131 |
| - key = new Uint8Array(f.keyUint8Array); |
132 |
| - keyType = 'Uint8Array'; |
133 |
| - } else if (f.keyInt32Array) { |
134 |
| - key = new Int32Array(f.keyInt32Array); |
135 |
| - keyType = 'Int32Array'; |
136 |
| - } else if (f.keyFloat64Array) { |
137 |
| - key = new Float64Array(f.keyFloat64Array); |
138 |
| - keyType = 'Float64Array'; |
139 |
| - } else if (f.keyHex) { |
140 |
| - key = Buffer.from(f.keyHex, 'hex'); |
141 |
| - keyType = 'hex'; |
142 |
| - } else { |
143 |
| - key = f.key; |
144 |
| - keyType = 'string'; |
145 |
| - } |
146 |
| - if (f.saltUint8Array) { |
147 |
| - salt = new Uint8Array(f.saltUint8Array); |
148 |
| - saltType = 'Uint8Array'; |
149 |
| - } else if (f.saltInt32Array) { |
150 |
| - salt = new Int32Array(f.saltInt32Array); |
151 |
| - saltType = 'Int32Array'; |
152 |
| - } else if (f.saltFloat64Array) { |
153 |
| - salt = new Float64Array(f.saltFloat64Array); |
154 |
| - saltType = 'Float64Array'; |
155 |
| - } else if (f.saltHex) { |
156 |
| - salt = Buffer.from(f.saltHex, 'hex'); |
157 |
| - saltType = 'hex'; |
158 |
| - } else { |
159 |
| - salt = f.salt; |
160 |
| - saltType = 'string'; |
161 |
| - } |
162 |
| - var expected = f.results[algorithm]; |
163 |
| - var description = algorithm + ' encodes "' + key + '" (' + keyType + ') with salt "' + salt + '" (' + saltType + ') with ' + algorithm + ' to ' + expected; |
164 |
| - |
165 |
| - tape(name + ' async w/ ' + description, function (t) { |
166 |
| - t.plan(2); |
167 |
| - |
168 |
| - compat.pbkdf2(key, salt, f.iterations, f.dkLen, algorithm, function (err, result) { |
169 |
| - t.error(err); |
170 |
| - t.equal(result.toString('hex'), expected); |
| 129 | + var isUnsupported = satisfies(process.version, '^17') && algorithm === 'ripemd160'; |
| 130 | + tape( |
| 131 | + name + ' + ' + algorithm, |
| 132 | + { skip: isUnsupported && 'this node version does not support ' + algorithm }, |
| 133 | + function (t) { |
| 134 | + fixtures.valid.forEach(function (f) { |
| 135 | + var key, keyType, salt, saltType; |
| 136 | + if (f.keyUint8Array) { |
| 137 | + key = new Uint8Array(f.keyUint8Array); |
| 138 | + keyType = 'Uint8Array'; |
| 139 | + } else if (f.keyInt32Array) { |
| 140 | + key = new Int32Array(f.keyInt32Array); |
| 141 | + keyType = 'Int32Array'; |
| 142 | + } else if (f.keyFloat64Array) { |
| 143 | + key = new Float64Array(f.keyFloat64Array); |
| 144 | + keyType = 'Float64Array'; |
| 145 | + } else if (f.keyHex) { |
| 146 | + key = Buffer.from(f.keyHex, 'hex'); |
| 147 | + keyType = 'hex'; |
| 148 | + } else { |
| 149 | + key = f.key; |
| 150 | + keyType = 'string'; |
| 151 | + } |
| 152 | + if (f.saltUint8Array) { |
| 153 | + salt = new Uint8Array(f.saltUint8Array); |
| 154 | + saltType = 'Uint8Array'; |
| 155 | + } else if (f.saltInt32Array) { |
| 156 | + salt = new Int32Array(f.saltInt32Array); |
| 157 | + saltType = 'Int32Array'; |
| 158 | + } else if (f.saltFloat64Array) { |
| 159 | + salt = new Float64Array(f.saltFloat64Array); |
| 160 | + saltType = 'Float64Array'; |
| 161 | + } else if (f.saltHex) { |
| 162 | + salt = Buffer.from(f.saltHex, 'hex'); |
| 163 | + saltType = 'hex'; |
| 164 | + } else { |
| 165 | + salt = f.salt; |
| 166 | + saltType = 'string'; |
| 167 | + } |
| 168 | + var expected = f.results[algorithm]; |
| 169 | + var description = algorithm + ' encodes "' + key + '" (' + keyType + ') with salt "' + salt + '" (' + saltType + ') with ' + algorithm + ' to ' + expected; |
| 170 | + |
| 171 | + t.test(name + ' async w/ ' + description, function (st) { |
| 172 | + st.plan(2); |
| 173 | + |
| 174 | + compat.pbkdf2(key, salt, f.iterations, f.dkLen, algorithm, function (err, result) { |
| 175 | + st.error(err); |
| 176 | + st.equal(result.toString('hex'), expected); |
| 177 | + }); |
| 178 | + }); |
| 179 | + |
| 180 | + t.test(name + 'sync w/ ' + description, function (st) { |
| 181 | + st.plan(1); |
| 182 | + |
| 183 | + var result = compat.pbkdf2Sync(key, salt, f.iterations, f.dkLen, algorithm); |
| 184 | + st.equal(result.toString('hex'), expected); |
| 185 | + }); |
171 | 186 | });
|
172 |
| - }); |
173 |
| - |
174 |
| - tape(name + 'sync w/ ' + description, function (t) { |
175 |
| - t.plan(1); |
176 | 187 |
|
177 |
| - var result = compat.pbkdf2Sync(key, salt, f.iterations, f.dkLen, algorithm); |
178 |
| - t.equal(result.toString('hex'), expected); |
179 |
| - }); |
180 |
| - }); |
| 188 | + fixtures.invalid.forEach(function (f) { |
| 189 | + var description = algorithm + ' should throw ' + f.exception; |
| 190 | + |
| 191 | + t.test(name + ' async w/ ' + description, function (st) { |
| 192 | + st.plan(1); |
| 193 | + /* istanbul ignore next */ |
| 194 | + function noop() {} |
| 195 | + st['throws'](function () { |
| 196 | + compat.pbkdf2(f.key, f.salt, f.iterations, f.dkLen, f.algo, noop); |
| 197 | + }, new RegExp(f.exception)); |
| 198 | + }); |
| 199 | + |
| 200 | + t.test(name + ' sync w/' + description, function (st) { |
| 201 | + st.plan(1); |
| 202 | + |
| 203 | + st['throws'](function () { |
| 204 | + compat.pbkdf2Sync(f.key, f.salt, f.iterations, f.dkLen, f.algo); |
| 205 | + }, new RegExp(f.exception)); |
| 206 | + }); |
| 207 | + }); |
181 | 208 |
|
182 |
| - fixtures.invalid.forEach(function (f) { |
183 |
| - var description = algorithm + ' should throw ' + f.exception; |
184 |
| - |
185 |
| - tape(name + ' async w/ ' + description, function (t) { |
186 |
| - t.plan(1); |
187 |
| - /* istanbul ignore next */ |
188 |
| - function noop() {} |
189 |
| - t['throws'](function () { |
190 |
| - compat.pbkdf2(f.key, f.salt, f.iterations, f.dkLen, f.algo, noop); |
191 |
| - }, new RegExp(f.exception)); |
192 |
| - }); |
193 |
| - |
194 |
| - tape(name + ' sync w/' + description, function (t) { |
195 |
| - t.plan(1); |
196 |
| - |
197 |
| - t['throws'](function () { |
198 |
| - compat.pbkdf2Sync(f.key, f.salt, f.iterations, f.dkLen, f.algo); |
199 |
| - }, new RegExp(f.exception)); |
200 |
| - }); |
201 |
| - }); |
| 209 | + t.end(); |
| 210 | + } |
| 211 | + ); |
202 | 212 | });
|
203 | 213 | }
|
204 | 214 |
|
|
0 commit comments