GeezerVison is showing github.com — crypto: return cached copies from CryptoKey algorithm and usages getters · nodejs/node@2d05c04
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: lib/internal/crypto/keys.js +11-2Lines changed: 11 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | 3 | const { |
| 4 | + ArrayFrom, |
4 | 5 | ArrayPrototypeSlice, |
5 | 6 | ObjectDefineProperties, |
6 | 7 | ObjectDefineProperty, |
@@ -81,6 +82,8 @@ const kAlgorithm = Symbol('kAlgorithm'); |
81 | 82 | const kExtractable = Symbol('kExtractable'); |
82 | 83 | const kKeyType = Symbol('kKeyType'); |
83 | 84 | const kKeyUsages = Symbol('kKeyUsages'); |
| 85 | +const kCachedAlgorithm = Symbol('kCachedAlgorithm'); |
| 86 | +const kCachedKeyUsages = Symbol('kCachedKeyUsages'); |
84 | 87 |
|
85 | 88 | // Key input contexts. |
86 | 89 | const kConsumePublic = 0; |
@@ -835,13 +838,19 @@ class CryptoKey { |
835 | 838 | get algorithm() { |
836 | 839 | if (!(this instanceof CryptoKey)) |
837 | 840 | throw new ERR_INVALID_THIS('CryptoKey'); |
838 | | - return this[kAlgorithm]; |
| 841 | + if (!this[kCachedAlgorithm]) { |
| 842 | + this[kCachedAlgorithm] ??= { ...this[kAlgorithm] }; |
| 843 | + this[kCachedAlgorithm].hash &&= { ...this[kCachedAlgorithm].hash }; |
| 844 | + this[kCachedAlgorithm].publicExponent &&= new Uint8Array(this[kCachedAlgorithm].publicExponent); |
| 845 | + } |
| 846 | + return this[kCachedAlgorithm]; |
839 | 847 | } |
840 | 848 |
|
841 | 849 | get usages() { |
842 | 850 | if (!(this instanceof CryptoKey)) |
843 | 851 | throw new ERR_INVALID_THIS('CryptoKey'); |
844 | | - return this[kKeyUsages]; |
| 852 | + this[kCachedKeyUsages] ??= ArrayFrom(this[kKeyUsages]); |
| 853 | + return this[kCachedKeyUsages]; |
845 | 854 | } |
846 | 855 | } |
847 | 856 |
|
|
Collapse file
Copy file name to clipboard +21Lines changed: 21 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 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)); |