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 <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

2 files changed

Lines changed: 18 additions & 33 deletions

File tree

‎lib/internal/crypto/sig.js‎

Lines 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=functionupdate(data,encoding){
4343
encoding=encoding||getDefaultEncoding();
44-
data=toBuf(data,encoding);
45-
if(!isArrayBufferView(data)){
46-
thrownewERR_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
returnthis;
5447
};
@@ -84,14 +77,7 @@ Sign.prototype.sign = function sign(options, encoding) {
8477

8578
varpssSaltLength=getSaltLength(options);
8679

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

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

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

129115
varpssSaltLength=getSaltLength(options);
130116

131-
key=toBuf(key);
132-
if(!isArrayBufferView(key)){
133-
thrownewERR_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-
thrownewERR_INVALID_ARG_TYPE(
143-
'signature',
144-
['string','Buffer','TypedArray','DataView'],
145-
signature
146-
);
147-
}
119+
signature=checkIsArrayBufferView('signature',
120+
toBuf(signature,sigEncoding));
148121

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

‎lib/internal/crypto/util.js‎

Lines 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+
functioncheckIsArrayBufferView(name,buffer){
87+
if(!isArrayBufferView(buffer)){
88+
thrownewERR_INVALID_ARG_TYPE(
89+
name,
90+
['string','Buffer','TypedArray','DataView'],
91+
buffer
92+
);
93+
}
94+
returnbuffer;
95+
}
96+
8697
module.exports={
98+
checkIsArrayBufferView,
8799
getCiphers,
88100
getCurves,
89101
getDefaultEncoding,

0 commit comments

Comments
 (0)