Skip to content

Commit

Permalink
crypto: alias webcrypto.subtle and webcrypto.getRandomValues on crypto
Browse files Browse the repository at this point in the history
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: #41266
Reviewed-By: Filip Skokan <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
  • Loading branch information
jasnell authored and targos committed Jan 14, 2022
1 parent 9c41247 commit d62fe31
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
23 changes: 23 additions & 0 deletions doc/api/crypto.md
Original file line number Diff line number Diff line change
Expand Up @@ -4051,6 +4051,17 @@ const {
console.log(getHashes()); // ['DSA', 'DSA-SHA', 'DSA-SHA1', ...]
```

### `crypto.getRandomValues(typedArray)`

<!-- YAML
added: REPLACEME
-->

* `typedArray` {Buffer|TypedArray|DataView|ArrayBuffer}
* Returns: {Buffer|TypedArray|DataView|ArrayBuffer} Returns `typedArray`.

A convenient alias for [`crypto.webcrypto.getRandomValues()`][].

### `crypto.hkdf(digest, ikm, salt, info, keylen, callback)`

<!-- YAML
Expand Down Expand Up @@ -5230,6 +5241,16 @@ additional properties can be passed:

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

### `crypto.subtle`

<!-- YAML
added: REPLACEME
-->

* Type: {SubtleCrypto}

A convenient alias for [`crypto.webcrypto.subtle`][].

### `crypto.timingSafeEqual(a, b)`

<!-- YAML
Expand Down Expand Up @@ -5945,6 +5966,8 @@ See the [list of SSL OP Flags][] for details.
[`crypto.randomBytes()`]: #cryptorandombytessize-callback
[`crypto.randomFill()`]: #cryptorandomfillbuffer-offset-size-callback
[`crypto.scrypt()`]: #cryptoscryptpassword-salt-keylen-options-callback
[`crypto.webcrypto.getRandomValues()`]: webcrypto.md#cryptogetrandomvaluestypedarray
[`crypto.webcrypto.subtle`]: webcrypto.md#class-subtlecrypto
[`decipher.final()`]: #decipherfinaloutputencoding
[`decipher.update()`]: #decipherupdatedata-inputencoding-outputencoding
[`diffieHellman.setPublicKey()`]: #diffiehellmansetpublickeypublickey-encoding
Expand Down
24 changes: 22 additions & 2 deletions lib/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,16 @@ const {
getHashes,
setDefaultEncoding,
setEngine,
lazyRequire,
secureHeapUsed,
} = require('internal/crypto/util');
const Certificate = require('internal/crypto/certificate');

let webcrypto;
function lazyWebCrypto() {
webcrypto ??= require('internal/crypto/webcrypto');
return webcrypto;
}

// These helper functions are needed because the constructors can
// use new, in which case V8 cannot inline the recursive constructor call
function createHash(algorithm, options) {
Expand Down Expand Up @@ -284,7 +289,22 @@ ObjectDefineProperties(module.exports, {
webcrypto: {
configurable: false,
enumerable: true,
get() { return lazyRequire('internal/crypto/webcrypto').crypto; }
get() { return lazyWebCrypto().crypto; },
set: undefined,
},

subtle: {
configurable: false,
enumerable: true,
get() { return lazyWebCrypto().crypto.subtle; },
set: undefined,
},

getRandomValues: {
configurable: false,
enumerable: true,
get() { return lazyWebCrypto().crypto.getRandomValues; },
set: undefined,
},

// Aliases for randomBytes are deprecated.
Expand Down

0 comments on commit d62fe31

Please sign in to comment.