GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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
Filter options
  • lib/internal/crypto
    • keys.js
  • test/parallel
    • test-webcrypto-internal-slots.mjs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/crypto/keys.js‎

Copy file name to clipboardExpand all lines: lib/internal/crypto/keys.js
+11-2Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const {
4+
ArrayFrom,
45
ArrayPrototypeSlice,
56
ObjectDefineProperties,
67
ObjectDefineProperty,
@@ -81,6 +82,8 @@ const kAlgorithm = Symbol('kAlgorithm');
8182
const kExtractable = Symbol('kExtractable');
8283
const kKeyType = Symbol('kKeyType');
8384
const kKeyUsages = Symbol('kKeyUsages');
85+
const kCachedAlgorithm = Symbol('kCachedAlgorithm');
86+
const kCachedKeyUsages = Symbol('kCachedKeyUsages');
8487

8588
// Key input contexts.
8689
const kConsumePublic = 0;
@@ -835,13 +838,19 @@ class CryptoKey {
835838
get algorithm() {
836839
if (!(this instanceof CryptoKey))
837840
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];
839847
}
840848

841849
get usages() {
842850
if (!(this instanceof CryptoKey))
843851
throw new ERR_INVALID_THIS('CryptoKey');
844-
return this[kKeyUsages];
852+
this[kCachedKeyUsages] ??= ArrayFrom(this[kKeyUsages]);
853+
return this[kCachedKeyUsages];
845854
}
846855
}
847856

Collapse file

‎test/parallel/test-webcrypto-internal-slots.mjs‎

Copy file name to clipboard
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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));

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.