@@ -39,10 +39,11 @@ changes:
39
39
40
40
Node.js provides an implementation of the standard [ Web Crypto API] [ ] .
41
41
42
- Use ` require('node:crypto').webcrypto ` to access this module.
42
+ Use ` globalThis.crypto ` or ` require('node:crypto').webcrypto ` to access this
43
+ module.
43
44
44
45
``` js
45
- const { subtle } = require ( ' node:crypto ' ). webcrypto ;
46
+ const { subtle } = globalThis . crypto ;
46
47
47
48
(async function () {
48
49
@@ -72,7 +73,7 @@ or asymmetric key pairs (public key and private key).
72
73
#### AES keys
73
74
74
75
``` js
75
- const { subtle } = require ( ' node:crypto ' ). webcrypto ;
76
+ const { subtle } = globalThis . crypto ;
76
77
77
78
async function generateAesKey (length = 256 ) {
78
79
const key = await subtle .generateKey ({
@@ -87,7 +88,7 @@ async function generateAesKey(length = 256) {
87
88
#### ECDSA key pairs
88
89
89
90
``` js
90
- const { subtle } = require ( ' node:crypto ' ). webcrypto ;
91
+ const { subtle } = globalThis . crypto ;
91
92
92
93
async function generateEcKey (namedCurve = ' P-521' ) {
93
94
const {
@@ -107,7 +108,7 @@ async function generateEcKey(namedCurve = 'P-521') {
107
108
> Stability: 1 - Experimental
108
109
109
110
``` js
110
- const { subtle } = require ( ' node:crypto ' ). webcrypto ;
111
+ const { subtle } = globalThis . crypto ;
111
112
112
113
async function generateEd25519Key () {
113
114
return subtle .generateKey ({
@@ -125,7 +126,7 @@ async function generateX25519Key() {
125
126
#### HMAC keys
126
127
127
128
``` js
128
- const { subtle } = require ( ' node:crypto ' ). webcrypto ;
129
+ const { subtle } = globalThis . crypto ;
129
130
130
131
async function generateHmacKey (hash = ' SHA-256' ) {
131
132
const key = await subtle .generateKey ({
@@ -140,7 +141,7 @@ async function generateHmacKey(hash = 'SHA-256') {
140
141
#### RSA key pairs
141
142
142
143
``` js
143
- const { subtle } = require ( ' node:crypto ' ). webcrypto ;
144
+ const { subtle } = globalThis . crypto ;
144
145
const publicExponent = new Uint8Array ([1 , 0 , 1 ]);
145
146
146
147
async function generateRsaKey (modulusLength = 2048 , hash = ' SHA-256' ) {
@@ -161,7 +162,7 @@ async function generateRsaKey(modulusLength = 2048, hash = 'SHA-256') {
161
162
### Encryption and decryption
162
163
163
164
``` js
164
- const crypto = require ( ' node:crypto ' ). webcrypto ;
165
+ const crypto = globalThis . crypto ;
165
166
166
167
async function aesEncrypt (plaintext ) {
167
168
const ec = new TextEncoder ();
@@ -194,7 +195,7 @@ async function aesDecrypt(ciphertext, key, iv) {
194
195
### Exporting and importing keys
195
196
196
197
``` js
197
- const { subtle } = require ( ' node:crypto ' ). webcrypto ;
198
+ const { subtle } = globalThis . crypto ;
198
199
199
200
async function generateAndExportHmacKey (format = ' jwk' , hash = ' SHA-512' ) {
200
201
const key = await subtle .generateKey ({
@@ -218,7 +219,7 @@ async function importHmacKey(keyData, format = 'jwk', hash = 'SHA-512') {
218
219
### Wrapping and unwrapping keys
219
220
220
221
``` js
221
- const { subtle } = require ( ' node:crypto ' ). webcrypto ;
222
+ const { subtle } = globalThis . crypto ;
222
223
223
224
async function generateAndWrapHmacKey (format = ' jwk' , hash = ' SHA-512' ) {
224
225
const [
@@ -261,7 +262,7 @@ async function unwrapHmacKey(
261
262
### Sign and verify
262
263
263
264
``` js
264
- const { subtle } = require ( ' node:crypto ' ). webcrypto ;
265
+ const { subtle } = globalThis . crypto ;
265
266
266
267
async function sign (key , data ) {
267
268
const ec = new TextEncoder ();
@@ -285,7 +286,7 @@ async function verify(key, signature, data) {
285
286
### Deriving bits and keys
286
287
287
288
``` js
288
- const { subtle } = require ( ' node:crypto ' ). webcrypto ;
289
+ const { subtle } = globalThis . crypto ;
289
290
290
291
async function pbkdf2 (pass , salt , iterations = 1000 , length = 256 ) {
291
292
const ec = new TextEncoder ();
@@ -328,7 +329,7 @@ async function pbkdf2Key(pass, salt, iterations = 1000, length = 256) {
328
329
### Digest
329
330
330
331
``` js
331
- const { subtle } = require ( ' node:crypto ' ). webcrypto ;
332
+ const { subtle } = globalThis . crypto ;
332
333
333
334
async function digest (data , algorithm = ' SHA-512' ) {
334
335
const ec = new TextEncoder ();
@@ -371,7 +372,7 @@ implementation and the APIs supported for each:
371
372
added: v15.0.0
372
373
-->
373
374
374
- Calling ` require('node:crypto').webcrypto ` returns an instance of the ` Crypto `
375
+ ` globalThis.crypto ` is an instance of the ` Crypto `
375
376
class. ` Crypto ` is a singleton that provides access to the remainder of the
376
377
crypto API.
377
378
0 commit comments