Skip to content

Commit

Permalink
crypto: implement webcrypto.randomUUID
Browse files Browse the repository at this point in the history
Refs: https://wicg.github.io/uuid/
Refs: https://www.chromestatus.com/feature/5689159362543616

PR-URL: #39648
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
  • Loading branch information
targos authored and danielleadams committed Aug 16, 2021
1 parent 81e62f6 commit 93a904d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
11 changes: 11 additions & 0 deletions doc/api/webcrypto.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,16 @@ filled with random values, and a reference to `typedArray` is returned.

An error will be thrown if the given `typedArray` is larger than 65,536 bytes.

### `crypto.randomUUID()`
<!-- YAML
added: REPLACEME
-->

* Returns: {string}

Generates a random [RFC 4122][] Version 4 UUID. The UUID is generated using a
cryptographic pseudorandom number generator.

## Class: `CryptoKey`
<!-- YAML
added: v15.0.0
Expand Down Expand Up @@ -1771,4 +1781,5 @@ added: v15.0.0

[JSON Web Key]: https://tools.ietf.org/html/rfc7517
[Key usages]: #webcrypto_cryptokey_usages
[RFC 4122]: https://www.rfc-editor.org/rfc/rfc4122.txt
[Web Crypto API]: https://www.w3.org/TR/WebCryptoAPI/
2 changes: 1 addition & 1 deletion lib/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ ObjectDefineProperties(module.exports, {
webcrypto: {
configurable: false,
enumerable: true,
get() { return lazyRequire('internal/crypto/webcrypto'); }
get() { return lazyRequire('internal/crypto/webcrypto').crypto; }
},

// Aliases for randomBytes are deprecated.
Expand Down
15 changes: 14 additions & 1 deletion lib/internal/crypto/webcrypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ const {

const {
getRandomValues,
randomUUID: _randomUUID,
} = require('internal/crypto/random');

const randomUUID = () => _randomUUID();

async function generateKey(
algorithm,
extractable,
Expand Down Expand Up @@ -705,6 +708,12 @@ ObjectDefineProperties(
writable: true,
value: getRandomValues,
},
randomUUID: {
enumerable: true,
configurable: true,
writable: true,
value: randomUUID,
},
CryptoKey: {
enumerable: true,
configurable: true,
Expand Down Expand Up @@ -795,4 +804,8 @@ ObjectDefineProperties(
}
});

module.exports = crypto;
module.exports = {
Crypto,
SubtleCrypto,
crypto,
};

0 comments on commit 93a904d

Please sign in to comment.