Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 61e9396

Browse filesBrowse 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 <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>
1 parent e81bb9f commit 61e9396
Copy full SHA for 61e9396

File tree

Expand file treeCollapse file tree

2 files changed

+18
-33
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+18
-33
lines changed
Open diff view settings
Collapse file

‎lib/internal/crypto/sig.js‎

Copy file name to clipboardExpand all lines: lib/internal/crypto/sig.js
+6-33Lines changed: 6 additions & 33 deletions
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
};
Collapse file

‎lib/internal/crypto/util.js‎

Copy file name to clipboardExpand all lines: lib/internal/crypto/util.js
+12Lines changed: 12 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.