Skip to content

Commit cafd19c

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

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

doc/api/crypto.md

+20
Original file line numberDiff line numberDiff line change
@@ -1902,6 +1902,26 @@ const {
19021902
})();
19031903
```
19041904

1905+
### Static method: `KeyObject.isCryptoKey(obj)`
1906+
<!-- YAML
1907+
added: REPLACEME
1908+
-->
1909+
1910+
* `obj` {Object}
1911+
* Returns: {boolean}
1912+
1913+
Returns `true` if `obj` is a `CryptoKey`, `false` otherwise.
1914+
1915+
### Static method: `KeyObject.isKeyObject(obj)`
1916+
<!-- YAML
1917+
added: REPLACEME
1918+
-->
1919+
1920+
* `obj` {Object}
1921+
* Returns: {boolean}
1922+
1923+
Returns `true` if `obj` is a `KeyObject`, `false` otherwise.
1924+
19051925
### `keyObject.asymmetricKeyDetails`
19061926
<!-- YAML
19071927
added: v15.7.0

lib/internal/crypto/keys.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ const {
116116
});
117117
}
118118

119+
static isCryptoKey(obj) {
120+
return isCryptoKey(obj);
121+
}
122+
123+
static isKeyObject(obj) {
124+
return isKeyObject(obj);
125+
}
126+
119127
get type() {
120128
return this[kKeyType];
121129
}
@@ -639,8 +647,8 @@ function createPrivateKey(key) {
639647
return new PrivateKeyObject(handle);
640648
}
641649

642-
function isKeyObject(key) {
643-
return key instanceof KeyObject;
650+
function isKeyObject(obj) {
651+
return obj != null && obj[kHandle] !== undefined;
644652
}
645653

646654
// Our implementation of CryptoKey is a simple wrapper around a KeyObject

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

+11
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

+16-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ if (!common.hasCrypto)
66
common.skip('missing crypto');
77

88
const assert = require('assert');
9-
const { subtle, CryptoKey } = require('crypto').webcrypto;
9+
const { KeyObject, webcrypto: { subtle, CryptoKey } } = require('crypto');
1010

1111
const allUsages = [
1212
'encrypt',
@@ -220,6 +220,8 @@ const vectors = {
220220

221221
assert(publicKey);
222222
assert(privateKey);
223+
assert(KeyObject.isCryptoKey(publicKey));
224+
assert(KeyObject.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(KeyObject.isCryptoKey(publicKey));
372+
assert(KeyObject.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(KeyObject.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(KeyObject.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(KeyObject.isCryptoKey(publicKey));
554+
assert(KeyObject.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(KeyObject.isCryptoKey(publicKey));
646+
assert(KeyObject.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(!KeyObject.isCryptoKey(buffer));
663+
}

0 commit comments

Comments
 (0)