Skip to content

Commit 72029b8

Browse files
danbevaddaleax
authored andcommitted
crypto: add createCipher/WithIV functions
This commit extracts the common code from the Cipher/Cipheriv and Decipher/Decipheriv constructors into a separate function to avoid code duplication. Backport-PR-URL: #20706 PR-URL: #20164 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent bdd2856 commit 72029b8

1 file changed

Lines changed: 46 additions & 83 deletions

File tree

‎lib/internal/crypto/cipher.js‎

Lines changed: 46 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,21 @@ function getUIntOption(options, key) {
7373
return-1;
7474
}
7575

76-
functionCipher(cipher,password,options){
77-
if(!(thisinstanceofCipher))
78-
returnnewCipher(cipher,password,options);
76+
functioncreateCipherBase(cipher,credential,options,decipher,iv){
77+
constauthTagLength=getUIntOption(options,'authTagLength');
78+
79+
this._handle=newCipherBase(decipher);
80+
if(iv===undefined){
81+
this._handle.init(cipher,credential,authTagLength);
82+
}else{
83+
this._handle.initiv(cipher,credential,iv,authTagLength);
84+
}
85+
this._decoder=null;
7986

87+
LazyTransform.call(this,options);
88+
}
89+
90+
functioncreateCipher(cipher,password,options,decipher){
8091
if(typeofcipher!=='string')
8192
thrownewERR_INVALID_ARG_TYPE('cipher','string',cipher);
8293

@@ -89,14 +100,38 @@ function Cipher(cipher, password, options) {
89100
);
90101
}
91102

92-
constauthTagLength=getUIntOption(options,'authTagLength');
103+
createCipherBase.call(this,cipher,password,options,decipher);
104+
}
93105

94-
this._handle=newCipherBase(true);
106+
functioncreateCipherWithIV(cipher,key,options,decipher,iv){
107+
if(typeofcipher!=='string')
108+
thrownewERR_INVALID_ARG_TYPE('cipher','string',cipher);
95109

96-
this._handle.init(cipher,password,authTagLength);
97-
this._decoder=null;
110+
key=toBuf(key);
111+
if(!isArrayBufferView(key)){
112+
thrownewERR_INVALID_ARG_TYPE(
113+
'key',
114+
['string','Buffer','TypedArray','DataView'],
115+
key
116+
);
117+
}
98118

99-
LazyTransform.call(this,options);
119+
iv=toBuf(iv);
120+
if(iv!==null&&!isArrayBufferView(iv)){
121+
thrownewERR_INVALID_ARG_TYPE(
122+
'iv',
123+
['string','Buffer','TypedArray','DataView'],
124+
iv
125+
);
126+
}
127+
createCipherBase.call(this,cipher,key,options,decipher,iv);
128+
}
129+
130+
functionCipher(cipher,password,options){
131+
if(!(thisinstanceofCipher))
132+
returnnewCipher(cipher,password,options);
133+
134+
createCipher.call(this,cipher,password,options,true);
100135
}
101136

102137
inherits(Cipher,LazyTransform);
@@ -198,34 +233,7 @@ function Cipheriv(cipher, key, iv, options) {
198233
if(!(thisinstanceofCipheriv))
199234
returnnewCipheriv(cipher,key,iv,options);
200235

201-
if(typeofcipher!=='string')
202-
thrownewERR_INVALID_ARG_TYPE('cipher','string',cipher);
203-
204-
key=toBuf(key);
205-
if(!isArrayBufferView(key)){
206-
thrownewERR_INVALID_ARG_TYPE(
207-
'key',
208-
['string','Buffer','TypedArray','DataView'],
209-
key
210-
);
211-
}
212-
213-
iv=toBuf(iv);
214-
if(iv!==null&&!isArrayBufferView(iv)){
215-
thrownewERR_INVALID_ARG_TYPE(
216-
'iv',
217-
['string','Buffer','TypedArray','DataView'],
218-
iv
219-
);
220-
}
221-
222-
constauthTagLength=getUIntOption(options,'authTagLength');
223-
224-
this._handle=newCipherBase(true);
225-
this._handle.initiv(cipher,key,iv,authTagLength);
226-
this._decoder=null;
227-
228-
LazyTransform.call(this,options);
236+
createCipherWithIV.call(this,cipher,key,options,true,iv);
229237
}
230238

231239
inherits(Cipheriv,LazyTransform);
@@ -248,25 +256,7 @@ function Decipher(cipher, password, options) {
248256
if(!(thisinstanceofDecipher))
249257
returnnewDecipher(cipher,password,options);
250258

251-
if(typeofcipher!=='string')
252-
thrownewERR_INVALID_ARG_TYPE('cipher','string',cipher);
253-
254-
password=toBuf(password);
255-
if(!isArrayBufferView(password)){
256-
thrownewERR_INVALID_ARG_TYPE(
257-
'password',
258-
['string','Buffer','TypedArray','DataView'],
259-
password
260-
);
261-
}
262-
263-
constauthTagLength=getUIntOption(options,'authTagLength');
264-
265-
this._handle=newCipherBase(false);
266-
this._handle.init(cipher,password,authTagLength);
267-
this._decoder=null;
268-
269-
LazyTransform.call(this,options);
259+
createCipher.call(this,cipher,password,options,false);
270260
}
271261

272262
inherits(Decipher,LazyTransform);
@@ -286,34 +276,7 @@ function Decipheriv(cipher, key, iv, options) {
286276
if(!(thisinstanceofDecipheriv))
287277
returnnewDecipheriv(cipher,key,iv,options);
288278

289-
if(typeofcipher!=='string')
290-
thrownewERR_INVALID_ARG_TYPE('cipher','string',cipher);
291-
292-
key=toBuf(key);
293-
if(!isArrayBufferView(key)){
294-
thrownewERR_INVALID_ARG_TYPE(
295-
'key',
296-
['string','Buffer','TypedArray','DataView'],
297-
key
298-
);
299-
}
300-
301-
iv=toBuf(iv);
302-
if(iv!==null&&!isArrayBufferView(iv)){
303-
thrownewERR_INVALID_ARG_TYPE(
304-
'iv',
305-
['string','Buffer','TypedArray','DataView'],
306-
iv
307-
);
308-
}
309-
310-
constauthTagLength=getUIntOption(options,'authTagLength');
311-
312-
this._handle=newCipherBase(false);
313-
this._handle.initiv(cipher,key,iv,authTagLength);
314-
this._decoder=null;
315-
316-
LazyTransform.call(this,options);
279+
createCipherWithIV.call(this,cipher,key,options,false,iv);
317280
}
318281

319282
inherits(Decipheriv,LazyTransform);

0 commit comments

Comments
 (0)