Skip to content

Commit 8ddc406

Browse files
committed
crypto: address review feedback for randomUUIDv7
- Make version/variant required params in serializeUUID - Share buffer pools (uuidData/uuidNotBuffered) between v4 and v7 Signed-off-by: nabeel378 <mohammadnabeeljameel@gmail.com>
1 parent e3a4a06 commit 8ddc406

2 files changed

Lines changed: 16 additions & 34 deletions

File tree

doc/api/crypto.md

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5845,20 +5845,6 @@ precision Unix timestamp in the most significant 48 bits, followed by
58455845
cryptographically secure random bits for the remaining fields, making it
58465846
suitable for use as a database key with time-based sorting.
58475847

5848-
```mjs
5849-
import { randomUUIDv7 } from 'node:crypto';
5850-
5851-
console.log(randomUUIDv7());
5852-
// e.g. '019d45ea-151f-780b-82a6-d097b39db62a'
5853-
```
5854-
5855-
```cjs
5856-
const { randomUUIDv7 } = require('node:crypto');
5857-
5858-
console.log(randomUUIDv7());
5859-
// e.g. '019d45ea-151f-780b-82a6-d097b39db62a'
5860-
```
5861-
58625848
### `crypto.scrypt(password, salt, keylen[, options], callback)`
58635849

58645850
<!-- YAML

lib/internal/crypto/random.js

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ function getHexBytes() {
360360
return hexBytesCache;
361361
}
362362

363-
function serializeUUID(buf, offset = 0, version = 0x40, variant = 0x80) {
363+
function serializeUUID(buf, version, variant, offset = 0) {
364364
const kHexBytes = getHexBytes();
365365
// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
366366
return kHexBytes[buf[offset]] +
@@ -392,15 +392,15 @@ function getBufferedUUID() {
392392

393393
if (uuidBatch === 0) randomFillSync(uuidData);
394394
uuidBatch = (uuidBatch + 1) % kBatchSize;
395-
return serializeUUID(uuidData, uuidBatch * 16);
395+
return serializeUUID(uuidData, 0x40, 0x80, uuidBatch * 16);
396396
}
397397

398398
function getUnbufferedUUID() {
399399
uuidNotBuffered ??= secureBuffer(16);
400400
if (uuidNotBuffered === undefined)
401401
throw new ERR_OPERATION_FAILED('Out of memory');
402402
randomFillSync(uuidNotBuffered);
403-
return serializeUUID(uuidNotBuffered);
403+
return serializeUUID(uuidNotBuffered, 0x40, 0x80);
404404
}
405405

406406
function randomUUID(options) {
@@ -415,10 +415,6 @@ function randomUUID(options) {
415415
return disableEntropyCache ? getUnbufferedUUID() : getBufferedUUID();
416416
}
417417

418-
let uuidV7Data;
419-
let uuidV7NotBuffered;
420-
let uuidV7Batch = 0;
421-
422418
function writeTimestamp(buf, offset) {
423419
const now = DateNow();
424420
const msb = now / (2 ** 32);
@@ -431,24 +427,24 @@ function writeTimestamp(buf, offset) {
431427
}
432428

433429
function getBufferedUUIDv7() {
434-
uuidV7Data ??= secureBuffer(16 * kBatchSize);
435-
if (uuidV7Data === undefined)
430+
uuidData ??= secureBuffer(16 * kBatchSize);
431+
if (uuidData === undefined)
436432
throw new ERR_OPERATION_FAILED('Out of memory');
437433

438-
if (uuidV7Batch === 0) randomFillSync(uuidV7Data);
439-
uuidV7Batch = (uuidV7Batch + 1) % kBatchSize;
440-
const offset = uuidV7Batch * 16;
441-
writeTimestamp(uuidV7Data, offset);
442-
return serializeUUID(uuidV7Data, offset, 0x70);
434+
if (uuidBatch === 0) randomFillSync(uuidData);
435+
uuidBatch = (uuidBatch + 1) % kBatchSize;
436+
const offset = uuidBatch * 16;
437+
writeTimestamp(uuidData, offset);
438+
return serializeUUID(uuidData, 0x70, 0x80, offset);
443439
}
444440

445441
function getUnbufferedUUIDv7() {
446-
uuidV7NotBuffered ??= secureBuffer(16);
447-
if (uuidV7NotBuffered === undefined)
442+
uuidNotBuffered ??= secureBuffer(16);
443+
if (uuidNotBuffered === undefined)
448444
throw new ERR_OPERATION_FAILED('Out of memory');
449-
randomFillSync(uuidV7NotBuffered, 6);
450-
writeTimestamp(uuidV7NotBuffered, 0);
451-
return serializeUUID(uuidV7NotBuffered, 0, 0x70);
445+
randomFillSync(uuidNotBuffered, 6);
446+
writeTimestamp(uuidNotBuffered, 0);
447+
return serializeUUID(uuidNotBuffered, 0x70, 0x80);
452448
}
453449

454450
function randomUUIDv7(options) {
@@ -461,7 +457,7 @@ function randomUUIDv7(options) {
461457
validateBoolean(disableEntropyCache, 'options.disableEntropyCache');
462458

463459
return disableEntropyCache ? getUnbufferedUUIDv7() : getBufferedUUIDv7();
464-
}
460+
}
465461

466462
function createRandomPrimeJob(type, size, options) {
467463
validateObject(options, 'options');

0 commit comments

Comments
 (0)