Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d62fe31

Browse filesBrowse files
jasnelltargos
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 <jasnell@gmail.com> PR-URL: #41266 Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 9c41247 commit d62fe31
Copy full SHA for d62fe31

File tree

Expand file treeCollapse file tree

2 files changed

+45
-2
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+45
-2
lines changed
Open diff view settings
Collapse file

‎doc/api/crypto.md‎

Copy file name to clipboardExpand all lines: doc/api/crypto.md
+23Lines changed: 23 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -4051,6 +4051,17 @@ const {
40514051
console.log(getHashes()); // ['DSA', 'DSA-SHA', 'DSA-SHA1', ...]
40524052
```
40534053

4054+
### `crypto.getRandomValues(typedArray)`
4055+
4056+
<!-- YAML
4057+
added: REPLACEME
4058+
-->
4059+
4060+
* `typedArray` {Buffer|TypedArray|DataView|ArrayBuffer}
4061+
* Returns: {Buffer|TypedArray|DataView|ArrayBuffer} Returns `typedArray`.
4062+
4063+
A convenient alias for [`crypto.webcrypto.getRandomValues()`][].
4064+
40544065
### `crypto.hkdf(digest, ikm, salt, info, keylen, callback)`
40554066

40564067
<!-- YAML
@@ -5230,6 +5241,16 @@ additional properties can be passed:
52305241

52315242
If the `callback` function is provided this function uses libuv's threadpool.
52325243

5244+
### `crypto.subtle`
5245+
5246+
<!-- YAML
5247+
added: REPLACEME
5248+
-->
5249+
5250+
* Type: {SubtleCrypto}
5251+
5252+
A convenient alias for [`crypto.webcrypto.subtle`][].
5253+
52335254
### `crypto.timingSafeEqual(a, b)`
52345255

52355256
<!-- YAML
@@ -5945,6 +5966,8 @@ See the [list of SSL OP Flags][] for details.
59455966
[`crypto.randomBytes()`]: #cryptorandombytessize-callback
59465967
[`crypto.randomFill()`]: #cryptorandomfillbuffer-offset-size-callback
59475968
[`crypto.scrypt()`]: #cryptoscryptpassword-salt-keylen-options-callback
5969+
[`crypto.webcrypto.getRandomValues()`]: webcrypto.md#cryptogetrandomvaluestypedarray
5970+
[`crypto.webcrypto.subtle`]: webcrypto.md#class-subtlecrypto
59485971
[`decipher.final()`]: #decipherfinaloutputencoding
59495972
[`decipher.update()`]: #decipherupdatedata-inputencoding-outputencoding
59505973
[`diffieHellman.setPublicKey()`]: #diffiehellmansetpublickeypublickey-encoding
Collapse file

‎lib/crypto.js‎

Copy file name to clipboardExpand all lines: lib/crypto.js
+22-2Lines changed: 22 additions & 2 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.