crypto: put legacy _handle accessors on prototypes

Creating deprecated accessors each time an object is created is very
time consuming.

Refs: https://github.com/nodejs/node/pull/22747
Fixes: https://github.com/nodejs/node/issues/24266

PR-URL: https://github.com/nodejs/node/pull/24269
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Michaël Zasso 2018-11-09 09:48:48 +01:00
parent 7c67cfd104
commit e83d7e8d88
No known key found for this signature in database
GPG Key ID: 770F7A9A5AE15600
5 changed files with 34 additions and 17 deletions

View File

@ -74,7 +74,7 @@ function getUIntOption(options, key) {
function createCipherBase(cipher, credential, options, decipher, iv) {
const authTagLength = getUIntOption(options, 'authTagLength');
legacyNativeHandle(this, new CipherBase(decipher));
this[kHandle] = new CipherBase(decipher);
if (iv === undefined) {
this[kHandle].init(cipher, credential, authTagLength);
} else {
@ -219,6 +219,8 @@ Cipher.prototype.setAAD = function setAAD(aadbuf, options) {
return this;
};
legacyNativeHandle(Cipher);
function Cipheriv(cipher, key, iv, options) {
if (!(this instanceof Cipheriv))
return new Cipheriv(cipher, key, iv, options);
@ -254,6 +256,7 @@ function addCipherPrototypeFunctions(constructor) {
inherits(Cipheriv, LazyTransform);
addCipherPrototypeFunctions(Cipheriv);
legacyNativeHandle(Cipheriv);
function Decipher(cipher, password, options) {
if (!(this instanceof Decipher))
@ -264,6 +267,7 @@ function Decipher(cipher, password, options) {
inherits(Decipher, LazyTransform);
addCipherPrototypeFunctions(Decipher);
legacyNativeHandle(Decipher);
function Decipheriv(cipher, key, iv, options) {
@ -275,6 +279,7 @@ function Decipheriv(cipher, key, iv, options) {
inherits(Decipheriv, LazyTransform);
addCipherPrototypeFunctions(Decipheriv);
legacyNativeHandle(Decipheriv);
module.exports = {
Cipher,

View File

@ -61,7 +61,7 @@ function DiffieHellman(sizeOrKey, keyEncoding, generator, genEncoding) {
else if (typeof generator !== 'number')
generator = toBuf(generator, genEncoding);
legacyNativeHandle(this, new _DiffieHellman(sizeOrKey, generator));
this[kHandle] = new _DiffieHellman(sizeOrKey, generator);
Object.defineProperty(this, 'verifyError', {
enumerable: true,
value: this[kHandle].verifyError,
@ -73,7 +73,7 @@ function DiffieHellman(sizeOrKey, keyEncoding, generator, genEncoding) {
function DiffieHellmanGroup(name) {
if (!(this instanceof DiffieHellmanGroup))
return new DiffieHellmanGroup(name);
legacyNativeHandle(this, new _DiffieHellmanGroup(name));
this[kHandle] = new _DiffieHellmanGroup(name);
Object.defineProperty(this, 'verifyError', {
enumerable: true,
value: this[kHandle].verifyError,
@ -165,13 +165,16 @@ DiffieHellman.prototype.setPrivateKey = function setPrivateKey(key, encoding) {
return this;
};
legacyNativeHandle(DiffieHellman);
legacyNativeHandle(DiffieHellmanGroup);
function ECDH(curve) {
if (!(this instanceof ECDH))
return new ECDH(curve);
validateString(curve, 'curve');
legacyNativeHandle(this, new _ECDH(curve));
this[kHandle] = new _ECDH(curve);
}
ECDH.prototype.computeSecret = DiffieHellman.prototype.computeSecret;
@ -192,6 +195,8 @@ ECDH.prototype.getPublicKey = function getPublicKey(encoding, format) {
return encode(key, encoding);
};
legacyNativeHandle(ECDH);
ECDH.convertKey = function convertKey(key, curve, inEnc, outEnc, format) {
if (typeof key !== 'string' && !isArrayBufferView(key)) {
throw new ERR_INVALID_ARG_TYPE(

View File

@ -32,7 +32,7 @@ function Hash(algorithm, options) {
if (!(this instanceof Hash))
return new Hash(algorithm, options);
validateString(algorithm, 'algorithm');
legacyNativeHandle(this, new _Hash(algorithm));
this[kHandle] = new _Hash(algorithm);
this[kState] = {
[kFinalized]: false
};
@ -81,6 +81,8 @@ Hash.prototype.digest = function digest(outputEncoding) {
return ret;
};
legacyNativeHandle(Hash);
function Hmac(hmac, key, options) {
if (!(this instanceof Hmac))
@ -90,7 +92,7 @@ function Hmac(hmac, key, options) {
throw new ERR_INVALID_ARG_TYPE('key',
['string', 'TypedArray', 'DataView'], key);
}
legacyNativeHandle(this, new _Hmac());
this[kHandle] = new _Hmac();
this[kHandle].init(hmac, toBuf(key));
this[kState] = {
[kFinalized]: false
@ -122,6 +124,8 @@ Hmac.prototype.digest = function digest(outputEncoding) {
Hmac.prototype._flush = Hash.prototype._flush;
Hmac.prototype._transform = Hash.prototype._transform;
legacyNativeHandle(Hmac);
module.exports = {
Hash,
Hmac

View File

@ -24,7 +24,7 @@ function Sign(algorithm, options) {
if (!(this instanceof Sign))
return new Sign(algorithm, options);
validateString(algorithm, 'algorithm');
legacyNativeHandle(this, new _Sign());
this[kHandle] = new _Sign();
this[kHandle].init(algorithm);
Writable.call(this, options);
@ -45,6 +45,8 @@ Sign.prototype.update = function update(data, encoding) {
return this;
};
legacyNativeHandle(Sign);
function getPadding(options) {
return getIntOption('padding', RSA_PKCS1_PADDING, options);
}
@ -93,7 +95,7 @@ function Verify(algorithm, options) {
if (!(this instanceof Verify))
return new Verify(algorithm, options);
validateString(algorithm, 'algorithm');
legacyNativeHandle(this, new _Verify());
this[kHandle] = new _Verify();
this[kHandle].init(algorithm);
Writable.call(this, options);
@ -121,6 +123,8 @@ Verify.prototype.verify = function verify(options, signature, sigEncoding) {
return this[kHandle].verify(key, signature, rsaPadding, pssSaltLength);
};
legacyNativeHandle(Verify);
module.exports = {
Sign,
Verify

View File

@ -30,15 +30,14 @@ const {
const kHandle = Symbol('kHandle');
function legacyNativeHandle(obj, handle) {
obj[kHandle] = handle;
Object.defineProperty(obj, '_handle', {
get: deprecate(() => handle,
`${obj.constructor.name}._handle is deprecated. Use the ` +
'public API instead.', 'DEP0117'),
set: deprecate((h) => obj[kHandle] = handle = h,
`${obj.constructor.name}._handle is deprecated. Use the ` +
'public API instead.', 'DEP0117'),
function legacyNativeHandle(clazz) {
Object.defineProperty(clazz.prototype, '_handle', {
get: deprecate(function() { return this[kHandle]; },
`${clazz.name}._handle is deprecated. Use the public API ` +
'instead.', 'DEP0117'),
set: deprecate(function(h) { this[kHandle] = h; },
`${clazz.name}._handle is deprecated. Use the public API ` +
'instead.', 'DEP0117'),
enumerable: false
});
}