diff options
author | Filip Skokan <panva.ip@gmail.com> | 2022-02-26 14:51:09 +0100 |
---|---|---|
committer | Danielle Adams <adamzdanielle@gmail.com> | 2022-04-23 22:47:06 -0400 |
commit | b7599d8b149717793848206411b9067832556e35 (patch) | |
tree | e1ebf94de881524e0118c809e1ee864b03ce961c /test | |
parent | e1704407ae660b69dac256254a66d6a27179707d (diff) | |
download | node-new-b7599d8b149717793848206411b9067832556e35.tar.gz |
crypto: add KeyObject.prototype.equals method
PR-URL: https://github.com/nodejs/node/pull/42093
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Diffstat (limited to 'test')
-rw-r--r-- | test/parallel/test-crypto-key-objects.js | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/test/parallel/test-crypto-key-objects.js b/test/parallel/test-crypto-key-objects.js index 13441ac6f9..40a982ea7b 100644 --- a/test/parallel/test-crypto-key-objects.js +++ b/test/parallel/test-crypto-key-objects.js @@ -21,6 +21,7 @@ const { privateDecrypt, privateEncrypt, getCurves, + generateKeySync, generateKeyPairSync, webcrypto, } = require('crypto'); @@ -844,3 +845,51 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem', assert(!isKeyObject(cryptoKey)); }); } + +{ + const first = Buffer.from('Hello'); + const second = Buffer.from('World'); + const keyObject = createSecretKey(first); + assert(createSecretKey(first).equals(createSecretKey(first))); + assert(!createSecretKey(first).equals(createSecretKey(second))); + + assert.throws(() => keyObject.equals(0), { + name: 'TypeError', + code: 'ERR_INVALID_ARG_TYPE', + message: 'The "otherKeyObject" argument must be an instance of KeyObject. Received type number (0)' + }); + + assert(keyObject.equals(keyObject)); + assert(!keyObject.equals(createPublicKey(publicPem))); + assert(!keyObject.equals(createPrivateKey(privatePem))); +} + +{ + const first = generateKeyPairSync('ed25519'); + const second = generateKeyPairSync('ed25519'); + const secret = generateKeySync('aes', { length: 128 }); + + assert(first.publicKey.equals(first.publicKey)); + assert(first.publicKey.equals(createPublicKey( + first.publicKey.export({ format: 'pem', type: 'spki' })))); + assert(!first.publicKey.equals(second.publicKey)); + assert(!first.publicKey.equals(second.privateKey)); + assert(!first.publicKey.equals(secret)); + + assert(first.privateKey.equals(first.privateKey)); + assert(first.privateKey.equals(createPrivateKey( + first.privateKey.export({ format: 'pem', type: 'pkcs8' })))); + assert(!first.privateKey.equals(second.privateKey)); + assert(!first.privateKey.equals(second.publicKey)); + assert(!first.privateKey.equals(secret)); +} + +{ + const first = generateKeyPairSync('ed25519'); + const second = generateKeyPairSync('ed448'); + + assert(!first.publicKey.equals(second.publicKey)); + assert(!first.publicKey.equals(second.privateKey)); + assert(!first.privateKey.equals(second.privateKey)); + assert(!first.privateKey.equals(second.publicKey)); +} |