Skip to content

Commit f702919

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
1 parent 0df5e6b commit f702919

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

lib/internal/crypto/random.js

Lines changed: 15 additions & 19 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) {

0 commit comments

Comments
 (0)