Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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>
  • Loading branch information
jasnell committed Dec 21, 2021
commit 181d08250c8567b64488b9b41f295e85e87b62a2
22 changes: 22 additions & 0 deletions doc/api/crypto.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`.
Comment thread
jasnell marked this conversation as resolved.
Outdated

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

<!-- YAML
Expand Down Expand Up @@ -5908,6 +5929,7 @@ 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
Comment thread
jasnell marked this conversation as resolved.
[`decipher.final()`]: #decipherfinaloutputencoding
[`decipher.update()`]: #decipherupdatedata-inputencoding-outputencoding
[`diffieHellman.setPublicKey()`]: #diffiehellmansetpublickeypublickey-encoding
Expand Down
23 changes: 21 additions & 2 deletions lib/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,18 @@ const {
getHashes,
setDefaultEncoding,
setEngine,
lazyRequire,
secureHeapUsed,
} = require('internal/crypto/util');
const Certificate = require('internal/crypto/certificate');

let webcrypto;
function lazyWebCrypto() {
if (webcrypto === undefined) {
webcrypto = require('internal/crypto/webcrypto');
}
Comment thread
jasnell marked this conversation as resolved.
Outdated
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 +291,19 @@ ObjectDefineProperties(module.exports, {
webcrypto: {
configurable: false,
enumerable: true,
get() { return lazyRequire('internal/crypto/webcrypto').crypto; }
get() { return lazyWebCrypto().crypto; }
Comment thread
jasnell marked this conversation as resolved.
Outdated
},

subtle: {
configurable: false,
enumerable: true,
get() { return lazyWebCrypto().crypto.subtle; }
Comment thread
jasnell marked this conversation as resolved.
Outdated
},

getRandomValues: {
configurable: false,
enumerable: true,
get() { return lazyWebCrypto().crypto.getRandomValues; }
Comment thread
jasnell marked this conversation as resolved.
Outdated
},

// Aliases for randomBytes are deprecated.
Expand Down