@@ -352,6 +352,73 @@ async function digest(data, algorithm = 'SHA-512') {
352
352
}
353
353
```
354
354
355
+ ### Checking for runtime algorithm support
356
+
357
+ > Stability: 1.0 - Early development. SubleCrypto.supports is an experimental
358
+ > implementation based on [ Modern Algorithms in the Web Cryptography API] [ ]
359
+
360
+ This example derives a key from a password using Argon2, if available,
361
+ or PBKDF2, otherwise; and then encrypts and decrypts some text with it
362
+ using AES-OCB, if available, and AES-GCM, otherwise.
363
+
364
+ ``` mjs
365
+ const password = ' correct horse battery staple' ;
366
+ const derivationAlg =
367
+ SubtleCrypto .supports ? .(' importKey' , ' Argon2id' ) ?
368
+ ' Argon2id' :
369
+ ' PBKDF2' ;
370
+ const encryptionAlg =
371
+ SubtleCrypto .supports ? .(' importKey' , ' AES-OCB' ) ?
372
+ ' AES-OCB' :
373
+ ' AES-GCM' ;
374
+ const passwordKey = await crypto .subtle .importKey (
375
+ ' raw' ,
376
+ new TextEncoder ().encode (password),
377
+ derivationAlg,
378
+ false ,
379
+ [' deriveKey' ],
380
+ );
381
+ const nonce = crypto .getRandomValues (new Uint8Array (16 ));
382
+ const derivationParams =
383
+ derivationAlg === ' Argon2id' ?
384
+ {
385
+ nonce,
386
+ parallelism: 4 ,
387
+ memory: 2 ** 21 ,
388
+ passes: 1 ,
389
+ } :
390
+ {
391
+ salt: nonce,
392
+ iterations: 100_000 ,
393
+ hash: ' SHA-256' ,
394
+ };
395
+ const key = await crypto .subtle .deriveKey (
396
+ {
397
+ name: derivationAlg,
398
+ ... derivationParams,
399
+ },
400
+ passwordKey,
401
+ {
402
+ name: encryptionAlg,
403
+ length: 256 ,
404
+ },
405
+ false ,
406
+ [' encrypt' , ' decrypt' ],
407
+ );
408
+ const plaintext = ' Hello, world!' ;
409
+ const iv = crypto .getRandomValues (new Uint8Array (16 ));
410
+ const encrypted = await crypto .subtle .encrypt (
411
+ { name: encryptionAlg, iv },
412
+ key,
413
+ new TextEncoder ().encode (plaintext),
414
+ );
415
+ const decrypted = new TextDecoder ().decode (await crypto .subtle .decrypt (
416
+ { name: encryptionAlg, iv },
417
+ key,
418
+ encrypted,
419
+ ));
420
+ ` ` `
421
+
355
422
## Algorithm matrix
356
423
357
424
The table details the algorithms supported by the Node.js Web Crypto API
@@ -550,6 +617,28 @@ added: v15.0.0
550
617
added: v15.0.0
551
618
-->
552
619
620
+ ### Static method: ` SubtleCrypto .supports (operation, algorithm[, lengthOrAdditionalAlgorithm])`
621
+
622
+ > Stability: 1.0 - Early development. SubleCrypto.supports is an experimental
623
+ > implementation based on [Modern Algorithms in the Web Cryptography API][]
624
+
625
+ <!-- YAML
626
+ added: REPLACEME
627
+ -->
628
+
629
+ <!--lint disable maximum-line-length remark-lint-->
630
+
631
+ * ` operation` {string} "encrypt", "decrypt", "sign", "verify", "digest", "generateKey", "deriveKey", "deriveBits", "importKey", "exportKey", "wrapKey", or "unwrapKey"
632
+ * ` algorithm` {string|Algorithm|AesCbcParams|AesCtrParams|AesGcmParams|AesKeyGenParams|EcdhKeyDeriveParams|EcdsaParams|EcKeyGenParams|EcKeyImportParams|Ed448Params|HkdfParams|HmacImportParams|HmacKeyGenParams|Pbkdf2Params|RsaHashedImportParams|RsaHashedKeyGenParams|RsaOaepParams|RsaPssParams}
633
+ * ` lengthOrAdditionalAlgorithm` {null|number|string|Algorithm|AesCbcParams|AesCtrParams|AesDerivedKeyParams|AesGcmParams|AesKeyGenParams|EcdhKeyDeriveParams|EcdsaParams|EcKeyGenParams|EcKeyImportParams|Ed448Params|HkdfParams|HmacImportParams|HmacKeyGenParams|Pbkdf2Params|RsaHashedImportParams|RsaHashedKeyGenParams|RsaOaepParams|RsaPssParams} Depending on the operation this is either ignored, the value of the length argument when operation is "deriveBits", the algorithm of key to be derived when operation is "deriveKey", the algorithm of key to be exported before wrapping when operation is "wrapKey", or the algorithm of key to be imported after unwrapping when operation is "unwrapKey". **Default:** ` null ` when operation is "deriveBits", ` undefined ` otherwise.
634
+ * Returns: {boolean} Indicating whether the implementation supports the given operation
635
+
636
+ <!--lint enable maximum-line-length remark-lint-->
637
+
638
+ Allows feature detection in Web Crypto API, which can be used to detect whether
639
+ a given algorithm identifier (including any of its parameters) is supported for
640
+ the given operation.
641
+
553
642
### ` subtle .decrypt (algorithm, key, data)`
554
643
555
644
<!-- YAML
@@ -1808,6 +1897,7 @@ The length (in bytes) of the random salt to use.
1808
1897
1809
1898
[JSON Web Key]: https://tools.ietf.org/html/rfc7517
1810
1899
[Key usages]: #cryptokeyusages
1900
+ [Modern Algorithms in the Web Cryptography API]: https://wicg.github.io/webcrypto-modern-algos/
1811
1901
[NIST SP 800-38D]: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
1812
1902
[RFC 4122]: https://www.rfc-editor.org/rfc/rfc4122.txt
1813
1903
[Secure Curves in the Web Cryptography API]: https://wicg.github.io/webcrypto-secure-curves/
0 commit comments