Skip to content

Commit 1d660d4

Browse files
jasnellLinkgoron
authored andcommitted
crypto: alias webcrypto.subtle and webcrypto.getRandomValues on crypto
The aliases allow code written to assume that `crypto.subtle` and `crypto.getRandomValues()` exist on the `crypto` global to just work. Signed-off-by: James M Snell <[email protected]> PR-URL: nodejs#41266 Reviewed-By: Filip Skokan <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent ff6d855 commit 1d660d4

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

doc/api/crypto.md

+23
Original file line numberDiff line numberDiff line change
@@ -4015,6 +4015,17 @@ const {
40154015
console.log(getHashes()); // ['DSA', 'DSA-SHA', 'DSA-SHA1', ...]
40164016
```
40174017

4018+
### `crypto.getRandomValues(typedArray)`
4019+
4020+
<!-- YAML
4021+
added: REPLACEME
4022+
-->
4023+
4024+
* `typedArray` {Buffer|TypedArray|DataView|ArrayBuffer}
4025+
* Returns: {Buffer|TypedArray|DataView|ArrayBuffer} Returns `typedArray`.
4026+
4027+
A convenient alias for [`crypto.webcrypto.getRandomValues()`][].
4028+
40184029
### `crypto.hkdf(digest, ikm, salt, info, keylen, callback)`
40194030

40204031
<!-- YAML
@@ -5194,6 +5205,16 @@ additional properties can be passed:
51945205

51955206
If the `callback` function is provided this function uses libuv's threadpool.
51965207

5208+
### `crypto.subtle`
5209+
5210+
<!-- YAML
5211+
added: REPLACEME
5212+
-->
5213+
5214+
* Type: {SubtleCrypto}
5215+
5216+
A convenient alias for [`crypto.webcrypto.subtle`][].
5217+
51975218
### `crypto.timingSafeEqual(a, b)`
51985219

51995220
<!-- YAML
@@ -5908,6 +5929,8 @@ See the [list of SSL OP Flags][] for details.
59085929
[`crypto.randomBytes()`]: #cryptorandombytessize-callback
59095930
[`crypto.randomFill()`]: #cryptorandomfillbuffer-offset-size-callback
59105931
[`crypto.scrypt()`]: #cryptoscryptpassword-salt-keylen-options-callback
5932+
[`crypto.webcrypto.getRandomValues()`]: webcrypto.md#cryptogetrandomvaluestypedarray
5933+
[`crypto.webcrypto.subtle`]: webcrypto.md#class-subtlecrypto
59115934
[`decipher.final()`]: #decipherfinaloutputencoding
59125935
[`decipher.update()`]: #decipherupdatedata-inputencoding-outputencoding
59135936
[`diffieHellman.setPublicKey()`]: #diffiehellmansetpublickeypublickey-encoding

lib/crypto.js

+22-2
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,16 @@ const {
119119
getHashes,
120120
setDefaultEncoding,
121121
setEngine,
122-
lazyRequire,
123122
secureHeapUsed,
124123
} = require('internal/crypto/util');
125124
const Certificate = require('internal/crypto/certificate');
126125

126+
let webcrypto;
127+
function lazyWebCrypto() {
128+
webcrypto ??= require('internal/crypto/webcrypto');
129+
return webcrypto;
130+
}
131+
127132
// These helper functions are needed because the constructors can
128133
// use new, in which case V8 cannot inline the recursive constructor call
129134
function createHash(algorithm, options) {
@@ -284,7 +289,22 @@ ObjectDefineProperties(module.exports, {
284289
webcrypto: {
285290
configurable: false,
286291
enumerable: true,
287-
get() { return lazyRequire('internal/crypto/webcrypto').crypto; }
292+
get() { return lazyWebCrypto().crypto; },
293+
set: undefined,
294+
},
295+
296+
subtle: {
297+
configurable: false,
298+
enumerable: true,
299+
get() { return lazyWebCrypto().crypto.subtle; },
300+
set: undefined,
301+
},
302+
303+
getRandomValues: {
304+
configurable: false,
305+
enumerable: true,
306+
get() { return lazyWebCrypto().crypto.getRandomValues; },
307+
set: undefined,
288308
},
289309

290310
// Aliases for randomBytes are deprecated.

0 commit comments

Comments
 (0)