crypto: return cached copies from CryptoKey algorithm and usages getters · nodejs/node@2d05c04

1+

import * as common from '../common/index.mjs';

2+3+

if (!common.hasCrypto)

4+

common.skip('missing crypto');

5+6+

import * as assert from 'node:assert';

7+

import * as util from 'node:util';

8+9+

const { subtle } = globalThis.crypto;

10+11+

const kp = await subtle.generateKey('Ed25519', true, ['sign', 'verify']);

12+

assert.notStrictEqual(kp.publicKey.algorithm, kp.privateKey.algorithm);

13+

assert.notStrictEqual(kp.publicKey.usages, kp.privateKey.usages);

14+

kp.publicKey.algorithm.name = 'ed25519';

15+

assert.strictEqual(kp.publicKey.algorithm.name, 'ed25519');

16+

kp.publicKey.usages.push('foo');

17+

assert.ok(kp.publicKey.usages.includes('foo'));

18+

assert.ok(util.inspect(kp.publicKey).includes("algorithm: { name: 'Ed25519' }"));

19+

assert.ok(util.inspect(kp.publicKey).includes("usages: [ 'verify' ]"));

20+21+

await subtle.sign('Ed25519', kp.privateKey, Buffer.alloc(32));