66
77Node.js provides an implementation of the standard [ Web Crypto API] [ ] .
88
9- Use ` require('crypto').webcrypto ` to access this module.
9+ Use ` require('crypto/web ').crypto ` to access this module.
1010
1111``` js
12- const { subtle } = require (' crypto' ). webcrypto ;
12+ const { crypto } = require (' crypto/web ' ) ;
1313
1414(async function () {
1515
16- const key = await subtle .generateKey ({
16+ const key = await crypto . subtle .generateKey ({
1717 name: ' HMAC' ,
1818 hash: ' SHA-256' ,
1919 length: 256
2020 }, true , [' sign' , ' verify' ]);
2121
22- const digest = await subtle .sign ({
22+ const digest = await crypto . subtle .sign ({
2323 name: ' HMAC'
2424 }, key, ' I love cupcakes' );
2525
@@ -36,10 +36,10 @@ or asymmetric key pairs (public key and private key).
3636#### AES keys
3737
3838``` js
39- const { subtle } = require (' crypto' ). webcrypto ;
39+ const { crypto } = require (' crypto/web ' ) ;
4040
4141async function generateAesKey (length = 256 ) {
42- const key = await subtle .generateKey ({
42+ const key = await crypto . subtle .generateKey ({
4343 name: ' AES-CBC' ,
4444 length
4545 }, true , [' encrypt' , ' decrypt' ]);
@@ -51,13 +51,13 @@ async function generateAesKey(length = 256) {
5151#### Elliptic curve key pairs
5252
5353``` js
54- const { subtle } = require (' crypto' ). webcrypto ;
54+ const { crypto } = require (' crypto/web ' ) ;
5555
5656async function generateEcKey (namedCurve = ' P-521' ) {
5757 const {
5858 publicKey ,
5959 privateKey
60- } = await subtle .generateKey ({
60+ } = await crypto . subtle .generateKey ({
6161 name: ' ECDSA' ,
6262 namedCurve,
6363 }, true , [' sign' , ' verify' ]);
@@ -69,17 +69,17 @@ async function generateEcKey(namedCurve = 'P-521') {
6969#### ED25519/ED448/X25519/X448 Elliptic curve key pairs
7070
7171``` js
72- const { subtle } = require (' crypto' ). webcrypto ;
72+ const { crypto } = require (' crypto/web ' ) ;
7373
7474async function generateEd25519Key () {
75- return subtle .generateKey ({
75+ return crypto . subtle .generateKey ({
7676 name: ' NODE-ED25519' ,
7777 namedCurve: ' NODE-ED25519' ,
7878 }, true , [' sign' , ' verify' ]);
7979}
8080
8181async function generateX25519Key () {
82- return subtle .generateKey ({
82+ return crypto . subtle .generateKey ({
8383 name: ' ECDH' ,
8484 namedCurve: ' NODE-X25519' ,
8585 }, true , [' deriveKey' ]);
@@ -89,10 +89,10 @@ async function generateX25519Key() {
8989#### HMAC keys
9090
9191``` js
92- const { subtle } = require (' crypto' ). webcrypto ;
92+ const { crypto } = require (' crypto/web ' ) ;
9393
9494async function generateHmacKey (hash = ' SHA-256' ) {
95- const key = await subtle .generateKey ({
95+ const key = await crypto . subtle .generateKey ({
9696 name: ' HMAC' ,
9797 hash
9898 }, true , [' sign' , ' verify' ]);
@@ -104,14 +104,14 @@ async function generateHmacKey(hash = 'SHA-256') {
104104#### RSA key pairs
105105
106106``` js
107- const { subtle } = require (' crypto' ). webcrypto ;
107+ const { crypto } = require (' crypto/web ' ) ;
108108const publicExponent = new Uint8Array ([1 , 0 , 1 ]);
109109
110110async function generateRsaKey (modulusLength = 2048 , hash = ' SHA-256' ) {
111111 const {
112112 publicKey ,
113113 privateKey
114- } = await subtle .generateKey ({
114+ } = await crypto . subtle .generateKey ({
115115 name: ' RSASSA-PKCS1-v1_5' ,
116116 modulusLength,
117117 publicExponent,
@@ -125,14 +125,14 @@ async function generateRsaKey(modulusLength = 2048, hash = 'SHA-256') {
125125### Encryption and decryption
126126
127127``` js
128- const { subtle , getRandomValues } = require (' crypto' ).webcrypto ;
128+ const { crypto } = require (' crypto/web ' ).crypto ;
129129
130130async function aesEncrypt (plaintext ) {
131131 const ec = new TextEncoder ();
132132 const key = await generateAesKey ();
133- const iv = getRandomValues (new Uint8Array (16 ));
133+ const iv = crypto . getRandomValues (new Uint8Array (16 ));
134134
135- const ciphertext = await subtle .encrypt ({
135+ const ciphertext = await crypto . subtle .encrypt ({
136136 name: ' AES-CBC' ,
137137 iv,
138138 }, key, ec .encode (plaintext));
@@ -146,7 +146,7 @@ async function aesEncrypt(plaintext) {
146146
147147async function aesDecrypt (ciphertext , key , iv ) {
148148 const dec = new TextDecoder ();
149- const plaintext = await subtle .decrypt ({
149+ const plaintext = await crypto . subtle .decrypt ({
150150 name: ' AES-CBC' ,
151151 iv,
152152 }, key, ciphertext);
@@ -158,19 +158,19 @@ async function aesDecrypt(ciphertext, key, iv) {
158158### Exporting and importing keys
159159
160160``` js
161- const { subtle } = require (' crypto' ). webcrypto ;
161+ const { crypto } = require (' crypto/web ' ) ;
162162
163163async function generateAndExportHmacKey (format = ' jwk' , hash = ' SHA-512' ) {
164- const key = await subtle .generateKey ({
164+ const key = await crypto . subtle .generateKey ({
165165 name: ' HMAC' ,
166166 hash
167167 }, true , [' sign' , ' verify' ]);
168168
169- return subtle .exportKey (format, key);
169+ return crypto . subtle .exportKey (format, key);
170170}
171171
172172async function importHmacKey (keyData , format = ' jwk' , hash = ' SHA-512' ) {
173- const key = await subtle .importKey (format, keyData, {
173+ const key = await crypto . subtle .importKey (format, keyData, {
174174 name: ' HMAC' ,
175175 hash
176176 }, true , [' sign' , ' verify' ]);
@@ -182,23 +182,25 @@ async function importHmacKey(keyData, format = 'jwk', hash = 'SHA-512') {
182182### Wrapping and unwrapping keys
183183
184184``` js
185- const { subtle } = require (' crypto' ). webcrypto ;
185+ const { crypto } = require (' crypto/web ' ) ;
186186
187187async function generateAndWrapHmacKey (format = ' jwk' , hash = ' SHA-512' ) {
188188 const [
189189 key,
190190 wrappingKey,
191191 ] = await Promise .all ([
192- subtle .generateKey ({
192+ crypto . subtle .generateKey ({
193193 name: ' HMAC' , hash
194194 }, true , [' sign' , ' verify' ]),
195- subtle .generateKey ({
195+ crypto . subtle .generateKey ({
196196 name: ' AES-KW' ,
197197 length: 256
198198 }, true , [' wrapKey' , ' unwrapKey' ]),
199199 ]);
200200
201- const wrappedKey = await subtle .wrapKey (format, key, wrappingKey, ' AES-KW' );
201+ const wrappedKey = await crypto .subtle .wrapKey (
202+ format, key, wrappingKey, ' AES-KW'
203+ );
202204
203205 return wrappedKey;
204206}
@@ -209,7 +211,7 @@ async function unwrapHmacKey(
209211 format = ' jwk' ,
210212 hash = ' SHA-512' ) {
211213
212- const key = await subtle .unwrapKey (
214+ const key = await crypto . subtle .unwrapKey (
213215 format,
214216 wrappedKey,
215217 unwrappingKey,
@@ -225,19 +227,19 @@ async function unwrapHmacKey(
225227### Sign and verify
226228
227229``` js
228- const { subtle } = require (' crypto' ). webcrypto ;
230+ const { crypto } = require (' crypto/web ' ) ;
229231
230232async function sign (key , data ) {
231233 const ec = new TextEncoder ();
232234 const signature =
233- await subtle .sign (' RSASSA-PKCS1-v1_5' , key, ec .encode (data));
235+ await crypto . subtle .sign (' RSASSA-PKCS1-v1_5' , key, ec .encode (data));
234236 return signature;
235237}
236238
237239async function verify (key , signature , data ) {
238240 const ec = new TextEncoder ();
239241 const verified =
240- await subtle .verify (
242+ await crypto . subtle .verify (
241243 ' RSASSA-PKCS1-v1_5' ,
242244 key,
243245 signature,
@@ -249,17 +251,17 @@ async function verify(key, signature, data) {
249251### Deriving bits and keys
250252
251253``` js
252- const { subtle } = require (' crypto' ). webcrypto ;
254+ const { crypto } = require (' crypto/web ' ) ;
253255
254256async function pbkdf2 (pass , salt , iterations = 1000 , length = 256 ) {
255257 const ec = new TextEncoder ();
256- const key = await subtle .importKey (
258+ const key = await crypto . subtle .importKey (
257259 ' raw' ,
258260 ec .encode (pass),
259261 ' PBKDF2' ,
260262 false ,
261263 [' deriveBits' ]);
262- const bits = await subtle .deriveBits ({
264+ const bits = await crypto . subtle .deriveBits ({
263265 name: ' PBKDF2' ,
264266 hash: ' SHA-512' ,
265267 salt: ec .encode (salt),
@@ -270,13 +272,13 @@ async function pbkdf2(pass, salt, iterations = 1000, length = 256) {
270272
271273async function pbkdf2Key (pass , salt , iterations = 1000 , length = 256 ) {
272274 const ec = new TextEncoder ();
273- const keyMaterial = await subtle .importKey (
275+ const keyMaterial = await crypto . subtle .importKey (
274276 ' raw' ,
275277 ec .encode (pass),
276278 ' PBKDF2' ,
277279 false ,
278280 [' deriveKey' ]);
279- const key = await subtle .deriveKey ({
281+ const key = await crypto . subtle .deriveKey ({
280282 name: ' PBKDF2' ,
281283 hash: ' SHA-512' ,
282284 salt: ec .encode (salt),
@@ -292,11 +294,11 @@ async function pbkdf2Key(pass, salt, iterations = 1000, length = 256) {
292294### Digest
293295
294296``` js
295- const { subtle } = require (' crypto' ). webcrypto ;
297+ const { crypto } = require (' crypto/web ' ) ;
296298
297299async function digest (data , algorithm = ' SHA-512' ) {
298300 const ec = new TextEncoder ();
299- const digest = await subtle .digest (algorithm, ec .encode (data));
301+ const digest = await crypto . subtle .digest (algorithm, ec .encode (data));
300302 return digest;
301303}
302304```
@@ -336,7 +338,7 @@ implementation and the APIs supported for each:
336338added: v15.0.0
337339-->
338340
339- Calling ` require('crypto').webcrypto ` returns an instance of the ` Crypto ` class.
341+ Calling ` require('crypto/web ').crypto ` returns an instance of the ` Crypto ` class.
340342` Crypto ` is a singleton that provides access to the remainder of the crypto API.
341343
342344### ` crypto.subtle `
0 commit comments