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>
This commit is contained in:
parent
358d8ffad6
commit
c851263023
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user