Skip to content

Commit 961710b

Browse files
panvaRafaelGSS
authored andcommitted
crypto: add KeyObject Symbol.toStringTag
PR-URL: #46043 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent a936eae commit 961710b

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

lib/internal/crypto/keys.js

+9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const {
77
ObjectDefineProperties,
88
ObjectSetPrototypeOf,
99
Symbol,
10+
SymbolToStringTag,
1011
Uint8Array,
1112
} = primordials;
1213

@@ -139,6 +140,14 @@ const {
139140
}
140141
}
141142

143+
ObjectDefineProperties(KeyObject.prototype, {
144+
[SymbolToStringTag]: {
145+
__proto__: null,
146+
configurable: true,
147+
value: 'KeyObject',
148+
},
149+
});
150+
142151
class SecretKeyObject extends KeyObject {
143152
constructor(handle) {
144153
super('secret', handle);

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

+6
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)