Skip to content

Commit cca5940

Browse files
committed
crypto: add KeyObject Symbol.toStringTag
PR-URL: #46043 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Backport-PR-URL: #47336
1 parent bc17568 commit cca5940

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

lib/internal/crypto/keys.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ const {
137137
}
138138
}
139139

140+
ObjectDefineProperty(KeyObject.prototype, SymbolToStringTag, {
141+
__proto__: null,
142+
configurable: true,
143+
value: 'KeyObject',
144+
});
145+
140146
class SecretKeyObject extends KeyObject {
141147
constructor(handle) {
142148
super('secret', handle);

test/parallel/test-crypto-key-objects.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem',
6969
const keybuf = randomBytes(32);
7070
const key = createSecretKey(keybuf);
7171
assert.strictEqual(key.type, 'secret');
72+
assert.strictEqual(key.toString(), '[object KeyObject]');
7273
assert.strictEqual(key.symmetricKeySize, 32);
7374
assert.strictEqual(key.asymmetricKeyType, undefined);
7475
assert.strictEqual(key.asymmetricKeyDetails, undefined);
@@ -150,27 +151,32 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem',
150151

151152
const publicKey = createPublicKey(publicPem);
152153
assert.strictEqual(publicKey.type, 'public');
154+
assert.strictEqual(publicKey.toString(), '[object KeyObject]');
153155
assert.strictEqual(publicKey.asymmetricKeyType, 'rsa');
154156
assert.strictEqual(publicKey.symmetricKeySize, undefined);
155157

156158
const privateKey = createPrivateKey(privatePem);
157159
assert.strictEqual(privateKey.type, 'private');
160+
assert.strictEqual(privateKey.toString(), '[object KeyObject]');
158161
assert.strictEqual(privateKey.asymmetricKeyType, 'rsa');
159162
assert.strictEqual(privateKey.symmetricKeySize, undefined);
160163

161164
// It should be possible to derive a public key from a private key.
162165
const derivedPublicKey = createPublicKey(privateKey);
163166
assert.strictEqual(derivedPublicKey.type, 'public');
167+
assert.strictEqual(derivedPublicKey.toString(), '[object KeyObject]');
164168
assert.strictEqual(derivedPublicKey.asymmetricKeyType, 'rsa');
165169
assert.strictEqual(derivedPublicKey.symmetricKeySize, undefined);
166170

167171
const publicKeyFromJwk = createPublicKey({ key: publicJwk, format: 'jwk' });
168172
assert.strictEqual(publicKey.type, 'public');
173+
assert.strictEqual(publicKey.toString(), '[object KeyObject]');
169174
assert.strictEqual(publicKey.asymmetricKeyType, 'rsa');
170175
assert.strictEqual(publicKey.symmetricKeySize, undefined);
171176

172177
const privateKeyFromJwk = createPrivateKey({ key: jwk, format: 'jwk' });
173178
assert.strictEqual(privateKey.type, 'private');
179+
assert.strictEqual(privateKey.toString(), '[object KeyObject]');
174180
assert.strictEqual(privateKey.asymmetricKeyType, 'rsa');
175181
assert.strictEqual(privateKey.symmetricKeySize, undefined);
176182

0 commit comments

Comments
 (0)