Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 46dcfb3

Browse filesBrowse files
authored
doc,crypto: update webcrypto docs for global access
PR-URL: #44723 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 6c8c3d8 commit 46dcfb3
Copy full SHA for 46dcfb3

File tree

Expand file treeCollapse file tree

1 file changed

+15
-14
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+15
-14
lines changed
Open diff view settings
Collapse file

‎doc/api/webcrypto.md‎

Copy file name to clipboardExpand all lines: doc/api/webcrypto.md
+15-14Lines changed: 15 additions & 14 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ changes:
3939
4040
Node.js provides an implementation of the standard [Web Crypto API][].
4141

42-
Use `require('node:crypto').webcrypto` to access this module.
42+
Use `globalThis.crypto` or `require('node:crypto').webcrypto` to access this
43+
module.
4344

4445
```js
45-
const { subtle } = require('node:crypto').webcrypto;
46+
const { subtle } = globalThis.crypto;
4647

4748
(async function() {
4849

@@ -72,7 +73,7 @@ or asymmetric key pairs (public key and private key).
7273
#### AES keys
7374

7475
```js
75-
const { subtle } = require('node:crypto').webcrypto;
76+
const { subtle } = globalThis.crypto;
7677

7778
async function generateAesKey(length = 256) {
7879
const key = await subtle.generateKey({
@@ -87,7 +88,7 @@ async function generateAesKey(length = 256) {
8788
#### ECDSA key pairs
8889

8990
```js
90-
const { subtle } = require('node:crypto').webcrypto;
91+
const { subtle } = globalThis.crypto;
9192

9293
async function generateEcKey(namedCurve = 'P-521') {
9394
const {
@@ -107,7 +108,7 @@ async function generateEcKey(namedCurve = 'P-521') {
107108
> Stability: 1 - Experimental
108109
109110
```js
110-
const { subtle } = require('node:crypto').webcrypto;
111+
const { subtle } = globalThis.crypto;
111112

112113
async function generateEd25519Key() {
113114
return subtle.generateKey({
@@ -125,7 +126,7 @@ async function generateX25519Key() {
125126
#### HMAC keys
126127

127128
```js
128-
const { subtle } = require('node:crypto').webcrypto;
129+
const { subtle } = globalThis.crypto;
129130

130131
async function generateHmacKey(hash = 'SHA-256') {
131132
const key = await subtle.generateKey({
@@ -140,7 +141,7 @@ async function generateHmacKey(hash = 'SHA-256') {
140141
#### RSA key pairs
141142

142143
```js
143-
const { subtle } = require('node:crypto').webcrypto;
144+
const { subtle } = globalThis.crypto;
144145
const publicExponent = new Uint8Array([1, 0, 1]);
145146

146147
async function generateRsaKey(modulusLength = 2048, hash = 'SHA-256') {
@@ -161,7 +162,7 @@ async function generateRsaKey(modulusLength = 2048, hash = 'SHA-256') {
161162
### Encryption and decryption
162163

163164
```js
164-
const crypto = require('node:crypto').webcrypto;
165+
const crypto = globalThis.crypto;
165166

166167
async function aesEncrypt(plaintext) {
167168
const ec = new TextEncoder();
@@ -194,7 +195,7 @@ async function aesDecrypt(ciphertext, key, iv) {
194195
### Exporting and importing keys
195196

196197
```js
197-
const { subtle } = require('node:crypto').webcrypto;
198+
const { subtle } = globalThis.crypto;
198199

199200
async function generateAndExportHmacKey(format = 'jwk', hash = 'SHA-512') {
200201
const key = await subtle.generateKey({
@@ -218,7 +219,7 @@ async function importHmacKey(keyData, format = 'jwk', hash = 'SHA-512') {
218219
### Wrapping and unwrapping keys
219220

220221
```js
221-
const { subtle } = require('node:crypto').webcrypto;
222+
const { subtle } = globalThis.crypto;
222223

223224
async function generateAndWrapHmacKey(format = 'jwk', hash = 'SHA-512') {
224225
const [
@@ -261,7 +262,7 @@ async function unwrapHmacKey(
261262
### Sign and verify
262263

263264
```js
264-
const { subtle } = require('node:crypto').webcrypto;
265+
const { subtle } = globalThis.crypto;
265266

266267
async function sign(key, data) {
267268
const ec = new TextEncoder();
@@ -285,7 +286,7 @@ async function verify(key, signature, data) {
285286
### Deriving bits and keys
286287

287288
```js
288-
const { subtle } = require('node:crypto').webcrypto;
289+
const { subtle } = globalThis.crypto;
289290

290291
async function pbkdf2(pass, salt, iterations = 1000, length = 256) {
291292
const ec = new TextEncoder();
@@ -328,7 +329,7 @@ async function pbkdf2Key(pass, salt, iterations = 1000, length = 256) {
328329
### Digest
329330

330331
```js
331-
const { subtle } = require('node:crypto').webcrypto;
332+
const { subtle } = globalThis.crypto;
332333

333334
async function digest(data, algorithm = 'SHA-512') {
334335
const ec = new TextEncoder();
@@ -371,7 +372,7 @@ implementation and the APIs supported for each:
371372
added: v15.0.0
372373
-->
373374

374-
Calling `require('node:crypto').webcrypto` returns an instance of the `Crypto`
375+
`globalThis.crypto` is an instance of the `Crypto`
375376
class. `Crypto` is a singleton that provides access to the remainder of the
376377
crypto API.
377378

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.