mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 13:48:44 +02:00
test: refactor test-crypto-pbkdf2
PR-URL: https://github.com/nodejs/node/pull/35693 Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
parent
3d9310d72b
commit
9e227d5ae2
1 changed files with 27 additions and 53 deletions
|
@ -6,19 +6,26 @@ if (!common.hasCrypto)
|
|||
const assert = require('assert');
|
||||
const crypto = require('crypto');
|
||||
|
||||
function runPBKDF2(password, salt, iterations, keylen, hash) {
|
||||
const syncResult =
|
||||
crypto.pbkdf2Sync(password, salt, iterations, keylen, hash);
|
||||
|
||||
crypto.pbkdf2(password, salt, iterations, keylen, hash,
|
||||
common.mustSucceed((asyncResult) => {
|
||||
assert.deepStrictEqual(asyncResult, syncResult);
|
||||
}));
|
||||
|
||||
return syncResult;
|
||||
}
|
||||
|
||||
function testPBKDF2(password, salt, iterations, keylen, expected, encoding) {
|
||||
const actual = runPBKDF2(password, salt, iterations, keylen, 'sha256');
|
||||
assert.strictEqual(actual.toString(encoding || 'latin1'), expected);
|
||||
}
|
||||
|
||||
//
|
||||
// Test PBKDF2 with RFC 6070 test vectors (except #4)
|
||||
//
|
||||
function testPBKDF2(password, salt, iterations, keylen, expected) {
|
||||
const actual =
|
||||
crypto.pbkdf2Sync(password, salt, iterations, keylen, 'sha256');
|
||||
assert.strictEqual(actual.toString('latin1'), expected);
|
||||
|
||||
crypto.pbkdf2(password, salt, iterations, keylen, 'sha256', (err, actual) => {
|
||||
assert.strictEqual(actual.toString('latin1'), expected);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
testPBKDF2('password', 'salt', 1, 20,
|
||||
'\x12\x0f\xb6\xcf\xfc\xf8\xb3\x2c\x43\xe7\x22\x52' +
|
||||
|
@ -43,15 +50,9 @@ testPBKDF2('pass\0word', 'sa\0lt', 4096, 16,
|
|||
'\x89\xb6\x9d\x05\x16\xf8\x29\x89\x3c\x69\x62\x26\x65' +
|
||||
'\x0a\x86\x87');
|
||||
|
||||
const expected =
|
||||
'64c486c55d30d4c5a079b8823b7d7cb37ff0556f537da8410233bcec330ed956';
|
||||
const key = crypto.pbkdf2Sync('password', 'salt', 32, 32, 'sha256');
|
||||
assert.strictEqual(key.toString('hex'), expected);
|
||||
|
||||
crypto.pbkdf2('password', 'salt', 32, 32, 'sha256', common.mustSucceed(ondone));
|
||||
function ondone(key) {
|
||||
assert.strictEqual(key.toString('hex'), expected);
|
||||
}
|
||||
testPBKDF2('password', 'salt', 32, 32,
|
||||
'64c486c55d30d4c5a079b8823b7d7cb37ff0556f537da8410233bcec330ed956',
|
||||
'hex');
|
||||
|
||||
// Error path should not leak memory (check with valgrind).
|
||||
assert.throws(
|
||||
|
@ -197,38 +198,12 @@ assert.throws(
|
|||
);
|
||||
});
|
||||
|
||||
// Any TypedArray should work for password and salt
|
||||
crypto.pbkdf2(new Uint8Array(10), 'salt', 8, 8, 'sha256', common.mustSucceed());
|
||||
crypto.pbkdf2('pass', new Uint8Array(10), 8, 8, 'sha256', common.mustSucceed());
|
||||
crypto.pbkdf2(new Uint16Array(10), 'salt', 8, 8, 'sha1', common.mustSucceed());
|
||||
crypto.pbkdf2('pass', new Uint16Array(10), 8, 8, 'sha1', common.mustSucceed());
|
||||
crypto.pbkdf2(new Uint32Array(10), 'salt', 8, 8, 'sha1', common.mustSucceed());
|
||||
crypto.pbkdf2('pass', new Uint32Array(10), 8, 8, 'sha1', common.mustSucceed());
|
||||
crypto.pbkdf2(new Float32Array(10), 'salt', 8, 8, 'sha1', common.mustSucceed());
|
||||
crypto.pbkdf2('pass', new Float32Array(10), 8, 8, 'sha1', common.mustSucceed());
|
||||
crypto.pbkdf2(new Float64Array(10), 'salt', 8, 8, 'sha1', common.mustSucceed());
|
||||
crypto.pbkdf2('pass', new Float64Array(10), 8, 8, 'sha1', common.mustSucceed());
|
||||
crypto.pbkdf2(new ArrayBuffer(10), 'salt', 8, 8, 'sha1', common.mustSucceed());
|
||||
crypto.pbkdf2('pass', new ArrayBuffer(10), 8, 8, 'sha1', common.mustSucceed());
|
||||
crypto.pbkdf2(new SharedArrayBuffer(10), 'salt', 8, 8, 'sha256',
|
||||
common.mustSucceed());
|
||||
crypto.pbkdf2('pass', new SharedArrayBuffer(10), 8, 8, 'sha256',
|
||||
common.mustSucceed());
|
||||
|
||||
crypto.pbkdf2Sync(new Uint8Array(10), 'salt', 8, 8, 'sha256');
|
||||
crypto.pbkdf2Sync('pass', new Uint8Array(10), 8, 8, 'sha256');
|
||||
crypto.pbkdf2Sync(new Uint16Array(10), 'salt', 8, 8, 'sha256');
|
||||
crypto.pbkdf2Sync('pass', new Uint16Array(10), 8, 8, 'sha256');
|
||||
crypto.pbkdf2Sync(new Uint32Array(10), 'salt', 8, 8, 'sha256');
|
||||
crypto.pbkdf2Sync('pass', new Uint32Array(10), 8, 8, 'sha256');
|
||||
crypto.pbkdf2Sync(new Float32Array(10), 'salt', 8, 8, 'sha256');
|
||||
crypto.pbkdf2Sync('pass', new Float32Array(10), 8, 8, 'sha256');
|
||||
crypto.pbkdf2Sync(new Float64Array(10), 'salt', 8, 8, 'sha256');
|
||||
crypto.pbkdf2Sync('pass', new Float64Array(10), 8, 8, 'sha256');
|
||||
crypto.pbkdf2Sync(new ArrayBuffer(10), 'salt', 8, 8, 'sha256');
|
||||
crypto.pbkdf2Sync('pass', new ArrayBuffer(10), 8, 8, 'sha256');
|
||||
crypto.pbkdf2Sync(new SharedArrayBuffer(10), 'salt', 8, 8, 'sha256');
|
||||
crypto.pbkdf2Sync('pass', new SharedArrayBuffer(10), 8, 8, 'sha256');
|
||||
// Any TypedArray should work for password and salt.
|
||||
for (const SomeArray of [Uint8Array, Uint16Array, Uint32Array, Float32Array,
|
||||
Float64Array, ArrayBuffer, SharedArrayBuffer]) {
|
||||
runPBKDF2(new SomeArray(10), 'salt', 8, 8, 'sha256');
|
||||
runPBKDF2('pass', new SomeArray(10), 8, 8, 'sha256');
|
||||
}
|
||||
|
||||
assert.throws(
|
||||
() => crypto.pbkdf2('pass', 'salt', 8, 8, 'md55', common.mustNotCall()),
|
||||
|
@ -252,6 +227,5 @@ const kNotPBKDF2Supported = ['shake128', 'shake256'];
|
|||
crypto.getHashes()
|
||||
.filter((hash) => !kNotPBKDF2Supported.includes(hash))
|
||||
.forEach((hash) => {
|
||||
crypto.pbkdf2Sync(new Uint8Array(10), 'salt', 8, 8, hash);
|
||||
crypto.pbkdf2(new Uint8Array(10), 'salt', 8, 8, hash, common.mustCall());
|
||||
runPBKDF2(new Uint8Array(10), 'salt', 8, 8, hash);
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue