Skip to content
/ node Public
forked from nodejs/node

Commit 4c9b917

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

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

doc/api/crypto.md

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

+10
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,16 @@ 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.
378+
369379
### `cryptoKey.algorithm`
370380
<!-- YAML
371381
added: v15.0.0

lib/internal/crypto/keys.js

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

119+
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+
isCryptoKey(obj) {
664+
return isCryptoKey(obj);
665+
}
666+
659667
[kInspect](depth, options) {
660668
if (depth < 0)
661669
return this;

0 commit comments

Comments
 (0)