Skip to content

Commit

Permalink
crypto: put legacy _handle accessors on prototypes
Browse files Browse the repository at this point in the history
Creating deprecated accessors each time an object is created is very
time consuming.

Refs: #22747
Fixes: #24266

PR-URL: #24269
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
  • Loading branch information
targos committed Nov 11, 2018
1 parent 7c67cfd commit e83d7e8
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 17 deletions.
7 changes: 6 additions & 1 deletion lib/internal/crypto/cipher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -254,6 +256,7 @@ function addCipherPrototypeFunctions(constructor) {

inherits(Cipheriv, LazyTransform);
addCipherPrototypeFunctions(Cipheriv);
legacyNativeHandle(Cipheriv);

function Decipher(cipher, password, options) {
if (!(this instanceof Decipher))
Expand All @@ -264,6 +267,7 @@ function Decipher(cipher, password, options) {

inherits(Decipher, LazyTransform);
addCipherPrototypeFunctions(Decipher);
legacyNativeHandle(Decipher);


function Decipheriv(cipher, key, iv, options) {
Expand All @@ -275,6 +279,7 @@ function Decipheriv(cipher, key, iv, options) {

inherits(Decipheriv, LazyTransform);
addCipherPrototypeFunctions(Decipheriv);
legacyNativeHandle(Decipheriv);

module.exports = {
Cipher,
Expand Down
11 changes: 8 additions & 3 deletions lib/internal/crypto/diffiehellman.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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(
Expand Down
8 changes: 6 additions & 2 deletions lib/internal/crypto/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand Down Expand Up @@ -81,6 +81,8 @@ Hash.prototype.digest = function digest(outputEncoding) {
return ret;
};

legacyNativeHandle(Hash);


function Hmac(hmac, key, options) {
if (!(this instanceof Hmac))
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions lib/internal/crypto/sig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
17 changes: 8 additions & 9 deletions lib/internal/crypto/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
}
Expand Down

0 comments on commit e83d7e8

Please sign in to comment.