Skip to content

Commit

Permalink
crypto: add checkIsArrayBufferView
Browse files Browse the repository at this point in the history
This commit adds a checkIsArrayBufferView function to avoid some code
duplication in sig.js

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 e81bb9f commit 61e9396
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 33 deletions.
39 changes: 6 additions & 33 deletions lib/internal/crypto/sig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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;
};
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
};
Expand Down
12 changes: 12 additions & 0 deletions lib/internal/crypto/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 61e9396

Please sign in to comment.