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

View File

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

View File

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

View File

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

View File

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