Skip to content

Commit

Permalink
crypto: implement webcrypto.randomUUID
Browse files Browse the repository at this point in the history
  • Loading branch information
targos committed Aug 3, 2021
1 parent 533cafc commit 43aad6b
Show file tree
Hide file tree
Showing 75 changed files with 7,547 additions and 5 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,
};
3 changes: 2 additions & 1 deletion test/fixtures/wpt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ Last update:
- html/webappapis/atob: https://github.com/web-platform-tests/wpt/tree/f267e1dca6/html/webappapis/atob
- html/webappapis/microtask-queuing: https://github.com/web-platform-tests/wpt/tree/2c5c3c4c27/html/webappapis/microtask-queuing
- html/webappapis/timers: https://github.com/web-platform-tests/wpt/tree/5873f2d8f1/html/webappapis/timers
- interfaces: https://github.com/web-platform-tests/wpt/tree/80a4176623/interfaces
- interfaces: https://github.com/web-platform-tests/wpt/tree/fc086c82d5/interfaces
- performance-timeline: https://github.com/web-platform-tests/wpt/tree/17ebc3aea0/performance-timeline
- resources: https://github.com/web-platform-tests/wpt/tree/972ca5b669/resources
- streams: https://github.com/web-platform-tests/wpt/tree/8f60d94439/streams
- url: https://github.com/web-platform-tests/wpt/tree/77d54aa9e0/url
- user-timing: https://github.com/web-platform-tests/wpt/tree/df24fb604e/user-timing
- WebCryptoAPI: https://github.com/web-platform-tests/wpt/tree/cdd0f03df4/WebCryptoAPI

[Web Platform Tests]: https://github.com/web-platform-tests/wpt
[`git node wpt`]: https://github.com/nodejs/node-core-utils/blob/main/docs/git-node.md#git-node-wpt
3 changes: 3 additions & 0 deletions test/fixtures/wpt/WebCryptoAPI/META.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
spec: https://w3c.github.io/webcrypto/
suggested_reviewers:
- twiss
1 change: 1 addition & 0 deletions test/fixtures/wpt/WebCryptoAPI/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Directory for Crypto API tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// META: title=WebCryptoAPI: deriveBits() Using ECDH
// META: script=ecdh_bits.js

// Define subtests from a `promise_test` to ensure the harness does not
// complete before the subtests are available. `explicit_done` cannot be used
// for this purpose because the global `done` function is automatically invoked
// by the WPT infrastructure in dedicated worker tests defined using the
// "multi-global" pattern.
promise_test(define_tests, 'setup - define tests');
268 changes: 268 additions & 0 deletions test/fixtures/wpt/WebCryptoAPI/derive_bits_keys/ecdh_bits.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// META: title=WebCryptoAPI: deriveKey() Using ECDH
// META: script=ecdh_keys.js

// Define subtests from a `promise_test` to ensure the harness does not
// complete before the subtests are available. `explicit_done` cannot be used
// for this purpose because the global `done` function is automatically invoked
// by the WPT infrastructure in dedicated worker tests defined using the
// "multi-global" pattern.
promise_test(define_tests, 'setup - define tests');
237 changes: 237 additions & 0 deletions test/fixtures/wpt/WebCryptoAPI/derive_bits_keys/ecdh_keys.js

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions test/fixtures/wpt/WebCryptoAPI/derive_bits_keys/hkdf.https.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// META: title=WebCryptoAPI: deriveBits() and deriveKey() Using HKDF
// META: variant=?1-1000
// META: variant=?1001-2000
// META: variant=?2001-3000
// META: variant=?3001-last
// META: script=/common/subset-tests.js
// META: script=hkdf_vectors.js
// META: script=hkdf.js

// Define subtests from a `promise_test` to ensure the harness does not
// complete before the subtests are available. `explicit_done` cannot be used
// for this purpose because the global `done` function is automatically invoked
// by the WPT infrastructure in dedicated worker tests defined using the
// "multi-global" pattern.
promise_test(define_tests, 'setup - define tests');
Loading

0 comments on commit 43aad6b

Please sign in to comment.