Skip to content

crypto: implement randomUUIDv7()#62553

Merged
nodejs-github-bot merged 7 commits intonodejs:mainfrom
nabeel378:main
Apr 5, 2026
Merged

crypto: implement randomUUIDv7()#62553
nodejs-github-bot merged 7 commits intonodejs:mainfrom
nabeel378:main

Conversation

@nabeel378
Copy link
Copy Markdown
Contributor

@nabeel378 nabeel378 commented Apr 2, 2026

Adds crypto.randomUUIDv7() which generates RFC 9562 version 7 UUIDs.

UUIDv7 encodes a millisecond-precision Unix timestamp in the most
significant 48 bits, making these UUIDs time-sortable and suitable
for use as database primary keys.

Layout (128 bits):

  • 48 bits: unix_ts_ms (UTC milliseconds)
  • 4 bits: version (0b0111)
  • 12 bits: rand_a (random)
  • 2 bits: variant (0b10)
  • 62 bits: rand_b (random)

Fixes: #62529
Refs: https://datatracker.ietf.org/doc/html/rfc9562#name-uuid-version-7

@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/crypto

@nodejs-github-bot nodejs-github-bot added crypto Issues and PRs related to the crypto subsystem. needs-ci PRs that need a full CI run. labels Apr 2, 2026
Copy link
Copy Markdown
Member

@Renegade334 Renegade334 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! A few initial comments.

Comment thread lib/internal/crypto/random.js Outdated
Comment thread lib/internal/crypto/random.js Outdated
@panva
Copy link
Copy Markdown
Member

panva commented Apr 2, 2026

A lot of effort went into optimizing the v4 version. We shall make sure to use the same primitives and setup without actually disrupting the v4 one.

At the moment this proposal doesn't align with the options v4 has.

@nabeel378
Copy link
Copy Markdown
Contributor Author

A lot of effort went into optimizing the v4 version. We shall make sure to use the same primitives and setup without actually disrupting the v4 one.

At the moment this proposal doesn't align with the options v4 has.

I have updated the implementation to fully align with v4: Added disableEntropyCache option matching randomUUID() Same batching strategy (kBatchSize = 128, secureBuffer, randomFillSync)

@nabeel378 nabeel378 requested a review from Renegade334 April 2, 2026 11:39
@panva panva added the commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. label Apr 2, 2026
Comment thread lib/internal/crypto/random.js Outdated
Comment thread lib/internal/crypto/random.js
Comment thread lib/internal/crypto/random.js
Comment thread doc/api/crypto.md Outdated
@panva
Copy link
Copy Markdown
Member

panva commented Apr 2, 2026

Please change the first commit message to crypto: implement randomUUIDv7() and make sure to use -s git flag to add a Signed-off-by trailer.

@panva panva changed the title crypto: add randomUUIDv7() crypto: implement randomUUIDv7() Apr 2, 2026
Signed-off-by: nabeel378 <mohammadnabeeljameel@gmail.com>
- Make version/variant required params in serializeUUID
- Share buffer pools (uuidData/uuidNotBuffered) between v4 and v7

Signed-off-by: nabeel378 <mohammadnabeeljameel@gmail.com>
Signed-off-by: nabeel378 <mohammadnabeeljameel@gmail.com>
@Renegade334 Renegade334 added the semver-minor PRs that contain new features and should be released in the next minor version. label Apr 2, 2026
@nodejs-github-bot

This comment was marked as outdated.

@aduh95
Copy link
Copy Markdown
Contributor

aduh95 commented Apr 3, 2026

test: retrigger CI

@nabeel378 Please don't do that

@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

@nabeel378
Copy link
Copy Markdown
Contributor Author

test: retrigger CI

@nabeel378 Please don't do that

Hi @aduh95, thanks for the heads-up! I added an empty commit to retry CI because sequential.test-watch-mode-inspect and node-test-commit-linux-containered tests failed, but I understand I shouldn’t do that.

@jasnell
Copy link
Copy Markdown
Member

jasnell commented Apr 3, 2026

@nabeel378 ... the github CI is not our main CI. We use jenkins for that and it requires a maintainer to kick it off. Pushing another commit does not trigger the jenkins CI to run again. Also keep in mind that PRs have to stay open for a minimum of 48 hours anyway so trying to rush CI is typically pointless.

@nabeel378
Copy link
Copy Markdown
Contributor Author

@jasnell ... Noted, thanks

Copy link
Copy Markdown
Member

@tniessen tniessen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer if the documentation pointed out that the implementation relies on a non-monotonic clock (because Date.now() merely approximates the current time and itself is not monotonic) and therefore does not guarantee that the timestamps will be in order (even though users should not assume monotonicity anyway).

@nabeel378
Copy link
Copy Markdown
Contributor Author

@tniessen is this wording okay, or would you suggest any changes?" Generates a random [RFC 9562][] version 7 UUID. The UUID contains a millisecond
precision Unix timestamp in the most significant 48 bits, followed by
cryptographically secure random bits for the remaining fields, making it
suitable for use as a database key with time-based sorting. The embedded
timestamp relies on a non-monotonic clock and is not guaranteed to be strictly
increasing."

