Skip to content

Commit

Permalink
crypto: expose KeyObject.isKeyObject(obj) and CryptoKey.isCryptoKey(obj)
Browse files Browse the repository at this point in the history
closes #38611
  • Loading branch information
panva committed May 10, 2021
1 parent 4243ce0 commit 96301e1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
10 changes: 10 additions & 0 deletions doc/api/crypto.md
Original file line number Diff line number Diff line change
Expand Up @@ -1852,6 +1852,16 @@ passing keys as strings or `Buffer`s due to improved security features.
The receiver obtains a cloned `KeyObject`, and the `KeyObject` does not need to
be listed in the `transferList` argument.

### Static method: `KeyObject.isKeyObject(obj)`
<!-- YAML
added: REPLACEME
-->

* `obj` {Object}
* Returns: {boolean}

Returns `true` if `obj` is a `KeyObject`, `false` otherwise.

### Static method: `KeyObject.from(key)`
<!-- YAML
added: v15.0.0
Expand Down
12 changes: 12 additions & 0 deletions doc/api/webcrypto.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,18 @@ An error will be thrown if the given `typedArray` is larger than 65,536 bytes.
added: v15.0.0
-->

### Static method: `CryptoKey.isCryptoKey(obj)`
<!-- YAML
added: REPLACEME
-->

* `obj` {Object}
* Returns: {boolean}

Returns `true` if `obj` is a `CryptoKey`, `false` otherwise. This method
is specific to Node.js, it is not portable to other implementations of
[Web Crypto API][].

### `cryptoKey.algorithm`
<!-- YAML
added: v15.0.0
Expand Down
12 changes: 10 additions & 2 deletions lib/internal/crypto/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ const {
});
}

static isKeyObject(obj) {
return isKeyObject(obj);
}

get type() {
return this[kKeyType];
}
Expand Down Expand Up @@ -639,8 +643,8 @@ function createPrivateKey(key) {
return new PrivateKeyObject(handle);
}

function isKeyObject(key) {
return key instanceof KeyObject;
function isKeyObject(obj) {
return obj != null && obj[kHandle] !== undefined;
}

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

static isCryptoKey(obj) {
return isCryptoKey(obj);
}

[kInspect](depth, options) {
if (depth < 0)
return this;
Expand Down

0 comments on commit 96301e1

Please sign in to comment.