Skip to content

Commit 61e9396

Browse files
danbevMylesBorins
authored andcommitted
crypto: add checkIsArrayBufferView
This commit adds a checkIsArrayBufferView function to avoid some code duplication in sig.js PR-URL: #20251 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
1 parent e81bb9f commit 61e9396

File tree

2 files changed

+18
-33
lines changed

2 files changed

+18
-33
lines changed

lib/internal/crypto/sig.js

+6-33
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ const {
1414
RSA_PKCS1_PADDING
1515
} = process.binding('constants').crypto;
1616
const {
17+
checkIsArrayBufferView,
1718
getDefaultEncoding,
1819
toBuf
1920
} = require('internal/crypto/util');
20-
const { isArrayBufferView } = require('internal/util/types');
2121
const { Writable } = require('stream');
2222
const { inherits } = require('util');
2323

@@ -41,14 +41,7 @@ Sign.prototype._write = function _write(chunk, encoding, callback) {
4141

4242
Sign.prototype.update = function update(data, encoding) {
4343
encoding = encoding || getDefaultEncoding();
44-
data = toBuf(data, encoding);
45-
if (!isArrayBufferView(data)) {
46-
throw new ERR_INVALID_ARG_TYPE(
47-
'data',
48-
['string', 'Buffer', 'TypedArray', 'DataView'],
49-
data
50-
);
51-
}
44+
data = checkIsArrayBufferView('data', toBuf(data, encoding));
5245
this._handle.update(data);
5346
return this;
5447
};
@@ -84,14 +77,7 @@ Sign.prototype.sign = function sign(options, encoding) {
8477

8578
var pssSaltLength = getSaltLength(options);
8679

87-
key = toBuf(key);
88-
if (!isArrayBufferView(key)) {
89-
throw new ERR_INVALID_ARG_TYPE(
90-
'key',
91-
['string', 'Buffer', 'TypedArray', 'DataView'],
92-
key
93-
);
94-
}
80+
key = checkIsArrayBufferView('key', toBuf(key));
9581

9682
var ret = this._handle.sign(key, passphrase, rsaPadding, pssSaltLength);
9783

@@ -128,23 +114,10 @@ Verify.prototype.verify = function verify(options, signature, sigEncoding) {
128114

129115
var pssSaltLength = getSaltLength(options);
130116

131-
key = toBuf(key);
132-
if (!isArrayBufferView(key)) {
133-
throw new ERR_INVALID_ARG_TYPE(
134-
'key',
135-
['string', 'Buffer', 'TypedArray', 'DataView'],
136-
key
137-
);
138-
}
117+
key = checkIsArrayBufferView('key', toBuf(key));
139118

140-
signature = toBuf(signature, sigEncoding);
141-
if (!isArrayBufferView(signature)) {
142-
throw new ERR_INVALID_ARG_TYPE(
143-
'signature',
144-
['string', 'Buffer', 'TypedArray', 'DataView'],
145-
signature
146-
);
147-
}
119+
signature = checkIsArrayBufferView('signature',
120+
toBuf(signature, sigEncoding));
148121

149122
return this._handle.verify(key, signature, rsaPadding, pssSaltLength);
150123
};

lib/internal/crypto/util.js

+12
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,19 @@ function timingSafeEqual(buf1, buf2) {
8383
return _timingSafeEqual(buf1, buf2);
8484
}
8585

86+
function checkIsArrayBufferView(name, buffer) {
87+
if (!isArrayBufferView(buffer)) {
88+
throw new ERR_INVALID_ARG_TYPE(
89+
name,
90+
['string', 'Buffer', 'TypedArray', 'DataView'],
91+
buffer
92+
);
93+
}
94+
return buffer;
95+
}
96+
8697
module.exports = {
98+
checkIsArrayBufferView,
8799
getCiphers,
88100
getCurves,
89101
getDefaultEncoding,

0 commit comments

Comments
 (0)