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

Browse filesBrowse 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
Copy full SHA for 72029b8

File tree

Expand file treeCollapse file tree

1 file changed

+46
-83
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+46
-83
lines changed
Open diff view settings
Collapse file

‎lib/internal/crypto/cipher.js‎

Copy file name to clipboardExpand all lines: lib/internal/crypto/cipher.js
+46-83Lines 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-
function Cipher(cipher, password, options) {
77-
if (!(this instanceof Cipher))
78-
return new Cipher(cipher, password, options);
76+
function createCipherBase(cipher, credential, options, decipher, iv) {
77+
const authTagLength = getUIntOption(options, 'authTagLength');
78+
79+
this._handle = new CipherBase(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+
function createCipher(cipher, password, options, decipher) {
8091
if (typeof cipher !== 'string')
8192
throw new ERR_INVALID_ARG_TYPE('cipher', 'string', cipher);
8293

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

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

94-
this._handle = new CipherBase(true);
106+
function createCipherWithIV(cipher, key, options, decipher, iv) {
107+
if (typeof cipher !== 'string')
108+
throw new ERR_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+
throw new ERR_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+
throw new ERR_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+
function Cipher(cipher, password, options) {
131+
if (!(this instanceof Cipher))
132+
return new Cipher(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 (!(this instanceof Cipheriv))
199234
return new Cipheriv(cipher, key, iv, options);
200235

201-
if (typeof cipher !== 'string')
202-
throw new ERR_INVALID_ARG_TYPE('cipher', 'string', cipher);
203-
204-
key = toBuf(key);
205-
if (!isArrayBufferView(key)) {
206-
throw new ERR_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-
throw new ERR_INVALID_ARG_TYPE(
216-
'iv',
217-
['string', 'Buffer', 'TypedArray', 'DataView'],
218-
iv
219-
);
220-
}
221-
222-
const authTagLength = getUIntOption(options, 'authTagLength');
223-
224-
this._handle = new CipherBase(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 (!(this instanceof Decipher))
249257
return new Decipher(cipher, password, options);
250258

251-
if (typeof cipher !== 'string')
252-
throw new ERR_INVALID_ARG_TYPE('cipher', 'string', cipher);
253-
254-
password = toBuf(password);
255-
if (!isArrayBufferView(password)) {
256-
throw new ERR_INVALID_ARG_TYPE(
257-
'password',
258-
['string', 'Buffer', 'TypedArray', 'DataView'],
259-
password
260-
);
261-
}
262-
263-
const authTagLength = getUIntOption(options, 'authTagLength');
264-
265-
this._handle = new CipherBase(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 (!(this instanceof Decipheriv))
287277
return new Decipheriv(cipher, key, iv, options);
288278

289-
if (typeof cipher !== 'string')
290-
throw new ERR_INVALID_ARG_TYPE('cipher', 'string', cipher);
291-
292-
key = toBuf(key);
293-
if (!isArrayBufferView(key)) {
294-
throw new ERR_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-
throw new ERR_INVALID_ARG_TYPE(
304-
'iv',
305-
['string', 'Buffer', 'TypedArray', 'DataView'],
306-
iv
307-
);
308-
}
309-
310-
const authTagLength = getUIntOption(options, 'authTagLength');
311-
312-
this._handle = new CipherBase(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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.