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: https://github.com/nodejs/node/pull/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>
This commit is contained in:
James M Snell 2021-12-27 06:48:59 -08:00 committed by GitHub
parent 67cd4a61ac
commit 353532b9c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 2 deletions

View File

@ -4015,6 +4015,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
@ -5194,6 +5205,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
@ -5908,6 +5929,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

View File

@ -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) {
@ -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.