diff options
author | Daniel Bevenius <daniel.bevenius@gmail.com> | 2018-04-19 15:02:44 +0200 |
---|---|---|
committer | Daniel Bevenius <daniel.bevenius@gmail.com> | 2018-04-23 05:39:57 +0200 |
commit | c85126302393b0f460268f6cc6fb73631f5bea1c (patch) | |
tree | 259c023963ef83ec636c0ca66810f350361da5e7 /lib | |
parent | 358d8ffad650b6fb966082e7bd1460b0d6a4eacc (diff) | |
download | android-node-v8-c85126302393b0f460268f6cc6fb73631f5bea1c.tar.gz android-node-v8-c85126302393b0f460268f6cc6fb73631f5bea1c.tar.bz2 android-node-v8-c85126302393b0f460268f6cc6fb73631f5bea1c.zip |
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.
PR-URL: https://github.com/nodejs/node/pull/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>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/internal/crypto/cipher.js | 129 |
1 files changed, 46 insertions, 83 deletions
diff --git a/lib/internal/crypto/cipher.js b/lib/internal/crypto/cipher.js index 118fc1da5b..0970d27c76 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); @@ -244,25 +252,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); @@ -281,34 +271,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); |