@@ -73,10 +73,21 @@ function getUIntOption(options, key) {
73
73
return - 1 ;
74
74
}
75
75
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 ;
79
86
87
+ LazyTransform . call ( this , options ) ;
88
+ }
89
+
90
+ function createCipher ( cipher , password , options , decipher ) {
80
91
if ( typeof cipher !== 'string' )
81
92
throw new ERR_INVALID_ARG_TYPE ( 'cipher' , 'string' , cipher ) ;
82
93
@@ -89,14 +100,38 @@ function Cipher(cipher, password, options) {
89
100
) ;
90
101
}
91
102
92
- const authTagLength = getUIntOption ( options , 'authTagLength' ) ;
103
+ createCipherBase . call ( this , cipher , password , options , decipher ) ;
104
+ }
93
105
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 ) ;
95
109
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
+ }
98
118
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 ) ;
100
135
}
101
136
102
137
inherits ( Cipher , LazyTransform ) ;
@@ -198,34 +233,7 @@ function Cipheriv(cipher, key, iv, options) {
198
233
if ( ! ( this instanceof Cipheriv ) )
199
234
return new Cipheriv ( cipher , key , iv , options ) ;
200
235
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 ) ;
229
237
}
230
238
231
239
inherits ( Cipheriv , LazyTransform ) ;
@@ -248,25 +256,7 @@ function Decipher(cipher, password, options) {
248
256
if ( ! ( this instanceof Decipher ) )
249
257
return new Decipher ( cipher , password , options ) ;
250
258
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 ) ;
270
260
}
271
261
272
262
inherits ( Decipher , LazyTransform ) ;
@@ -286,34 +276,7 @@ function Decipheriv(cipher, key, iv, options) {
286
276
if ( ! ( this instanceof Decipheriv ) )
287
277
return new Decipheriv ( cipher , key , iv , options ) ;
288
278
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 ) ;
317
280
}
318
281
319
282
inherits ( Decipheriv , LazyTransform ) ;
0 commit comments