@tniessen
Copy link
Copy Markdown
Member

tniessen commented Apr 4, 2026

Sure, that works, thank you! (If you don't want to have to re-run CI, I am also okay with updating the docs in a separate PR later.)

@Renegade334
Copy link
Copy Markdown
Member

+1 to not tweaking this PR over it, CI is rolling with disadvantage at present 😉

@panva panva added the commit-queue Add this label to land a pull request using GitHub Actions. label Apr 5, 2026
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Apr 5, 2026
@nodejs-github-bot nodejs-github-bot merged commit dec5973 into nodejs:main Apr 5, 2026
105 checks passed
@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

Landed in dec5973

@panva
Copy link
Copy Markdown
Member

panva commented Apr 5, 2026

@nabeel378 Thank you for your contribution, it is much appreciated. Please follow up with a doc only PR as discussed above

@nabeel378
Copy link
Copy Markdown
Contributor Author

@nabeel378 Thank you for your contribution, it is much appreciated. Please follow up with a doc only PR as discussed above

@panva yes, I am creating PR.

@theoludwig
Copy link
Copy Markdown
Contributor

Can we add an option similar as the npm package uuid: https://www.npmjs.com/package/uuid#uuidv7 (e.g called msecs) to be able to set "RFC "timestamp" field (Number of milliseconds, unix epoch)"?

@nabeel378
Copy link
Copy Markdown
Contributor Author

@theoludwig Thanks for the suggestion!
We prefer keeping the API consistent with crypto.randomUUID() (v4) without exposing internal fields.
Happy to revisit if there’s strong demand.

aduh95 pushed a commit that referenced this pull request May 5, 2026
Signed-off-by: nabeel378 <mohammadnabeeljameel@gmail.com>
PR-URL: #62553
Fixes: #62529
Refs: https://datatracker.ietf.org/doc/html/rfc9562#name-uuid-version-7
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: René <contact.9a5d6388@renegade334.me.uk>
aduh95 added a commit that referenced this pull request May 5, 2026
Notable changes:

buffer:
  * (SEMVER-MINOR) add `end` parameter (Robert Nagy) #62390
crypto:
  * (SEMVER-MINOR) accept key data in `crypto.diffieHellman()` and cleanup DH jobs (Filip Skokan) #62527
  * (SEMVER-MINOR) implement `randomUUIDv7()` (nabeel378) #62553
debugger:
  * (SEMVER-MINOR) add edit-free runtime expression probes to `node inspect` (Joyee Cheung) #62713
fs:
  * (SEMVER-MINOR) add `signal` option to `fs.stat()` (Mert Can Altin) #57775
  * (SEMVER-MINOR) expose frsize field in statfs (Jinho Jang) #62277
http:
  * (SEMVER-MINOR) harden `ClientRequest` options merge (Matteo Collina) #63082
  * (SEMVER-MINOR) add `req.signal` to `IncomingMessage` (Akshat) #62541
lib,src,test,doc:
  * (SEMVER-MINOR) add `node:ffi` module (Colin Ihrig) #62072
process:
  * (SEMVER-MINOR) throw on `execve(2)` failure instead of aborting (Bryan English) #62878
src:
  * (SEMVER-MINOR) allow empty `--experimental-config-file` (Marco Ippolito) #61610
stream:
  * (SEMVER-MINOR) propagate destruction in `duplexPair` (Ahmed Elhor) #61098
test_runner:
  * (SEMVER-MINOR) align mock timeout api (sangwook) #62820
  * (SEMVER-MINOR) add mock-timers support for `AbortSignal.timeout` (DeveloperViraj) #60751
  * (SEMVER-MINOR) support test order randomization (Pietro Marchini) #61747
util:
  * (SEMVER-MINOR) colorize text with hex colors (Guilherme Araújo) #61556

PR-URL: #63137
aduh95 added a commit that referenced this pull request May 6, 2026
Notable changes:

buffer:
  * (SEMVER-MINOR) add `end` parameter (Robert Nagy) #62390
crypto:
  * (SEMVER-MINOR) accept key data in `crypto.diffieHellman()` and cleanup DH jobs (Filip Skokan) #62527
  * (SEMVER-MINOR) implement `randomUUIDv7()` (nabeel378) #62553
debugger:
  * (SEMVER-MINOR) add edit-free runtime expression probes to `node inspect` (Joyee Cheung) #62713
fs:
  * (SEMVER-MINOR) add `signal` option to `fs.stat()` (Mert Can Altin) #57775
  * (SEMVER-MINOR) expose frsize field in statfs (Jinho Jang) #62277
http:
  * (SEMVER-MINOR) harden `ClientRequest` options merge (Matteo Collina) #63082
  * (SEMVER-MINOR) add `req.signal` to `IncomingMessage` (Akshat) #62541
lib,src,test,doc:
  * (SEMVER-MINOR) add `node:ffi` module (Colin Ihrig) #62072
process:
  * (SEMVER-MINOR) throw on `execve(2)` failure instead of aborting (Bryan English) #62878
src:
  * (SEMVER-MINOR) allow empty `--experimental-config-file` (Marco Ippolito) #61610
stream:
  * (SEMVER-MINOR) propagate destruction in `duplexPair` (Ahmed Elhor) #61098
test_runner:
  * (SEMVER-MINOR) align mock timeout api (sangwook) #62820
  * (SEMVER-MINOR) add mock-timers support for `AbortSignal.timeout` (DeveloperViraj) #60751
  * (SEMVER-MINOR) support test order randomization (Pietro Marchini) #61747
util:
  * (SEMVER-MINOR) colorize text with hex colors (Guilherme Araújo) #61556

PR-URL: #63137
aduh95 added a commit that referenced this pull request May 6, 2026
Notable changes:

buffer:
  * (SEMVER-MINOR) add `end` parameter (Robert Nagy) #62390
crypto:
  * (SEMVER-MINOR) accept key data in `crypto.diffieHellman()` and cleanup DH jobs (Filip Skokan) #62527
  * (SEMVER-MINOR) implement `randomUUIDv7()` (nabeel378) #62553
debugger:
  * (SEMVER-MINOR) add edit-free runtime expression probes to `node inspect` (Joyee Cheung) #62713
fs:
  * (SEMVER-MINOR) add `signal` option to `fs.stat()` (Mert Can Altin) #57775
  * (SEMVER-MINOR) expose frsize field in statfs (Jinho Jang) #62277
http:
  * (SEMVER-MINOR) harden `ClientRequest` options merge (Matteo Collina) #63082
  * (SEMVER-MINOR) add `req.signal` to `IncomingMessage` (Akshat) #62541
lib,src,test,doc:
  * (SEMVER-MINOR) add `node:ffi` module (Colin Ihrig) #62072
process:
  * (SEMVER-MINOR) throw on `execve(2)` failure instead of aborting (Bryan English) #62878
src:
  * (SEMVER-MINOR) allow empty `--experimental-config-file` (Marco Ippolito) #61610
stream:
  * (SEMVER-MINOR) propagate destruction in `duplexPair` (Ahmed Elhor) #61098
test_runner:
  * (SEMVER-MINOR) align mock timeout api (sangwook) #62820
  * (SEMVER-MINOR) add mock-timers support for `AbortSignal.timeout` (DeveloperViraj) #60751
  * (SEMVER-MINOR) support test order randomization (Pietro Marchini) #61747
util:
  * (SEMVER-MINOR) colorize text with hex colors (Guilherme Araújo) #61556

PR-URL: #63137
aduh95 added a commit that referenced this pull request May 7, 2026
Notable changes:

buffer:
  * (SEMVER-MINOR) add `end` parameter (Robert Nagy) #62390
crypto:
  * (SEMVER-MINOR) accept key data in `crypto.diffieHellman()` and cleanup DH jobs (Filip Skokan) #62527
  * (SEMVER-MINOR) implement `randomUUIDv7()` (nabeel378) #62553
debugger:
  * (SEMVER-MINOR) add edit-free runtime expression probes to `node inspect` (Joyee Cheung) #62713
fs:
  * (SEMVER-MINOR) add `signal` option to `fs.stat()` (Mert Can Altin) #57775
  * (SEMVER-MINOR) expose frsize field in statfs (Jinho Jang) #62277
http:
  * (SEMVER-MINOR) harden `ClientRequest` options merge (Matteo Collina) #63082
  * (SEMVER-MINOR) add `req.signal` to `IncomingMessage` (Akshat) #62541
lib,src,test,doc:
  * (SEMVER-MINOR) add `node:ffi` module (Colin Ihrig) #62072
process:
  * (SEMVER-MINOR) throw on `execve(2)` failure instead of aborting (Bryan English) #62878
src:
  * (SEMVER-MINOR) allow empty `--experimental-config-file` (Marco Ippolito) #61610
stream:
  * (SEMVER-MINOR) propagate destruction in `duplexPair` (Ahmed Elhor) #61098
test_runner:
  * (SEMVER-MINOR) align mock timeout api (sangwook) #62820
  * (SEMVER-MINOR) add mock-timers support for `AbortSignal.timeout` (DeveloperViraj) #60751
  * (SEMVER-MINOR) support test order randomization (Pietro Marchini) #61747
util:
  * (SEMVER-MINOR) colorize text with hex colors (Guilherme Araújo) #61556

PR-URL: #63137
aduh95 pushed a commit that referenced this pull request May 7, 2026
Signed-off-by: nabeel378 <mohammadnabeeljameel@gmail.com>
PR-URL: #62553
Fixes: #62529
Refs: https://datatracker.ietf.org/doc/html/rfc9562#name-uuid-version-7
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: René <contact.9a5d6388@renegade334.me.uk>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs that have at least one approval, no pending requests for changes, and a CI started. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. crypto Issues and PRs related to the crypto subsystem. needs-ci PRs that need a full CI run. semver-minor PRs that contain new features and should be released in the next minor version.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for generating UUIDv7s

9 participants