Skip to content

Commit

Permalink
crypto: make pbkdf2 use checkIsArrayBufferView
Browse files Browse the repository at this point in the history
This commit updates pbkdf2 to use checkIsArrayBufferView from
internal/crypto/util.

PR-URL: #20251
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
  • Loading branch information
danbev authored and MylesBorins committed May 4, 2018
1 parent 61e9396 commit e17280e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 15 deletions.
17 changes: 3 additions & 14 deletions lib/internal/crypto/pbkdf2.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ const {
ERR_OUT_OF_RANGE
} = require('internal/errors').codes;
const {
checkIsArrayBufferView,
getDefaultEncoding,
toBuf
} = require('internal/crypto/util');
const { isArrayBufferView } = require('internal/util/types');
const {
PBKDF2
} = process.binding('crypto');
Expand Down Expand Up @@ -39,19 +39,8 @@ function _pbkdf2(password, salt, iterations, keylen, digest, callback) {
if (digest !== null && typeof digest !== 'string')
throw new ERR_INVALID_ARG_TYPE('digest', ['string', 'null'], digest);

password = toBuf(password);
salt = toBuf(salt);

if (!isArrayBufferView(password)) {
throw new ERR_INVALID_ARG_TYPE('password',
['string', 'Buffer', 'TypedArray'],
password);
}

if (!isArrayBufferView(salt)) {
throw new ERR_INVALID_ARG_TYPE('salt',
['string', 'Buffer', 'TypedArray'], salt);
}
password = checkIsArrayBufferView('password', toBuf(password));
salt = checkIsArrayBufferView('salt', toBuf(salt));

if (typeof iterations !== 'number')
throw new ERR_INVALID_ARG_TYPE('iterations', 'number', iterations);
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-crypto-pbkdf2.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ assert.throws(
});

[1, {}, [], true, undefined, null].forEach((input) => {
const msgPart2 = `Buffer, or TypedArray. Received type ${typeof input}`;
const msgPart2 = 'Buffer, TypedArray, or DataView.' +
` Received type ${typeof input}`;
assert.throws(
() => crypto.pbkdf2(input, 'salt', 8, 8, 'sha256', common.mustNotCall()),
{
Expand Down

0 comments on commit e17280e

Please sign in to comment.