From e17280e8c94e18740909e66081dae0801114e2f8 Mon Sep 17 00:00:00 2001 From: Daniel Bevenius Date: Wed, 25 Apr 2018 09:11:37 +0200 Subject: [PATCH] crypto: make pbkdf2 use checkIsArrayBufferView MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit updates pbkdf2 to use checkIsArrayBufferView from internal/crypto/util. PR-URL: https://github.com/nodejs/node/pull/20251 Reviewed-By: Ben Noordhuis Reviewed-By: Joyee Cheung Reviewed-By: Tobias Nießen Reviewed-By: Colin Ihrig Reviewed-By: Trivikram Kamat Reviewed-By: James M Snell Reviewed-By: Tiancheng "Timothy" Gu --- lib/internal/crypto/pbkdf2.js | 17 +++-------------- test/parallel/test-crypto-pbkdf2.js | 3 ++- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/lib/internal/crypto/pbkdf2.js b/lib/internal/crypto/pbkdf2.js index 4a7f26b509b521..82ea9feb852649 100644 --- a/lib/internal/crypto/pbkdf2.js +++ b/lib/internal/crypto/pbkdf2.js @@ -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'); @@ -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); diff --git a/test/parallel/test-crypto-pbkdf2.js b/test/parallel/test-crypto-pbkdf2.js index b4ecd3c6061158..f65176132ae5c4 100644 --- a/test/parallel/test-crypto-pbkdf2.js +++ b/test/parallel/test-crypto-pbkdf2.js @@ -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()), {