Skip to content

Commit 961710b

Browse files
panvaRafaelGSS
authored andcommitted
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>
1 parent a936eae commit 961710b

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

‎lib/internal/crypto/keys.js‎

Lines changed: 9 additions & 0 deletions
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
classSecretKeyObjectextendsKeyObject{
143152
constructor(handle){
144153
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
constkeybuf=randomBytes(32);
7070
constkey=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
constpublicKey=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
constprivateKey=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
constderivedPublicKey=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
constpublicKeyFromJwk=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
constprivateKeyFromJwk=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)