diff --git a/lib/internal/crypto/cipher.js b/lib/internal/crypto/cipher.js index d33a970148f52a..76965a9c15b154 100644 --- a/lib/internal/crypto/cipher.js +++ b/lib/internal/crypto/cipher.js @@ -73,10 +73,21 @@ function getUIntOption(options, key) { return -1; } -function Cipher(cipher, password, options) { - if (!(this instanceof Cipher)) - return new Cipher(cipher, password, options); +function createCipherBase(cipher, credential, options, decipher, iv) { + const authTagLength = getUIntOption(options, 'authTagLength'); + + this._handle = new CipherBase(decipher); + if (iv === undefined) { + this._handle.init(cipher, credential, authTagLength); + } else { + this._handle.initiv(cipher, credential, iv, authTagLength); + } + this._decoder = null; + LazyTransform.call(this, options); +} + +function createCipher(cipher, password, options, decipher) { if (typeof cipher !== 'string') throw new ERR_INVALID_ARG_TYPE('cipher', 'string', cipher); @@ -89,14 +100,38 @@ function Cipher(cipher, password, options) { ); } - const authTagLength = getUIntOption(options, 'authTagLength'); + createCipherBase.call(this, cipher, password, options, decipher); +} - this._handle = new CipherBase(true); +function createCipherWithIV(cipher, key, options, decipher, iv) { + if (typeof cipher !== 'string') + throw new ERR_INVALID_ARG_TYPE('cipher', 'string', cipher); - this._handle.init(cipher, password, authTagLength); - this._decoder = null; + key = toBuf(key); + if (!isArrayBufferView(key)) { + throw new ERR_INVALID_ARG_TYPE( + 'key', + ['string', 'Buffer', 'TypedArray', 'DataView'], + key + ); + } - LazyTransform.call(this, options); + iv = toBuf(iv); + if (iv !== null && !isArrayBufferView(iv)) { + throw new ERR_INVALID_ARG_TYPE( + 'iv', + ['string', 'Buffer', 'TypedArray', 'DataView'], + iv + ); + } + createCipherBase.call(this, cipher, key, options, decipher, iv); +} + +function Cipher(cipher, password, options) { + if (!(this instanceof Cipher)) + return new Cipher(cipher, password, options); + + createCipher.call(this, cipher, password, options, true); } inherits(Cipher, LazyTransform); @@ -198,34 +233,7 @@ function Cipheriv(cipher, key, iv, options) { if (!(this instanceof Cipheriv)) return new Cipheriv(cipher, key, iv, options); - if (typeof cipher !== 'string') - throw new ERR_INVALID_ARG_TYPE('cipher', 'string', cipher); - - key = toBuf(key); - if (!isArrayBufferView(key)) { - throw new ERR_INVALID_ARG_TYPE( - 'key', - ['string', 'Buffer', 'TypedArray', 'DataView'], - key - ); - } - - iv = toBuf(iv); - if (iv !== null && !isArrayBufferView(iv)) { - throw new ERR_INVALID_ARG_TYPE( - 'iv', - ['string', 'Buffer', 'TypedArray', 'DataView'], - iv - ); - } - - const authTagLength = getUIntOption(options, 'authTagLength'); - - this._handle = new CipherBase(true); - this._handle.initiv(cipher, key, iv, authTagLength); - this._decoder = null; - - LazyTransform.call(this, options); + createCipherWithIV.call(this, cipher, key, options, true, iv); } inherits(Cipheriv, LazyTransform); @@ -248,25 +256,7 @@ function Decipher(cipher, password, options) { if (!(this instanceof Decipher)) return new Decipher(cipher, password, options); - if (typeof cipher !== 'string') - throw new ERR_INVALID_ARG_TYPE('cipher', 'string', cipher); - - password = toBuf(password); - if (!isArrayBufferView(password)) { - throw new ERR_INVALID_ARG_TYPE( - 'password', - ['string', 'Buffer', 'TypedArray', 'DataView'], - password - ); - } - - const authTagLength = getUIntOption(options, 'authTagLength'); - - this._handle = new CipherBase(false); - this._handle.init(cipher, password, authTagLength); - this._decoder = null; - - LazyTransform.call(this, options); + createCipher.call(this, cipher, password, options, false); } inherits(Decipher, LazyTransform); @@ -286,34 +276,7 @@ function Decipheriv(cipher, key, iv, options) { if (!(this instanceof Decipheriv)) return new Decipheriv(cipher, key, iv, options); - if (typeof cipher !== 'string') - throw new ERR_INVALID_ARG_TYPE('cipher', 'string', cipher); - - key = toBuf(key); - if (!isArrayBufferView(key)) { - throw new ERR_INVALID_ARG_TYPE( - 'key', - ['string', 'Buffer', 'TypedArray', 'DataView'], - key - ); - } - - iv = toBuf(iv); - if (iv !== null && !isArrayBufferView(iv)) { - throw new ERR_INVALID_ARG_TYPE( - 'iv', - ['string', 'Buffer', 'TypedArray', 'DataView'], - iv - ); - } - - const authTagLength = getUIntOption(options, 'authTagLength'); - - this._handle = new CipherBase(false); - this._handle.initiv(cipher, key, iv, authTagLength); - this._decoder = null; - - LazyTransform.call(this, options); + createCipherWithIV.call(this, cipher, key, options, false, iv); } inherits(Decipheriv, LazyTransform);