Skip to content
/ node Public
forked from nodejs/node

Commit

Permalink
crypto: expose KeyObject.isKeyObject(obj) and KeyObject.isCryptoKey(obj)
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed May 10, 2021
1 parent 4243ce0 commit cafd19c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
20 changes: 20 additions & 0 deletions doc/api/crypto.md
Original file line number Diff line number Diff line change
Expand Up @@ -1902,6 +1902,26 @@ const {
})();
```

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

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

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

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

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

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

### `keyObject.asymmetricKeyDetails`
<!-- YAML
added: v15.7.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,14 @@ const {
});
}

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

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

get type() {
return this[kKeyType];
}
Expand Down Expand Up @@ -639,8 +647,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 Down
11 changes: 11 additions & 0 deletions test/parallel/test-crypto-key-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -757,3 +757,14 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem',
message: `Unsupported JWK EC curve: ${namedCurve}.`
});
}

{
const buffer = Buffer.from('Hello World');
const keyObject = createSecretKey(buffer);
const keyPair = generateKeyPairSync('ed25519');
assert(KeyObject.isKeyObject(keyPair.publicKey));
assert(KeyObject.isKeyObject(keyPair.privateKey));
assert(KeyObject.isKeyObject(keyObject));

assert(!KeyObject.isKeyObject(buffer));
}
17 changes: 16 additions & 1 deletion test/parallel/test-webcrypto-keygen.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ if (!common.hasCrypto)
common.skip('missing crypto');

const assert = require('assert');
const { subtle, CryptoKey } = require('crypto').webcrypto;
const { KeyObject, webcrypto: { subtle, CryptoKey } } = require('crypto');

const allUsages = [
'encrypt',
Expand Down Expand Up @@ -220,6 +220,8 @@ const vectors = {

assert(publicKey);
assert(privateKey);
assert(KeyObject.isCryptoKey(publicKey));
assert(KeyObject.isCryptoKey(privateKey));

assert(publicKey instanceof CryptoKey);
assert(privateKey instanceof CryptoKey);
Expand Down Expand Up @@ -366,6 +368,8 @@ const vectors = {

assert(publicKey);
assert(privateKey);
assert(KeyObject.isCryptoKey(publicKey));
assert(KeyObject.isCryptoKey(privateKey));

assert.strictEqual(publicKey.type, 'public');
assert.strictEqual(privateKey.type, 'private');
Expand Down Expand Up @@ -430,6 +434,7 @@ const vectors = {
}, true, usages);

assert(key);
assert(KeyObject.isCryptoKey(key));

assert.strictEqual(key.type, 'secret');
assert.strictEqual(key.extractable, true);
Expand Down Expand Up @@ -488,6 +493,7 @@ const vectors = {
}

assert(key);
assert(KeyObject.isCryptoKey(key));

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

assert(publicKey);
assert(privateKey);
assert(KeyObject.isCryptoKey(publicKey));
assert(KeyObject.isCryptoKey(privateKey));

assert.strictEqual(publicKey.type, 'public');
assert.strictEqual(privateKey.type, 'private');
Expand Down Expand Up @@ -634,6 +642,8 @@ const vectors = {
}, true, ['deriveKey']);
assert(publicKey);
assert(privateKey);
assert(KeyObject.isCryptoKey(publicKey));
assert(KeyObject.isCryptoKey(privateKey));
assert.strictEqual(publicKey.type, 'public');
assert.strictEqual(privateKey.type, 'private');
assert.strictEqual(publicKey.algorithm.name, 'NODE-DH');
Expand All @@ -646,3 +656,8 @@ const vectors = {
assert.throws(() => new CryptoKey(), {
code: 'ERR_OPERATION_FAILED'
});

{
const buffer = Buffer.from('Hello World');
assert(!KeyObject.isCryptoKey(buffer));
}

0 comments on commit cafd19c

Please sign in to comment.