Skip to content

Commit bdc2204

Browse files
committed
crypto: use globalThis.crypto over require('crypto').webcrypto
1 parent 94d23f5 commit bdc2204

34 files changed

+72
-85
lines changed

benchmark/crypto/webcrypto-digest.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
'use strict';
22

33
const common = require('../common.js');
4-
const {
5-
createHash,
6-
webcrypto,
7-
} = require('crypto');
8-
const { subtle } = webcrypto;
4+
const { createHash } = require('crypto');
5+
const { subtle } = globalThis.crypto;
96

107
const bench = common.createBenchmark(main, {
118
sync: ['createHash', 'subtle'],
@@ -48,7 +45,7 @@ function measureSubtle(n, data, method) {
4845
}
4946

5047
function main({ n, sync, data, method }) {
51-
data = webcrypto.getRandomValues(Buffer.alloc(data));
48+
data = globalThis.crypto.getRandomValues(Buffer.alloc(data));
5249
switch (sync) {
5350
case 'createHash': return measureLegacy(n, data, method);
5451
case 'subtle': return measureSubtle(n, data, method);

doc/api/crypto.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1930,8 +1930,8 @@ added: v15.0.0
19301930
Example: Converting a `CryptoKey` instance to a `KeyObject`:
19311931

19321932
```mjs
1933-
const { webcrypto, KeyObject } = await import('node:crypto');
1934-
const { subtle } = webcrypto;
1933+
const { KeyObject } = await import('node:crypto');
1934+
const { subtle } = globalThis.crypto;
19351935

19361936
const key = await subtle.generateKey({
19371937
name: 'HMAC',
@@ -1945,12 +1945,8 @@ console.log(keyObject.symmetricKeySize);
19451945
```
19461946

19471947
```cjs
1948-
const {
1949-
webcrypto: {
1950-
subtle,
1951-
},
1952-
KeyObject,
1953-
} = require('node:crypto');
1948+
const { KeyObject } = require('node:crypto');
1949+
const { subtle } = globalThis.crypto;
19541950

19551951
(async function() {
19561952
const key = await subtle.generateKey({

src/crypto/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,12 +310,12 @@ crypto.randomFill(buf, (err, buf) => {
310310
For the legacy Node.js crypto API, asynchronous single-call
311311
operations use the traditional Node.js callback pattern, as
312312
illustrated in the previous `randomFill()` example. In the
313-
Web Crypto API (accessible via `require('node:crypto').webcrypto`),
313+
Web Crypto API (accessible via `globalThis.crypto`),
314314
all asynchronous single-call operations are Promise-based.
315315

316316
```js
317317
// Example Web Crypto API asynchronous single-call operation
318-
const { subtle } = require('node:crypto').webcrypto;
318+
const { subtle } = globalThis.crypto;
319319

320320
subtle.generateKeys({ name: 'HMAC', length: 256 }, true, ['sign'])
321321
.then((key) => {

test/parallel/test-crypto-psychic-signatures.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ for (const [encoding, signatures] of Object.entries(vectors)) {
8080
);
8181

8282
// webcrypto
83-
crypto.webcrypto.subtle.importKey(
83+
globalThis.crypto.subtle.importKey(
8484
'spki',
8585
keyPair.publicKey,
8686
{ name: 'ECDSA', namedCurve: 'P-256' },
8787
false,
8888
['verify'],
8989
).then((publicKey) => {
90-
return crypto.webcrypto.subtle.verify(
90+
return globalThis.crypto.subtle.verify(
9191
{ name: 'ECDSA', hash: 'SHA-256' },
9292
publicKey,
9393
signature,

test/parallel/test-crypto-random.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ if (!common.hasCrypto)
2828

2929
const assert = require('assert');
3030
const crypto = require('crypto');
31-
const cryptop = require('crypto').webcrypto;
3231
const { kMaxLength } = require('buffer');
3332

3433
const kMaxInt32 = 2 ** 31 - 1;
@@ -107,7 +106,7 @@ common.expectWarning('DeprecationWarning',
107106
new Uint32Array(10),
108107
].forEach((buf) => {
109108
const before = Buffer.from(buf.buffer).toString('hex');
110-
cryptop.getRandomValues(buf);
109+
globalThis.crypto.getRandomValues(buf);
111110
const after = Buffer.from(buf.buffer).toString('hex');
112111
assert.notStrictEqual(before, after);
113112
});

test/parallel/test-crypto-subtle-zero-length.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ if (!common.hasCrypto)
66
common.skip('missing crypto');
77

88
const assert = require('assert');
9-
const crypto = require('crypto').webcrypto;
9+
const { subtle } = globalThis.crypto;
1010

1111
(async () => {
12-
const k = await crypto.subtle.importKey(
12+
const k = await subtle.importKey(
1313
'raw',
1414
new Uint8Array(32),
1515
{ name: 'AES-GCM' },
1616
false,
1717
[ 'encrypt', 'decrypt' ]);
1818
assert(k instanceof CryptoKey);
1919

20-
const e = await crypto.subtle.encrypt({
20+
const e = await subtle.encrypt({
2121
name: 'AES-GCM',
2222
iv: new Uint8Array(12),
2323
}, k, new Uint8Array(0));
@@ -28,7 +28,7 @@ const crypto = require('crypto').webcrypto;
2828
0x53, 0x0f, 0x8a, 0xfb, 0xc7, 0x45, 0x36, 0xb9,
2929
0xa9, 0x63, 0xb4, 0xf1, 0xc4, 0xcb, 0x73, 0x8b ]));
3030

31-
const v = await crypto.subtle.decrypt({
31+
const v = await subtle.decrypt({
3232
name: 'AES-GCM',
3333
iv: new Uint8Array(12),
3434
}, k, e);

test/parallel/test-crypto-webcrypto-aes-decrypt-tag-too-small.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ if (!common.hasCrypto)
66
common.skip('missing crypto');
77

88
const assert = require('assert');
9-
const crypto = require('crypto').webcrypto;
9+
const { subtle } = globalThis.crypto;
1010

11-
crypto.subtle.importKey(
11+
subtle.importKey(
1212
'raw',
1313
new Uint8Array(32),
1414
{
@@ -18,7 +18,7 @@ crypto.subtle.importKey(
1818
[ 'encrypt', 'decrypt' ])
1919
.then((k) => {
2020
assert.rejects(() => {
21-
return crypto.subtle.decrypt({
21+
return subtle.decrypt({
2222
name: 'AES-GCM',
2323
iv: new Uint8Array(12),
2424
}, k, new Uint8Array(0));

test/parallel/test-webcrypto-constructors.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ if (!common.hasCrypto)
77
common.skip('missing crypto');
88

99
const assert = require('assert');
10+
const { subtle } = globalThis.crypto;
1011

1112
// Test CryptoKey constructor
1213
{
@@ -138,15 +139,15 @@ const notSubtle = Reflect.construct(function() {}, [], SubtleCrypto);
138139
}
139140

140141
{
141-
globalThis.crypto.subtle.importKey(
142+
subtle.importKey(
142143
'raw',
143144
globalThis.crypto.getRandomValues(new Uint8Array(4)),
144145
'PBKDF2',
145146
false,
146147
['deriveKey'],
147148
).then((key) => {
148-
globalThis.crypto.subtle.importKey = common.mustNotCall();
149-
return globalThis.crypto.subtle.deriveKey({
149+
subtle.importKey = common.mustNotCall();
150+
return subtle.deriveKey({
150151
name: 'PBKDF2',
151152
hash: 'SHA-512',
152153
salt: globalThis.crypto.getRandomValues(new Uint8Array()),

test/parallel/test-webcrypto-cryptokey-workers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ if (!common.hasCrypto)
88
common.skip('missing crypto');
99

1010
const assert = require('assert');
11-
const { subtle } = require('crypto').webcrypto;
11+
const { subtle } = globalThis.crypto;
1212
const { once } = require('events');
1313

1414
const {

test/parallel/test-webcrypto-derivebits-cfrg.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ if (!common.hasCrypto)
66
common.skip('missing crypto');
77

88
const assert = require('assert');
9-
const { webcrypto } = require('crypto');
10-
const { subtle } = webcrypto;
9+
const { subtle } = globalThis.crypto;
1110

1211
const kTests = [
1312
{
@@ -196,7 +195,7 @@ async function prepareKeys() {
196195

197196
{
198197
// Public is a secret key
199-
const keyData = webcrypto.getRandomValues(new Uint8Array(32));
198+
const keyData = globalThis.crypto.getRandomValues(new Uint8Array(32));
200199
const key = await subtle.importKey(
201200
'raw',
202201
keyData,

test/parallel/test-webcrypto-derivebits-ecdh.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ if (!common.hasCrypto)
66
common.skip('missing crypto');
77

88
const assert = require('assert');
9-
const { webcrypto } = require('crypto');
10-
const { subtle } = webcrypto;
9+
const { subtle } = globalThis.crypto;
1110

1211
const kTests = [
1312
{
@@ -251,7 +250,7 @@ async function prepareKeys() {
251250

252251
{
253252
// Public is a secret key
254-
const keyData = webcrypto.getRandomValues(new Uint8Array(32));
253+
const keyData = globalThis.crypto.getRandomValues(new Uint8Array(32));
255254
const key = await subtle.importKey(
256255
'raw',
257256
keyData,

test/parallel/test-webcrypto-derivebits-hkdf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ if (!common.hasCrypto)
66
common.skip('missing crypto');
77

88
const assert = require('assert');
9-
const { subtle } = require('crypto').webcrypto;
9+
const { subtle } = globalThis.crypto;
1010

1111
function getDeriveKeyInfo(name, length, hash, ...usages) {
1212
return [{ name, length, hash }, usages];

test/parallel/test-webcrypto-derivebits.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ if (!common.hasCrypto)
77
common.skip('missing crypto');
88

99
const assert = require('assert');
10-
const { subtle } = require('crypto').webcrypto;
10+
const { subtle } = globalThis.crypto;
1111

1212
// This is only a partial test. The WebCrypto Web Platform Tests
1313
// will provide much greater coverage.

test/parallel/test-webcrypto-derivekey-cfrg.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ if (!common.hasCrypto)
66
common.skip('missing crypto');
77

88
const assert = require('assert');
9-
const { webcrypto } = require('crypto');
10-
const { subtle } = webcrypto;
9+
const { subtle } = globalThis.crypto;
1110

1211
const kTests = [
1312
{
@@ -168,7 +167,7 @@ async function prepareKeys() {
168167

169168
{
170169
// Public is a secret key
171-
const keyData = webcrypto.getRandomValues(new Uint8Array(32));
170+
const keyData = globalThis.crypto.getRandomValues(new Uint8Array(32));
172171
const key = await subtle.importKey(
173172
'raw',
174173
keyData,

test/parallel/test-webcrypto-derivekey-ecdh.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ if (!common.hasCrypto)
66
common.skip('missing crypto');
77

88
const assert = require('assert');
9-
const { webcrypto } = require('crypto');
10-
const { subtle } = webcrypto;
9+
const { subtle } = globalThis.crypto;
1110

1211
const kTests = [
1312
{
@@ -227,7 +226,7 @@ async function prepareKeys() {
227226

228227
{
229228
// Public is a secret key
230-
const keyData = webcrypto.getRandomValues(new Uint8Array(32));
229+
const keyData = globalThis.crypto.getRandomValues(new Uint8Array(32));
231230
const key = await subtle.importKey(
232231
'raw',
233232
keyData,

test/parallel/test-webcrypto-derivekey.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ if (!common.hasCrypto)
77
common.skip('missing crypto');
88

99
const assert = require('assert');
10-
const { webcrypto: { subtle }, KeyObject } = require('crypto');
10+
const { subtle } = globalThis.crypto;
11+
const { KeyObject } = require('crypto');
1112

1213
// This is only a partial test. The WebCrypto Web Platform Tests
1314
// will provide much greater coverage.

test/parallel/test-webcrypto-digest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ if (!common.hasCrypto)
77

88
const assert = require('assert');
99
const { Buffer } = require('buffer');
10-
const { subtle } = require('crypto').webcrypto;
10+
const { subtle } = globalThis.crypto;
1111
const { createHash } = require('crypto');
1212

1313
const kTests = [

test/parallel/test-webcrypto-encrypt-decrypt-aes.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ if (!common.hasCrypto)
66
common.skip('missing crypto');
77

88
const assert = require('assert');
9-
const { webcrypto } = require('crypto');
10-
const { subtle } = webcrypto;
9+
const { subtle } = globalThis.crypto;
1110

1211
async function testEncrypt({ keyBuffer, algorithm, plaintext, result }) {
1312
// Using a copy of plaintext to prevent tampering of the original
@@ -214,8 +213,8 @@ async function testDecrypt({ keyBuffer, algorithm, result }) {
214213
['encrypt', 'decrypt'],
215214
);
216215

217-
const iv = webcrypto.getRandomValues(new Uint8Array(12));
218-
const aad = webcrypto.getRandomValues(new Uint8Array(32));
216+
const iv = globalThis.crypto.getRandomValues(new Uint8Array(12));
217+
const aad = globalThis.crypto.getRandomValues(new Uint8Array(32));
219218

220219
const encrypted = await subtle.encrypt(
221220
{
@@ -225,7 +224,7 @@ async function testDecrypt({ keyBuffer, algorithm, result }) {
225224
tagLength: 128
226225
},
227226
secretKey,
228-
webcrypto.getRandomValues(new Uint8Array(32))
227+
globalThis.crypto.getRandomValues(new Uint8Array(32))
229228
);
230229

231230
await subtle.decrypt(

test/parallel/test-webcrypto-encrypt-decrypt-rsa.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ if (!common.hasCrypto)
66
common.skip('missing crypto');
77

88
const assert = require('assert');
9-
const { subtle } = require('crypto').webcrypto;
9+
const { subtle } = globalThis.crypto;
1010

1111
const {
1212
passing

test/parallel/test-webcrypto-encrypt-decrypt.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ if (!common.hasCrypto)
66
common.skip('missing crypto');
77

88
const assert = require('assert');
9-
const { webcrypto } = require('crypto');
10-
const { subtle } = webcrypto;
9+
const { subtle } = globalThis.crypto;
1110

1211
// This is only a partial test. The WebCrypto Web Platform Tests
1312
// will provide much greater coverage.
1413

1514
// Test Encrypt/Decrypt RSA-OAEP
1615
{
17-
const buf = webcrypto.getRandomValues(new Uint8Array(50));
16+
const buf = globalThis.crypto.getRandomValues(new Uint8Array(50));
1817

1918
async function test() {
2019
const ec = new TextEncoder();
@@ -45,8 +44,8 @@ const { subtle } = webcrypto;
4544

4645
// Test Encrypt/Decrypt AES-CTR
4746
{
48-
const buf = webcrypto.getRandomValues(new Uint8Array(50));
49-
const counter = webcrypto.getRandomValues(new Uint8Array(16));
47+
const buf = globalThis.crypto.getRandomValues(new Uint8Array(50));
48+
const counter = globalThis.crypto.getRandomValues(new Uint8Array(16));
5049

5150
async function test() {
5251
const key = await subtle.generateKey({
@@ -72,8 +71,8 @@ const { subtle } = webcrypto;
7271

7372
// Test Encrypt/Decrypt AES-CBC
7473
{
75-
const buf = webcrypto.getRandomValues(new Uint8Array(50));
76-
const iv = webcrypto.getRandomValues(new Uint8Array(16));
74+
const buf = globalThis.crypto.getRandomValues(new Uint8Array(50));
75+
const iv = globalThis.crypto.getRandomValues(new Uint8Array(16));
7776

7877
async function test() {
7978
const key = await subtle.generateKey({
@@ -99,8 +98,8 @@ const { subtle } = webcrypto;
9998

10099
// Test Encrypt/Decrypt AES-GCM
101100
{
102-
const buf = webcrypto.getRandomValues(new Uint8Array(50));
103-
const iv = webcrypto.getRandomValues(new Uint8Array(12));
101+
const buf = globalThis.crypto.getRandomValues(new Uint8Array(50));
102+
const iv = globalThis.crypto.getRandomValues(new Uint8Array(12));
104103

105104
async function test() {
106105
const key = await subtle.generateKey({

test/parallel/test-webcrypto-export-import-cfrg.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ if (!common.hasCrypto)
88

99
const assert = require('assert');
1010
const crypto = require('crypto');
11-
const { subtle } = crypto.webcrypto;
11+
const { subtle } = globalThis.crypto;
1212

1313
const keyData = {
1414
'Ed25519': {

0 commit comments

Comments
 (0)