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