diff --git a/lib/crypto.js b/lib/crypto.js index 56795e23f24af9..8b5690a3b88570 100644 --- a/lib/crypto.js +++ b/lib/crypto.js @@ -67,7 +67,6 @@ function toBuf(str, encoding) { } exports._toBuf = toBuf; - const assert = require('assert'); const StringDecoder = require('string_decoder').StringDecoder; @@ -559,17 +558,16 @@ DiffieHellman.prototype.setPrivateKey = function setPrivateKey(key, encoding) { }; +exports.createECDH = exports.ECDH = ECDH; function ECDH(curve) { + if (!(this instanceof ECDH)) + return new ECDH(curve); if (typeof curve !== 'string') throw new TypeError('"curve" argument should be a string'); this._handle = new binding.ECDH(curve); } -exports.createECDH = function createECDH(curve) { - return new ECDH(curve); -}; - ECDH.prototype.computeSecret = DiffieHellman.prototype.computeSecret; ECDH.prototype.setPrivateKey = DiffieHellman.prototype.setPrivateKey; ECDH.prototype.setPublicKey = DiffieHellman.prototype.setPublicKey; diff --git a/test/parallel/test-crypto-classes.js b/test/parallel/test-crypto-classes.js new file mode 100644 index 00000000000000..78d248c2b2850c --- /dev/null +++ b/test/parallel/test-crypto-classes.js @@ -0,0 +1,33 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); + +if (!common.hasCrypto) { + common.skip('missing crypto'); + return; +} +const crypto = require('crypto'); + +// 'ClassName' : ['args', 'for', 'constructor'] +const TEST_CASES = { + 'Hash': ['sha1'], + 'Hmac': ['sha1', 'Node'], + 'Cipheriv': ['des-ede3-cbc', '0123456789abcd0123456789', '12345678'], + 'Decipheriv': ['des-ede3-cbc', '0123456789abcd0123456789', '12345678'], + 'Sign': ['RSA-SHA1'], + 'Verify': ['RSA-SHA1'], + 'DiffieHellman': [1024], + 'DiffieHellmanGroup': ['modp5'], + 'ECDH': ['prime256v1'], + 'Credentials': [] +}; + +if (!common.hasFipsCrypto) { + TEST_CASES.Cipher = ['aes192', 'secret']; + TEST_CASES.Decipher = ['aes192', 'secret']; + TEST_CASES.DiffieHellman = [256]; +} + +for (const [clazz, args] of Object.entries(TEST_CASES)) { + assert(crypto[`create${clazz}`](...args) instanceof crypto[clazz]); +}