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;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
function Cipher(cipher, password, options) {
|
function createCipherBase(cipher, credential, options, decipher, iv) {
|
||||||
if (!(this instanceof Cipher))
|
const authTagLength = getUIntOption(options, 'authTagLength');
|
||||||
return new Cipher(cipher, password, options);
|
|
||||||
|
|
||||||
|
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')
|
if (typeof cipher !== 'string')
|
||||||
throw new ERR_INVALID_ARG_TYPE('cipher', 'string', cipher);
|
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);
|
key = toBuf(key);
|
||||||
this._decoder = null;
|
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);
|
inherits(Cipher, LazyTransform);
|
||||||
@ -198,34 +233,7 @@ function Cipheriv(cipher, key, iv, options) {
|
|||||||
if (!(this instanceof Cipheriv))
|
if (!(this instanceof Cipheriv))
|
||||||
return new Cipheriv(cipher, key, iv, options);
|
return new Cipheriv(cipher, key, iv, options);
|
||||||
|
|
||||||
if (typeof cipher !== 'string')
|
createCipherWithIV.call(this, cipher, key, options, true, iv);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inherits(Cipheriv, LazyTransform);
|
inherits(Cipheriv, LazyTransform);
|
||||||
@ -244,25 +252,7 @@ function Decipher(cipher, password, options) {
|
|||||||
if (!(this instanceof Decipher))
|
if (!(this instanceof Decipher))
|
||||||
return new Decipher(cipher, password, options);
|
return new Decipher(cipher, password, options);
|
||||||
|
|
||||||
if (typeof cipher !== 'string')
|
createCipher.call(this, cipher, password, options, false);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inherits(Decipher, LazyTransform);
|
inherits(Decipher, LazyTransform);
|
||||||
@ -281,34 +271,7 @@ function Decipheriv(cipher, key, iv, options) {
|
|||||||
if (!(this instanceof Decipheriv))
|
if (!(this instanceof Decipheriv))
|
||||||
return new Decipheriv(cipher, key, iv, options);
|
return new Decipheriv(cipher, key, iv, options);
|
||||||
|
|
||||||
if (typeof cipher !== 'string')
|
createCipherWithIV.call(this, cipher, key, options, false, iv);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inherits(Decipheriv, LazyTransform);
|
inherits(Decipheriv, LazyTransform);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user