From 6f6cf28095143a82207447173593cb104749e29a Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Tue, 3 Jan 2023 17:34:41 +0100 Subject: [PATCH] crypto: add KeyObject Symbol.toStringTag MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/46043 Reviewed-By: Antoine du Hamel Reviewed-By: Yagiz Nizipli Reviewed-By: Tobias Nießen Backport-PR-URL: https://github.com/nodejs/node/pull/46341 --- lib/internal/crypto/keys.js | 10 ++++++++++ test/parallel/test-crypto-key-objects.js | 6 ++++++ 2 files changed, 16 insertions(+) diff --git a/lib/internal/crypto/keys.js b/lib/internal/crypto/keys.js index 4303dc44fe60d7..576e2444d93d95 100644 --- a/lib/internal/crypto/keys.js +++ b/lib/internal/crypto/keys.js @@ -3,9 +3,11 @@ const { ArrayFrom, ArrayPrototypeSlice, + ObjectDefineProperties, ObjectDefineProperty, ObjectSetPrototypeOf, Symbol, + SymbolToStringTag, Uint8Array, } = primordials; @@ -136,6 +138,14 @@ const { } } + ObjectDefineProperties(KeyObject.prototype, { + [SymbolToStringTag]: { + __proto__: null, + configurable: true, + value: 'KeyObject', + }, + }); + class SecretKeyObject extends KeyObject { constructor(handle) { super('secret', handle); diff --git a/test/parallel/test-crypto-key-objects.js b/test/parallel/test-crypto-key-objects.js index 93e7cca3fbbdbd..84e32de6ba1ae9 100644 --- a/test/parallel/test-crypto-key-objects.js +++ b/test/parallel/test-crypto-key-objects.js @@ -69,6 +69,7 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem', const keybuf = randomBytes(32); const key = createSecretKey(keybuf); assert.strictEqual(key.type, 'secret'); + assert.strictEqual(key.toString(), '[object KeyObject]'); assert.strictEqual(key.symmetricKeySize, 32); assert.strictEqual(key.asymmetricKeyType, undefined); assert.strictEqual(key.asymmetricKeyDetails, undefined); @@ -150,27 +151,32 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem', const publicKey = createPublicKey(publicPem); assert.strictEqual(publicKey.type, 'public'); + assert.strictEqual(publicKey.toString(), '[object KeyObject]'); assert.strictEqual(publicKey.asymmetricKeyType, 'rsa'); assert.strictEqual(publicKey.symmetricKeySize, undefined); const privateKey = createPrivateKey(privatePem); assert.strictEqual(privateKey.type, 'private'); + assert.strictEqual(privateKey.toString(), '[object KeyObject]'); assert.strictEqual(privateKey.asymmetricKeyType, 'rsa'); assert.strictEqual(privateKey.symmetricKeySize, undefined); // It should be possible to derive a public key from a private key. const derivedPublicKey = createPublicKey(privateKey); assert.strictEqual(derivedPublicKey.type, 'public'); + assert.strictEqual(derivedPublicKey.toString(), '[object KeyObject]'); assert.strictEqual(derivedPublicKey.asymmetricKeyType, 'rsa'); assert.strictEqual(derivedPublicKey.symmetricKeySize, undefined); const publicKeyFromJwk = createPublicKey({ key: publicJwk, format: 'jwk' }); assert.strictEqual(publicKey.type, 'public'); + assert.strictEqual(publicKey.toString(), '[object KeyObject]'); assert.strictEqual(publicKey.asymmetricKeyType, 'rsa'); assert.strictEqual(publicKey.symmetricKeySize, undefined); const privateKeyFromJwk = createPrivateKey({ key: jwk, format: 'jwk' }); assert.strictEqual(privateKey.type, 'private'); + assert.strictEqual(privateKey.toString(), '[object KeyObject]'); assert.strictEqual(privateKey.asymmetricKeyType, 'rsa'); assert.strictEqual(privateKey.symmetricKeySize, undefined);