@@ -351,6 +351,74 @@ async function digest(data, algorithm = 'SHA-512') {
351
351
}
352
352
```
353
353
354
+ ### Checking for runtime algorithm support
355
+
356
+ > Stability: 1.0 - Early development. SubleCrypto.supports is an experimental
357
+ > implementation based on [ Modern Algorithms in the Web Cryptography API] [ ] as
358
+ > of 8 January 2025
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
+
354
422
## Algorithm matrix
355
423
356
424
The table details the algorithms supported by the Node.js Web Crypto API
@@ -549,6 +617,28 @@ added: v15.0.0
549
617
added: v15.0.0
550
618
-->
551
619
620
+ ### Static method: ` SubtleCrypto .supports (operation, algorithm[, lengthOrAdditionalAlgorithm])`
621
+
622
+ > Stability: 1.0 - Early development. An experimental implementation of SubtleCrypto.supports from
623
+ > [Modern Algorithms in the Web Cryptography API][] as of 8 January 2025
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|AlgorithmIdentifier|AesCbcParams|AesCtrParams|AesGcmParams|AesKeyGenParams|EcdhKeyDeriveParams|EcdsaParams|EcKeyGenParams|EcKeyImportParams|Ed448Params|HkdfParams|HmacImportParams|HmacKeyGenParams|Pbkdf2Params|RsaHashedImportParams|RsaHashedKeyGenParams|RsaOaepParams|RsaPssParams}
633
+ * ` lengthOrAdditionalAlgorithm` : {null|number|string|AlgorithmIdentifier|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
+
552
642
### ` subtle .decrypt (algorithm, key, data)`
553
643
554
644
<!-- YAML
@@ -1653,6 +1743,7 @@ The length (in bytes) of the random salt to use.
1653
1743
1654
1744
[JSON Web Key]: https://tools.ietf.org/html/rfc7517
1655
1745
[Key usages]: #cryptokeyusages
1746
+ [Modern Algorithms in the Web Cryptography API]: https://twiss.github.io/webcrypto-modern-algos/
1656
1747
[NIST SP 800-38D]: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
1657
1748
[RFC 4122]: https://www.rfc-editor.org/rfc/rfc4122.txt
1658
1749
[Secure Curves in the Web Cryptography API]: https://wicg.github.io/webcrypto-secure-curves/
0 commit comments