Skip to content

Commit 856c82a

Browse files
committed
crypto: expose KeyObject.isKeyObject(obj) and CryptoKey.isCryptoKey(obj)
closes #38611
1 parent 4243ce0 commit 856c82a

File tree

5 files changed

+58
-2
lines changed

5 files changed

+58
-2
lines changed

doc/api/crypto.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,6 +1852,16 @@ passing keys as strings or `Buffer`s due to improved security features.
18521852
The receiver obtains a cloned `KeyObject`, and the `KeyObject` does not need to
18531853
be listed in the `transferList` argument.
18541854

1855+
### Static method: `KeyObject.isKeyObject(obj)`
1856+
<!-- YAML
1857+
added: REPLACEME
1858+
-->
1859+
1860+
* `obj` {Object}
1861+
* Returns: {boolean}
1862+
1863+
Returns `true` if `obj` is a `KeyObject`, `false` otherwise.
1864+
18551865
### Static method: `KeyObject.from(key)`
18561866
<!-- YAML
18571867
added: v15.0.0

doc/api/webcrypto.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,18 @@ An error will be thrown if the given `typedArray` is larger than 65,536 bytes.
366366
added: v15.0.0
367367
-->
368368

369+
### Static method: `CryptoKey.isCryptoKey(obj)`
370+
<!-- YAML
371+
added: REPLACEME
372+
-->
373+
374+
* `obj` {Object}
375+
* Returns: {boolean}
376+
377+
Returns `true` if `obj` is a `CryptoKey`, `false` otherwise. This method
378+
is specific to Node.js, it is not portable to other implementations of
379+
[Web Crypto API][].
380+
369381
### `cryptoKey.algorithm`
370382
<!-- YAML
371383
added: v15.0.0

lib/internal/crypto/keys.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ const {
116116
});
117117
}
118118

119+
static isKeyObject(obj) {
120+
return isKeyObject(obj);
121+
}
122+
119123
get type() {
120124
return this[kKeyType];
121125
}
@@ -639,8 +643,8 @@ function createPrivateKey(key) {
639643
return new PrivateKeyObject(handle);
640644
}
641645

642-
function isKeyObject(key) {
643-
return key instanceof KeyObject;
646+
function isKeyObject(obj) {
647+
return obj != null && obj[kHandle] !== undefined;
644648
}
645649

646650
// Our implementation of CryptoKey is a simple wrapper around a KeyObject
@@ -656,6 +660,10 @@ class CryptoKey extends JSTransferable {
656660
throw new ERR_OPERATION_FAILED('Illegal constructor');
657661
}
658662

663+
static isCryptoKey(obj) {
664+
return isCryptoKey(obj);
665+
}
666+
659667
[kInspect](depth, options) {
660668
if (depth < 0)
661669
return this;

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,3 +757,14 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem',
757757
message: `Unsupported JWK EC curve: ${namedCurve}.`
758758
});
759759
}
760+
761+
{
762+
const buffer = Buffer.from('Hello World');
763+
const keyObject = createSecretKey(buffer);
764+
const keyPair = generateKeyPairSync('ed25519');
765+
assert(KeyObject.isKeyObject(keyPair.publicKey));
766+
assert(KeyObject.isKeyObject(keyPair.privateKey));
767+
assert(KeyObject.isKeyObject(keyObject));
768+
769+
assert(!KeyObject.isKeyObject(buffer));
770+
}

test/parallel/test-webcrypto-keygen.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ const vectors = {
220220

221221
assert(publicKey);
222222
assert(privateKey);
223+
assert(CryptoKey.isCryptoKey(publicKey));
224+
assert(CryptoKey.isCryptoKey(privateKey));
223225

224226
assert(publicKey instanceof CryptoKey);
225227
assert(privateKey instanceof CryptoKey);
@@ -366,6 +368,8 @@ const vectors = {
366368

367369
assert(publicKey);
368370
assert(privateKey);
371+
assert(CryptoKey.isCryptoKey(publicKey));
372+
assert(CryptoKey.isCryptoKey(privateKey));
369373

370374
assert.strictEqual(publicKey.type, 'public');
371375
assert.strictEqual(privateKey.type, 'private');
@@ -430,6 +434,7 @@ const vectors = {
430434
}, true, usages);
431435

432436
assert(key);
437+
assert(CryptoKey.isCryptoKey(key));
433438

434439
assert.strictEqual(key.type, 'secret');
435440
assert.strictEqual(key.extractable, true);
@@ -488,6 +493,7 @@ const vectors = {
488493
}
489494

490495
assert(key);
496+
assert(CryptoKey.isCryptoKey(key));
491497

492498
assert.strictEqual(key.type, 'secret');
493499
assert.strictEqual(key.extractable, true);
@@ -544,6 +550,8 @@ const vectors = {
544550

545551
assert(publicKey);
546552
assert(privateKey);
553+
assert(CryptoKey.isCryptoKey(publicKey));
554+
assert(CryptoKey.isCryptoKey(privateKey));
547555

548556
assert.strictEqual(publicKey.type, 'public');
549557
assert.strictEqual(privateKey.type, 'private');
@@ -634,6 +642,8 @@ const vectors = {
634642
}, true, ['deriveKey']);
635643
assert(publicKey);
636644
assert(privateKey);
645+
assert(CryptoKey.isCryptoKey(publicKey));
646+
assert(CryptoKey.isCryptoKey(privateKey));
637647
assert.strictEqual(publicKey.type, 'public');
638648
assert.strictEqual(privateKey.type, 'private');
639649
assert.strictEqual(publicKey.algorithm.name, 'NODE-DH');
@@ -646,3 +656,8 @@ const vectors = {
646656
assert.throws(() => new CryptoKey(), {
647657
code: 'ERR_OPERATION_FAILED'
648658
});
659+
660+
{
661+
const buffer = Buffer.from('Hello World');
662+
assert(!CryptoKey.isCryptoKey(buffer));
663+
}

0 commit comments

Comments
 (0)