From 7ad069c53c550eb6530260780b48d23a9582df24 Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Fri, 16 Dec 2022 20:55:42 +0100 Subject: [PATCH] crypto: use globalThis.crypto over require('crypto').webcrypto PR-URL: https://github.com/nodejs/node/pull/45817 Reviewed-By: Yagiz Nizipli Reviewed-By: James M Snell --- .eslintrc.js | 6 ++++++ benchmark/crypto/webcrypto-digest.js | 9 +++------ test/.eslintrc.yaml | 2 ++ test/parallel/test-crypto-psychic-signatures.js | 4 ++-- test/parallel/test-crypto-random.js | 3 +-- test/parallel/test-crypto-subtle-zero-length.js | 8 ++++---- ...rypto-webcrypto-aes-decrypt-tag-too-small.js | 6 +++--- test/parallel/test-global-webcrypto-classes.js | 3 ++- test/parallel/test-global-webcrypto.js | 1 + test/parallel/test-webcrypto-constructors.js | 7 ++++--- .../test-webcrypto-cryptokey-workers.js | 2 +- test/parallel/test-webcrypto-derivebits-cfrg.js | 5 ++--- test/parallel/test-webcrypto-derivebits-ecdh.js | 5 ++--- test/parallel/test-webcrypto-derivebits-hkdf.js | 2 +- test/parallel/test-webcrypto-derivebits.js | 2 +- test/parallel/test-webcrypto-derivekey-cfrg.js | 5 ++--- test/parallel/test-webcrypto-derivekey-ecdh.js | 5 ++--- test/parallel/test-webcrypto-derivekey.js | 3 ++- test/parallel/test-webcrypto-digest.js | 2 +- .../test-webcrypto-encrypt-decrypt-aes.js | 9 ++++----- .../test-webcrypto-encrypt-decrypt-rsa.js | 2 +- test/parallel/test-webcrypto-encrypt-decrypt.js | 17 ++++++++--------- .../test-webcrypto-export-import-cfrg.js | 2 +- .../parallel/test-webcrypto-export-import-ec.js | 2 +- .../test-webcrypto-export-import-rsa.js | 2 +- test/parallel/test-webcrypto-export-import.js | 9 ++++----- test/parallel/test-webcrypto-getRandomValues.js | 2 +- test/parallel/test-webcrypto-keygen.js | 2 +- test/parallel/test-webcrypto-random.js | 12 ++++++------ .../test-webcrypto-sign-verify-ecdsa.js | 2 +- .../test-webcrypto-sign-verify-eddsa.js | 2 +- .../parallel/test-webcrypto-sign-verify-hmac.js | 2 +- test/parallel/test-webcrypto-sign-verify-rsa.js | 2 +- test/parallel/test-webcrypto-sign-verify.js | 2 +- test/parallel/test-webcrypto-wrap-unwrap.js | 2 +- test/pummel/test-webcrypto-derivebits-pbkdf2.js | 2 +- 36 files changed, 77 insertions(+), 76 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index b676485c5909ab..c608434c9d151c 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -239,6 +239,12 @@ module.exports = { selector: "CallExpression[callee.name='isNaN']", message: 'Use Number.isNaN() instead of the global isNaN() function.', }, + { + // TODO(@panva): move this to no-restricted-properties + // when https://github.com/eslint/eslint/issues/16412 is fixed + selector: "Identifier[name='webcrypto']", + message: 'Use `globalThis.crypto`.', + }, ], 'no-return-await': 'error', 'no-self-compare': 'error', diff --git a/benchmark/crypto/webcrypto-digest.js b/benchmark/crypto/webcrypto-digest.js index 4acd82878dacf2..7bfb486c69ea54 100644 --- a/benchmark/crypto/webcrypto-digest.js +++ b/benchmark/crypto/webcrypto-digest.js @@ -1,11 +1,8 @@ 'use strict'; const common = require('../common.js'); -const { - createHash, - webcrypto, -} = require('crypto'); -const { subtle } = webcrypto; +const { createHash } = require('crypto'); +const { subtle } = globalThis.crypto; const bench = common.createBenchmark(main, { sync: ['createHash', 'subtle'], @@ -48,7 +45,7 @@ function measureSubtle(n, data, method) { } function main({ n, sync, data, method }) { - data = webcrypto.getRandomValues(Buffer.alloc(data)); + data = globalThis.crypto.getRandomValues(Buffer.alloc(data)); switch (sync) { case 'createHash': return measureLegacy(n, data, method); case 'subtle': return measureSubtle(n, data, method); diff --git a/test/.eslintrc.yaml b/test/.eslintrc.yaml index b0a648738eb086..b3e39ecf45b797 100644 --- a/test/.eslintrc.yaml +++ b/test/.eslintrc.yaml @@ -49,6 +49,8 @@ rules: message: Use 'test' as debuglog value in tests. - selector: CallExpression:matches([callee.object.name="common"][callee.property.name=/^mustCall/],[callee.name="mustCall"],[callee.name="mustCallAtLeast"])>:first-child[type=/FunctionExpression$/][body.body.length=0] message: Do not use an empty function, omit the parameter altogether. + - selector: Identifier[name='webcrypto'] + message: Use `globalThis.crypto`. # Custom rules in tools/eslint-rules node-core/prefer-assert-iferror: error diff --git a/test/parallel/test-crypto-psychic-signatures.js b/test/parallel/test-crypto-psychic-signatures.js index b8e1207b5c8a17..e8228b26bef832 100644 --- a/test/parallel/test-crypto-psychic-signatures.js +++ b/test/parallel/test-crypto-psychic-signatures.js @@ -80,14 +80,14 @@ for (const [encoding, signatures] of Object.entries(vectors)) { ); // webcrypto - crypto.webcrypto.subtle.importKey( + globalThis.crypto.subtle.importKey( 'spki', keyPair.publicKey, { name: 'ECDSA', namedCurve: 'P-256' }, false, ['verify'], ).then((publicKey) => { - return crypto.webcrypto.subtle.verify( + return globalThis.crypto.subtle.verify( { name: 'ECDSA', hash: 'SHA-256' }, publicKey, signature, diff --git a/test/parallel/test-crypto-random.js b/test/parallel/test-crypto-random.js index 7b8c2c2b7a59ea..ceaa859a0e0316 100644 --- a/test/parallel/test-crypto-random.js +++ b/test/parallel/test-crypto-random.js @@ -28,7 +28,6 @@ if (!common.hasCrypto) const assert = require('assert'); const crypto = require('crypto'); -const cryptop = require('crypto').webcrypto; const { kMaxLength } = require('buffer'); const kMaxInt32 = 2 ** 31 - 1; @@ -107,7 +106,7 @@ common.expectWarning('DeprecationWarning', new Uint32Array(10), ].forEach((buf) => { const before = Buffer.from(buf.buffer).toString('hex'); - cryptop.getRandomValues(buf); + globalThis.crypto.getRandomValues(buf); const after = Buffer.from(buf.buffer).toString('hex'); assert.notStrictEqual(before, after); }); diff --git a/test/parallel/test-crypto-subtle-zero-length.js b/test/parallel/test-crypto-subtle-zero-length.js index 544374b7482a83..f5a84727b02a01 100644 --- a/test/parallel/test-crypto-subtle-zero-length.js +++ b/test/parallel/test-crypto-subtle-zero-length.js @@ -6,10 +6,10 @@ if (!common.hasCrypto) common.skip('missing crypto'); const assert = require('assert'); -const crypto = require('crypto').webcrypto; +const { subtle } = globalThis.crypto; (async () => { - const k = await crypto.subtle.importKey( + const k = await subtle.importKey( 'raw', new Uint8Array(32), { name: 'AES-GCM' }, @@ -17,7 +17,7 @@ const crypto = require('crypto').webcrypto; [ 'encrypt', 'decrypt' ]); assert(k instanceof CryptoKey); - const e = await crypto.subtle.encrypt({ + const e = await subtle.encrypt({ name: 'AES-GCM', iv: new Uint8Array(12), }, k, new Uint8Array(0)); @@ -28,7 +28,7 @@ const crypto = require('crypto').webcrypto; 0x53, 0x0f, 0x8a, 0xfb, 0xc7, 0x45, 0x36, 0xb9, 0xa9, 0x63, 0xb4, 0xf1, 0xc4, 0xcb, 0x73, 0x8b ])); - const v = await crypto.subtle.decrypt({ + const v = await subtle.decrypt({ name: 'AES-GCM', iv: new Uint8Array(12), }, k, e); diff --git a/test/parallel/test-crypto-webcrypto-aes-decrypt-tag-too-small.js b/test/parallel/test-crypto-webcrypto-aes-decrypt-tag-too-small.js index b9f9c74b2623ed..555876b3994e64 100644 --- a/test/parallel/test-crypto-webcrypto-aes-decrypt-tag-too-small.js +++ b/test/parallel/test-crypto-webcrypto-aes-decrypt-tag-too-small.js @@ -6,9 +6,9 @@ if (!common.hasCrypto) common.skip('missing crypto'); const assert = require('assert'); -const crypto = require('crypto').webcrypto; +const { subtle } = globalThis.crypto; -crypto.subtle.importKey( +subtle.importKey( 'raw', new Uint8Array(32), { @@ -18,7 +18,7 @@ crypto.subtle.importKey( [ 'encrypt', 'decrypt' ]) .then((k) => { assert.rejects(() => { - return crypto.subtle.decrypt({ + return subtle.decrypt({ name: 'AES-GCM', iv: new Uint8Array(12), }, k, new Uint8Array(0)); diff --git a/test/parallel/test-global-webcrypto-classes.js b/test/parallel/test-global-webcrypto-classes.js index ac42ff1d82e2d1..2b929680019b1c 100644 --- a/test/parallel/test-global-webcrypto-classes.js +++ b/test/parallel/test-global-webcrypto-classes.js @@ -6,8 +6,9 @@ if (!common.hasCrypto) common.skip('missing crypto'); const assert = require('assert'); -const webcrypto = require('internal/crypto/webcrypto'); +/* eslint-disable no-restricted-syntax */ +const webcrypto = require('internal/crypto/webcrypto'); assert.strictEqual(Crypto, webcrypto.Crypto); assert.strictEqual(CryptoKey, webcrypto.CryptoKey); assert.strictEqual(SubtleCrypto, webcrypto.SubtleCrypto); diff --git a/test/parallel/test-global-webcrypto.js b/test/parallel/test-global-webcrypto.js index d129bcbb756f37..2afb0ac55886d4 100644 --- a/test/parallel/test-global-webcrypto.js +++ b/test/parallel/test-global-webcrypto.js @@ -7,6 +7,7 @@ if (!common.hasCrypto) const assert = require('assert'); const crypto = require('crypto'); +/* eslint-disable no-restricted-syntax */ assert.strictEqual(globalThis.crypto, crypto.webcrypto); assert.strictEqual(Crypto, crypto.webcrypto.constructor); assert.strictEqual(SubtleCrypto, crypto.webcrypto.subtle.constructor); diff --git a/test/parallel/test-webcrypto-constructors.js b/test/parallel/test-webcrypto-constructors.js index 308f1ed29a9cb8..4a60c9325e24a1 100644 --- a/test/parallel/test-webcrypto-constructors.js +++ b/test/parallel/test-webcrypto-constructors.js @@ -6,6 +6,7 @@ if (!common.hasCrypto) common.skip('missing crypto'); const assert = require('assert'); +const { subtle } = globalThis.crypto; // Test CryptoKey constructor { @@ -137,15 +138,15 @@ const notSubtle = Reflect.construct(function() {}, [], SubtleCrypto); } { - globalThis.crypto.subtle.importKey( + subtle.importKey( 'raw', globalThis.crypto.getRandomValues(new Uint8Array(4)), 'PBKDF2', false, ['deriveKey'], ).then((key) => { - globalThis.crypto.subtle.importKey = common.mustNotCall(); - return globalThis.crypto.subtle.deriveKey({ + subtle.importKey = common.mustNotCall(); + return subtle.deriveKey({ name: 'PBKDF2', hash: 'SHA-512', salt: globalThis.crypto.getRandomValues(new Uint8Array()), diff --git a/test/parallel/test-webcrypto-cryptokey-workers.js b/test/parallel/test-webcrypto-cryptokey-workers.js index 7f54481149dd59..4f800fdb9b6871 100644 --- a/test/parallel/test-webcrypto-cryptokey-workers.js +++ b/test/parallel/test-webcrypto-cryptokey-workers.js @@ -8,7 +8,7 @@ if (!common.hasCrypto) common.skip('missing crypto'); const assert = require('assert'); -const { subtle } = require('crypto').webcrypto; +const { subtle } = globalThis.crypto; const { once } = require('events'); const { diff --git a/test/parallel/test-webcrypto-derivebits-cfrg.js b/test/parallel/test-webcrypto-derivebits-cfrg.js index 1324a5fecc4c87..104ee5f1a9f3b9 100644 --- a/test/parallel/test-webcrypto-derivebits-cfrg.js +++ b/test/parallel/test-webcrypto-derivebits-cfrg.js @@ -6,8 +6,7 @@ if (!common.hasCrypto) common.skip('missing crypto'); const assert = require('assert'); -const { webcrypto } = require('crypto'); -const { subtle } = webcrypto; +const { subtle } = globalThis.crypto; const kTests = [ { @@ -196,7 +195,7 @@ async function prepareKeys() { { // Public is a secret key - const keyData = webcrypto.getRandomValues(new Uint8Array(32)); + const keyData = globalThis.crypto.getRandomValues(new Uint8Array(32)); const key = await subtle.importKey( 'raw', keyData, diff --git a/test/parallel/test-webcrypto-derivebits-ecdh.js b/test/parallel/test-webcrypto-derivebits-ecdh.js index 2ab152ba5a7615..9405dbc2d7b8bd 100644 --- a/test/parallel/test-webcrypto-derivebits-ecdh.js +++ b/test/parallel/test-webcrypto-derivebits-ecdh.js @@ -6,8 +6,7 @@ if (!common.hasCrypto) common.skip('missing crypto'); const assert = require('assert'); -const { webcrypto } = require('crypto'); -const { subtle } = webcrypto; +const { subtle } = globalThis.crypto; const kTests = [ { @@ -251,7 +250,7 @@ async function prepareKeys() { { // Public is a secret key - const keyData = webcrypto.getRandomValues(new Uint8Array(32)); + const keyData = globalThis.crypto.getRandomValues(new Uint8Array(32)); const key = await subtle.importKey( 'raw', keyData, diff --git a/test/parallel/test-webcrypto-derivebits-hkdf.js b/test/parallel/test-webcrypto-derivebits-hkdf.js index 7897cad97168df..670a5e0a953b75 100644 --- a/test/parallel/test-webcrypto-derivebits-hkdf.js +++ b/test/parallel/test-webcrypto-derivebits-hkdf.js @@ -6,7 +6,7 @@ if (!common.hasCrypto) common.skip('missing crypto'); const assert = require('assert'); -const { subtle } = require('crypto').webcrypto; +const { subtle } = globalThis.crypto; function getDeriveKeyInfo(name, length, hash, ...usages) { return [{ name, length, hash }, usages]; diff --git a/test/parallel/test-webcrypto-derivebits.js b/test/parallel/test-webcrypto-derivebits.js index 442423954b10b2..eb09bc24f0cb82 100644 --- a/test/parallel/test-webcrypto-derivebits.js +++ b/test/parallel/test-webcrypto-derivebits.js @@ -7,7 +7,7 @@ if (!common.hasCrypto) common.skip('missing crypto'); const assert = require('assert'); -const { subtle } = require('crypto').webcrypto; +const { subtle } = globalThis.crypto; // This is only a partial test. The WebCrypto Web Platform Tests // will provide much greater coverage. diff --git a/test/parallel/test-webcrypto-derivekey-cfrg.js b/test/parallel/test-webcrypto-derivekey-cfrg.js index 90289c98efd5d5..08897aed1baa34 100644 --- a/test/parallel/test-webcrypto-derivekey-cfrg.js +++ b/test/parallel/test-webcrypto-derivekey-cfrg.js @@ -6,8 +6,7 @@ if (!common.hasCrypto) common.skip('missing crypto'); const assert = require('assert'); -const { webcrypto } = require('crypto'); -const { subtle } = webcrypto; +const { subtle } = globalThis.crypto; const kTests = [ { @@ -168,7 +167,7 @@ async function prepareKeys() { { // Public is a secret key - const keyData = webcrypto.getRandomValues(new Uint8Array(32)); + const keyData = globalThis.crypto.getRandomValues(new Uint8Array(32)); const key = await subtle.importKey( 'raw', keyData, diff --git a/test/parallel/test-webcrypto-derivekey-ecdh.js b/test/parallel/test-webcrypto-derivekey-ecdh.js index d3ba660fde6d5b..2afec3db26b34b 100644 --- a/test/parallel/test-webcrypto-derivekey-ecdh.js +++ b/test/parallel/test-webcrypto-derivekey-ecdh.js @@ -6,8 +6,7 @@ if (!common.hasCrypto) common.skip('missing crypto'); const assert = require('assert'); -const { webcrypto } = require('crypto'); -const { subtle } = webcrypto; +const { subtle } = globalThis.crypto; const kTests = [ { @@ -227,7 +226,7 @@ async function prepareKeys() { { // Public is a secret key - const keyData = webcrypto.getRandomValues(new Uint8Array(32)); + const keyData = globalThis.crypto.getRandomValues(new Uint8Array(32)); const key = await subtle.importKey( 'raw', keyData, diff --git a/test/parallel/test-webcrypto-derivekey.js b/test/parallel/test-webcrypto-derivekey.js index b819b998d217e0..558d37d90d5796 100644 --- a/test/parallel/test-webcrypto-derivekey.js +++ b/test/parallel/test-webcrypto-derivekey.js @@ -7,7 +7,8 @@ if (!common.hasCrypto) common.skip('missing crypto'); const assert = require('assert'); -const { webcrypto: { subtle }, KeyObject } = require('crypto'); +const { subtle } = globalThis.crypto; +const { KeyObject } = require('crypto'); // This is only a partial test. The WebCrypto Web Platform Tests // will provide much greater coverage. diff --git a/test/parallel/test-webcrypto-digest.js b/test/parallel/test-webcrypto-digest.js index 25b78500b7d346..ea8c258cf1c2ed 100644 --- a/test/parallel/test-webcrypto-digest.js +++ b/test/parallel/test-webcrypto-digest.js @@ -7,7 +7,7 @@ if (!common.hasCrypto) const assert = require('assert'); const { Buffer } = require('buffer'); -const { subtle } = require('crypto').webcrypto; +const { subtle } = globalThis.crypto; const { createHash } = require('crypto'); const kTests = [ diff --git a/test/parallel/test-webcrypto-encrypt-decrypt-aes.js b/test/parallel/test-webcrypto-encrypt-decrypt-aes.js index 5dfae1c5d55385..298f6d60698139 100644 --- a/test/parallel/test-webcrypto-encrypt-decrypt-aes.js +++ b/test/parallel/test-webcrypto-encrypt-decrypt-aes.js @@ -6,8 +6,7 @@ if (!common.hasCrypto) common.skip('missing crypto'); const assert = require('assert'); -const { webcrypto } = require('crypto'); -const { subtle } = webcrypto; +const { subtle } = globalThis.crypto; async function testEncrypt({ keyBuffer, algorithm, plaintext, result }) { // Using a copy of plaintext to prevent tampering of the original @@ -214,8 +213,8 @@ async function testDecrypt({ keyBuffer, algorithm, result }) { ['encrypt', 'decrypt'], ); - const iv = webcrypto.getRandomValues(new Uint8Array(12)); - const aad = webcrypto.getRandomValues(new Uint8Array(32)); + const iv = globalThis.crypto.getRandomValues(new Uint8Array(12)); + const aad = globalThis.crypto.getRandomValues(new Uint8Array(32)); const encrypted = await subtle.encrypt( { @@ -225,7 +224,7 @@ async function testDecrypt({ keyBuffer, algorithm, result }) { tagLength: 128 }, secretKey, - webcrypto.getRandomValues(new Uint8Array(32)) + globalThis.crypto.getRandomValues(new Uint8Array(32)) ); await subtle.decrypt( diff --git a/test/parallel/test-webcrypto-encrypt-decrypt-rsa.js b/test/parallel/test-webcrypto-encrypt-decrypt-rsa.js index 6af0fa727969d8..e1a10023d2bcf8 100644 --- a/test/parallel/test-webcrypto-encrypt-decrypt-rsa.js +++ b/test/parallel/test-webcrypto-encrypt-decrypt-rsa.js @@ -6,7 +6,7 @@ if (!common.hasCrypto) common.skip('missing crypto'); const assert = require('assert'); -const { subtle } = require('crypto').webcrypto; +const { subtle } = globalThis.crypto; const { passing diff --git a/test/parallel/test-webcrypto-encrypt-decrypt.js b/test/parallel/test-webcrypto-encrypt-decrypt.js index cc997e7d2e6d26..cba193b8c76594 100644 --- a/test/parallel/test-webcrypto-encrypt-decrypt.js +++ b/test/parallel/test-webcrypto-encrypt-decrypt.js @@ -6,15 +6,14 @@ if (!common.hasCrypto) common.skip('missing crypto'); const assert = require('assert'); -const { webcrypto } = require('crypto'); -const { subtle } = webcrypto; +const { subtle } = globalThis.crypto; // This is only a partial test. The WebCrypto Web Platform Tests // will provide much greater coverage. // Test Encrypt/Decrypt RSA-OAEP { - const buf = webcrypto.getRandomValues(new Uint8Array(50)); + const buf = globalThis.crypto.getRandomValues(new Uint8Array(50)); async function test() { const ec = new TextEncoder(); @@ -45,8 +44,8 @@ const { subtle } = webcrypto; // Test Encrypt/Decrypt AES-CTR { - const buf = webcrypto.getRandomValues(new Uint8Array(50)); - const counter = webcrypto.getRandomValues(new Uint8Array(16)); + const buf = globalThis.crypto.getRandomValues(new Uint8Array(50)); + const counter = globalThis.crypto.getRandomValues(new Uint8Array(16)); async function test() { const key = await subtle.generateKey({ @@ -72,8 +71,8 @@ const { subtle } = webcrypto; // Test Encrypt/Decrypt AES-CBC { - const buf = webcrypto.getRandomValues(new Uint8Array(50)); - const iv = webcrypto.getRandomValues(new Uint8Array(16)); + const buf = globalThis.crypto.getRandomValues(new Uint8Array(50)); + const iv = globalThis.crypto.getRandomValues(new Uint8Array(16)); async function test() { const key = await subtle.generateKey({ @@ -99,8 +98,8 @@ const { subtle } = webcrypto; // Test Encrypt/Decrypt AES-GCM { - const buf = webcrypto.getRandomValues(new Uint8Array(50)); - const iv = webcrypto.getRandomValues(new Uint8Array(12)); + const buf = globalThis.crypto.getRandomValues(new Uint8Array(50)); + const iv = globalThis.crypto.getRandomValues(new Uint8Array(12)); async function test() { const key = await subtle.generateKey({ diff --git a/test/parallel/test-webcrypto-export-import-cfrg.js b/test/parallel/test-webcrypto-export-import-cfrg.js index af6a7956e6716a..0552e8d6fcba9b 100644 --- a/test/parallel/test-webcrypto-export-import-cfrg.js +++ b/test/parallel/test-webcrypto-export-import-cfrg.js @@ -8,7 +8,7 @@ if (!common.hasCrypto) const assert = require('assert'); const crypto = require('crypto'); -const { subtle } = crypto.webcrypto; +const { subtle } = globalThis.crypto; const keyData = { 'Ed25519': { diff --git a/test/parallel/test-webcrypto-export-import-ec.js b/test/parallel/test-webcrypto-export-import-ec.js index ec5615b6582f88..d49d3fa032bc59 100644 --- a/test/parallel/test-webcrypto-export-import-ec.js +++ b/test/parallel/test-webcrypto-export-import-ec.js @@ -8,7 +8,7 @@ if (!common.hasCrypto) const assert = require('assert'); const crypto = require('crypto'); -const { subtle } = crypto.webcrypto; +const { subtle } = globalThis.crypto; const curves = ['P-256', 'P-384', 'P-521']; diff --git a/test/parallel/test-webcrypto-export-import-rsa.js b/test/parallel/test-webcrypto-export-import-rsa.js index 1efc69b70aa971..e2d20eaafb1f63 100644 --- a/test/parallel/test-webcrypto-export-import-rsa.js +++ b/test/parallel/test-webcrypto-export-import-rsa.js @@ -8,7 +8,7 @@ if (!common.hasCrypto) const assert = require('assert'); const crypto = require('crypto'); -const { subtle } = crypto.webcrypto; +const { subtle } = globalThis.crypto; const sizes = [1024, 2048, 4096]; diff --git a/test/parallel/test-webcrypto-export-import.js b/test/parallel/test-webcrypto-export-import.js index 02e178f72628d5..23940464b81246 100644 --- a/test/parallel/test-webcrypto-export-import.js +++ b/test/parallel/test-webcrypto-export-import.js @@ -6,12 +6,11 @@ if (!common.hasCrypto) common.skip('missing crypto'); const assert = require('assert'); -const { webcrypto } = require('crypto'); -const { subtle } = webcrypto; +const { subtle } = globalThis.crypto; { async function test() { - const keyData = webcrypto.getRandomValues(new Uint8Array(32)); + const keyData = globalThis.crypto.getRandomValues(new Uint8Array(32)); await Promise.all([1, null, undefined, {}, []].map((format) => assert.rejects( subtle.importKey(format, keyData, {}, false, ['wrapKey']), { @@ -76,7 +75,7 @@ const { subtle } = webcrypto; // Import/Export HMAC Secret Key { async function test() { - const keyData = webcrypto.getRandomValues(new Uint8Array(32)); + const keyData = globalThis.crypto.getRandomValues(new Uint8Array(32)); const key = await subtle.importKey( 'raw', keyData, { @@ -106,7 +105,7 @@ const { subtle } = webcrypto; // Import/Export AES Secret Key { async function test() { - const keyData = webcrypto.getRandomValues(new Uint8Array(32)); + const keyData = globalThis.crypto.getRandomValues(new Uint8Array(32)); const key = await subtle.importKey( 'raw', keyData, { diff --git a/test/parallel/test-webcrypto-getRandomValues.js b/test/parallel/test-webcrypto-getRandomValues.js index 049cdcc847feb1..f0fbe61a202eea 100644 --- a/test/parallel/test-webcrypto-getRandomValues.js +++ b/test/parallel/test-webcrypto-getRandomValues.js @@ -6,6 +6,6 @@ if (!common.hasCrypto) common.skip('missing crypto'); const assert = require('assert'); -const { getRandomValues } = require('crypto').webcrypto; +const { getRandomValues } = globalThis.crypto; assert.throws(() => getRandomValues(new Uint8Array()), { code: 'ERR_INVALID_THIS' }); diff --git a/test/parallel/test-webcrypto-keygen.js b/test/parallel/test-webcrypto-keygen.js index 8a94b09d3e2b88..7cb02c9863f4eb 100644 --- a/test/parallel/test-webcrypto-keygen.js +++ b/test/parallel/test-webcrypto-keygen.js @@ -9,10 +9,10 @@ if (!common.hasCrypto) const assert = require('assert'); const { types: { isCryptoKey } } = require('util'); const { - webcrypto: { subtle }, createSecretKey, KeyObject, } = require('crypto'); +const { subtle } = globalThis.crypto; const { bigIntArrayToUnsignedBigInt } = require('internal/crypto/util'); diff --git a/test/parallel/test-webcrypto-random.js b/test/parallel/test-webcrypto-random.js index 4c5d54f640f8cf..31d4884d1f653f 100644 --- a/test/parallel/test-webcrypto-random.js +++ b/test/parallel/test-webcrypto-random.js @@ -7,7 +7,7 @@ if (!common.hasCrypto) const { Buffer } = require('buffer'); const assert = require('assert'); -const { webcrypto } = require('crypto'); +const { crypto } = globalThis; [ undefined, null, '', 1, {}, [], @@ -16,14 +16,14 @@ const { webcrypto } = require('crypto'); new DataView(new ArrayBuffer(1)), ].forEach((i) => { assert.throws( - () => webcrypto.getRandomValues(i), + () => crypto.getRandomValues(i), { name: 'TypeMismatchError', code: 17 }, ); }); { const buf = new Uint8Array(0); - webcrypto.getRandomValues(buf); + crypto.getRandomValues(buf); } const intTypedConstructors = [ @@ -41,7 +41,7 @@ const intTypedConstructors = [ for (const ctor of intTypedConstructors) { const buf = new ctor(10); const before = Buffer.from(buf.buffer).toString('hex'); - webcrypto.getRandomValues(buf); + crypto.getRandomValues(buf); const after = Buffer.from(buf.buffer).toString('hex'); assert.notStrictEqual(before, after); } @@ -49,7 +49,7 @@ for (const ctor of intTypedConstructors) { { const buf = Buffer.alloc(10); const before = buf.toString('hex'); - webcrypto.getRandomValues(buf); + crypto.getRandomValues(buf); const after = buf.toString('hex'); assert.notStrictEqual(before, after); } @@ -64,7 +64,7 @@ for (const ctor of intTypedConstructors) { if (kData !== undefined) { assert.throws( - () => webcrypto.getRandomValues(kData), + () => crypto.getRandomValues(kData), { name: 'QuotaExceededError', code: 22 }, ); } diff --git a/test/parallel/test-webcrypto-sign-verify-ecdsa.js b/test/parallel/test-webcrypto-sign-verify-ecdsa.js index 4e79e4d4f846d6..67d62754c62243 100644 --- a/test/parallel/test-webcrypto-sign-verify-ecdsa.js +++ b/test/parallel/test-webcrypto-sign-verify-ecdsa.js @@ -6,7 +6,7 @@ if (!common.hasCrypto) common.skip('missing crypto'); const assert = require('assert'); -const { subtle } = require('crypto').webcrypto; +const { subtle } = globalThis.crypto; const vectors = require('../fixtures/crypto/ecdsa')(); diff --git a/test/parallel/test-webcrypto-sign-verify-eddsa.js b/test/parallel/test-webcrypto-sign-verify-eddsa.js index cec4f57a41a2a4..7f16d62c2ea71c 100644 --- a/test/parallel/test-webcrypto-sign-verify-eddsa.js +++ b/test/parallel/test-webcrypto-sign-verify-eddsa.js @@ -6,7 +6,7 @@ if (!common.hasCrypto) common.skip('missing crypto'); const assert = require('assert'); -const { subtle } = require('crypto').webcrypto; +const { subtle } = globalThis.crypto; const vectors = require('../fixtures/crypto/eddsa')(); diff --git a/test/parallel/test-webcrypto-sign-verify-hmac.js b/test/parallel/test-webcrypto-sign-verify-hmac.js index 0962773b132768..9a5ed318e72fd7 100644 --- a/test/parallel/test-webcrypto-sign-verify-hmac.js +++ b/test/parallel/test-webcrypto-sign-verify-hmac.js @@ -6,7 +6,7 @@ if (!common.hasCrypto) common.skip('missing crypto'); const assert = require('assert'); -const { subtle } = require('crypto').webcrypto; +const { subtle } = globalThis.crypto; const vectors = require('../fixtures/crypto/hmac')(); diff --git a/test/parallel/test-webcrypto-sign-verify-rsa.js b/test/parallel/test-webcrypto-sign-verify-rsa.js index 60815c5ea0451d..bd727f2f998fa7 100644 --- a/test/parallel/test-webcrypto-sign-verify-rsa.js +++ b/test/parallel/test-webcrypto-sign-verify-rsa.js @@ -6,7 +6,7 @@ if (!common.hasCrypto) common.skip('missing crypto'); const assert = require('assert'); -const { subtle } = require('crypto').webcrypto; +const { subtle } = globalThis.crypto; const rsa_pkcs = require('../fixtures/crypto/rsa_pkcs'); const rsa_pss = require('../fixtures/crypto/rsa_pss'); diff --git a/test/parallel/test-webcrypto-sign-verify.js b/test/parallel/test-webcrypto-sign-verify.js index 6c6b15781549a4..de736102bdcb71 100644 --- a/test/parallel/test-webcrypto-sign-verify.js +++ b/test/parallel/test-webcrypto-sign-verify.js @@ -6,7 +6,7 @@ if (!common.hasCrypto) common.skip('missing crypto'); const assert = require('assert'); -const { subtle } = require('crypto').webcrypto; +const { subtle } = globalThis.crypto; // This is only a partial test. The WebCrypto Web Platform Tests // will provide much greater coverage. diff --git a/test/parallel/test-webcrypto-wrap-unwrap.js b/test/parallel/test-webcrypto-wrap-unwrap.js index 1094845c73e143..aae1ac6c573b1f 100644 --- a/test/parallel/test-webcrypto-wrap-unwrap.js +++ b/test/parallel/test-webcrypto-wrap-unwrap.js @@ -6,7 +6,7 @@ if (!common.hasCrypto) common.skip('missing crypto'); const assert = require('assert'); -const { subtle } = require('crypto').webcrypto; +const { subtle } = globalThis.crypto; const kWrappingData = { 'RSA-OAEP': { diff --git a/test/pummel/test-webcrypto-derivebits-pbkdf2.js b/test/pummel/test-webcrypto-derivebits-pbkdf2.js index c4b4f44f7064ad..243856d372774a 100644 --- a/test/pummel/test-webcrypto-derivebits-pbkdf2.js +++ b/test/pummel/test-webcrypto-derivebits-pbkdf2.js @@ -11,7 +11,7 @@ if (common.isPi) { } const assert = require('assert'); -const { subtle } = require('crypto').webcrypto; +const { subtle } = globalThis.crypto; function getDeriveKeyInfo(name, length, hash, ...usages) { return [{ name, length, hash }, usages];