diff --git a/lib/internal/crypto/sig.js b/lib/internal/crypto/sig.js index 92b42412a82e92..1073b83d720098 100644 --- a/lib/internal/crypto/sig.js +++ b/lib/internal/crypto/sig.js @@ -14,10 +14,10 @@ const { RSA_PKCS1_PADDING } = process.binding('constants').crypto; const { + checkIsArrayBufferView, getDefaultEncoding, toBuf } = require('internal/crypto/util'); -const { isArrayBufferView } = require('internal/util/types'); const { Writable } = require('stream'); const { inherits } = require('util'); @@ -41,14 +41,7 @@ Sign.prototype._write = function _write(chunk, encoding, callback) { Sign.prototype.update = function update(data, encoding) { encoding = encoding || getDefaultEncoding(); - data = toBuf(data, encoding); - if (!isArrayBufferView(data)) { - throw new ERR_INVALID_ARG_TYPE( - 'data', - ['string', 'Buffer', 'TypedArray', 'DataView'], - data - ); - } + data = checkIsArrayBufferView('data', toBuf(data, encoding)); this._handle.update(data); return this; }; @@ -84,14 +77,7 @@ Sign.prototype.sign = function sign(options, encoding) { var pssSaltLength = getSaltLength(options); - key = toBuf(key); - if (!isArrayBufferView(key)) { - throw new ERR_INVALID_ARG_TYPE( - 'key', - ['string', 'Buffer', 'TypedArray', 'DataView'], - key - ); - } + key = checkIsArrayBufferView('key', toBuf(key)); var ret = this._handle.sign(key, passphrase, rsaPadding, pssSaltLength); @@ -128,23 +114,10 @@ Verify.prototype.verify = function verify(options, signature, sigEncoding) { var pssSaltLength = getSaltLength(options); - key = toBuf(key); - if (!isArrayBufferView(key)) { - throw new ERR_INVALID_ARG_TYPE( - 'key', - ['string', 'Buffer', 'TypedArray', 'DataView'], - key - ); - } + key = checkIsArrayBufferView('key', toBuf(key)); - signature = toBuf(signature, sigEncoding); - if (!isArrayBufferView(signature)) { - throw new ERR_INVALID_ARG_TYPE( - 'signature', - ['string', 'Buffer', 'TypedArray', 'DataView'], - signature - ); - } + signature = checkIsArrayBufferView('signature', + toBuf(signature, sigEncoding)); return this._handle.verify(key, signature, rsaPadding, pssSaltLength); }; diff --git a/lib/internal/crypto/util.js b/lib/internal/crypto/util.js index 095ca0478bd99f..59a5d57a1e4f39 100644 --- a/lib/internal/crypto/util.js +++ b/lib/internal/crypto/util.js @@ -83,7 +83,19 @@ function timingSafeEqual(buf1, buf2) { return _timingSafeEqual(buf1, buf2); } +function checkIsArrayBufferView(name, buffer) { + if (!isArrayBufferView(buffer)) { + throw new ERR_INVALID_ARG_TYPE( + name, + ['string', 'Buffer', 'TypedArray', 'DataView'], + buffer + ); + } + return buffer; +} + module.exports = { + checkIsArrayBufferView, getCiphers, getCurves, getDefaultEncoding,