From 7f24cfca5bf613540bfe2796e20158784c2bcaa2 Mon Sep 17 00:00:00 2001 From: Bryan English Date: Fri, 19 Aug 2016 14:45:20 -0700 Subject: [PATCH 1/2] test: crypto createClass instanceof Class The crypto classes are also exposed as createClass for each class. This tests that each of them returns an instance of the class in question. --- test/parallel/test-crypto-classes.js | 31 ++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 test/parallel/test-crypto-classes.js diff --git a/test/parallel/test-crypto-classes.js b/test/parallel/test-crypto-classes.js new file mode 100644 index 00000000000000..ed6bfd76c1e012 --- /dev/null +++ b/test/parallel/test-crypto-classes.js @@ -0,0 +1,31 @@ +'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'], + 'Credentials': [] +}; + +if (!common.hasFipsCrypto) { + TEST_CASES.Cipher = ['aes192', 'secret']; + TEST_CASES.Decipher = ['aes192', 'secret']; +} + +for (const [clazz, args] of Object.entries(TEST_CASES)) { + assert(crypto[`create${clazz}`](...args) instanceof crypto[clazz]); +} From 55af690c8f955358eb73b4b4a4d4bf3d3e8a232b Mon Sep 17 00:00:00 2001 From: Bryan English Date: Fri, 19 Aug 2016 13:56:58 -0700 Subject: [PATCH 2/2] crypto: expose ECDH class For consistency with the rest of the crypto classes, exposes the ECDH class. Originally, only the createECDH function was exposed, and there was no real reason to hide the class. --- lib/crypto.js | 7 ++----- lib/internal/crypto/diffiehellman.js | 3 +++ test/parallel/test-crypto-classes.js | 1 + 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/crypto.js b/lib/crypto.js index 1c8c6b36fb3efb..eb797b86ee512d 100644 --- a/lib/crypto.js +++ b/lib/crypto.js @@ -79,10 +79,6 @@ const { } = require('internal/crypto/util'); const Certificate = require('internal/crypto/certificate'); -function createECDH(curve) { - return new ECDH(curve); -} - module.exports = exports = { // Methods _toBuf: toBuf, @@ -92,7 +88,7 @@ module.exports = exports = { createDecipheriv: Decipheriv, createDiffieHellman: DiffieHellman, createDiffieHellmanGroup: DiffieHellmanGroup, - createECDH, + createECDH: ECDH, createHash: Hash, createHmac: Hmac, createSign: Sign, @@ -124,6 +120,7 @@ module.exports = exports = { Decipheriv, DiffieHellman, DiffieHellmanGroup, + ECDH, Hash, Hmac, Sign, diff --git a/lib/internal/crypto/diffiehellman.js b/lib/internal/crypto/diffiehellman.js index b891a0b35413b6..71fd13819c62cf 100644 --- a/lib/internal/crypto/diffiehellman.js +++ b/lib/internal/crypto/diffiehellman.js @@ -168,6 +168,9 @@ DiffieHellman.prototype.setPrivateKey = function setPrivateKey(key, encoding) { function ECDH(curve) { + if (!(this instanceof ECDH)) + return new ECDH(curve); + if (typeof curve !== 'string') throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'curve', 'string'); diff --git a/test/parallel/test-crypto-classes.js b/test/parallel/test-crypto-classes.js index ed6bfd76c1e012..3923cb0dc7cfa8 100644 --- a/test/parallel/test-crypto-classes.js +++ b/test/parallel/test-crypto-classes.js @@ -18,6 +18,7 @@ const TEST_CASES = { 'Verify': ['RSA-SHA1'], 'DiffieHellman': [1024], 'DiffieHellmanGroup': ['modp5'], + 'ECDH': ['prime256v1'], 'Credentials': [] };