diff --git a/doc/api/crypto.md b/doc/api/crypto.md index 0918d599c287fc..03564234aefbc5 100644 --- a/doc/api/crypto.md +++ b/doc/api/crypto.md @@ -3836,6 +3836,18 @@ const { console.log(getHashes()); // ['DSA', 'DSA-SHA', 'DSA-SHA1', ...] ``` +### `crypto.getRandomValues(typedArray)` + + +* `typedArray` {Buffer|TypedArray|DataView|ArrayBuffer} +* Returns: {Buffer|TypedArray|DataView|ArrayBuffer} Returns `typedArray`. + +Alias for `crypto.webcrypto.getRandomValues(typedArray)` + +See [`webcrypto.getRandomValues()`][] for details. + ### `crypto.hkdf(digest, key, salt, info, keylen, callback)` + +Alias for [`webcrypto.subtle`][]. + ### `crypto.timingSafeEqual(a, b)` -> Stability: 1 - Experimental +> Stability: 2 - Stable Node.js provides an implementation of the standard [Web Crypto API][]. diff --git a/lib/crypto.js b/lib/crypto.js index e2355b0f5d53f6..c05880fe59baaf 100644 --- a/lib/crypto.js +++ b/lib/crypto.js @@ -287,6 +287,18 @@ ObjectDefineProperties(module.exports, { get() { return lazyRequire('internal/crypto/webcrypto'); } }, + subtle: { + configurable: true, + enumerable: false, + get() { return this.webcrypto.subtle; }, + }, + + getRandomValues: { + configurable: true, + enumerable: false, + get() { return this.webcrypto.getRandomValues; }, + }, + // Aliases for randomBytes are deprecated. // The ecosystem needs those to exist for backwards compatibility. prng: { diff --git a/test/parallel/test-webcrypto.js b/test/parallel/test-webcrypto.js new file mode 100644 index 00000000000000..3ec0f1cc7d6328 --- /dev/null +++ b/test/parallel/test-webcrypto.js @@ -0,0 +1,16 @@ +'use strict'; + +const common = require('../common'); + +if (!common.hasCrypto) + common.skip('missing crypto'); + +const { strictEqual } = require('assert'); +const { + webcrypto, + subtle, + getRandomValues, +} = require('crypto'); + +strictEqual(subtle, webcrypto.subtle); +strictEqual(getRandomValues, webcrypto.getRandomValues);