Skip to content

Commit e60619e

Browse files
committed
crypto: reuse uuid buffer
1 parent c798973 commit e60619e

1 file changed

Lines changed: 41 additions & 50 deletions

File tree

lib/internal/crypto/random.js

Lines changed: 41 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,6 @@ let uuidData;
348348
let uuidNotBuffered;
349349
let uuidBatch = 0;
350350

351-
let uuidDataV7;
352-
let uuidBatchV7 = 0;
353351
let v7LastTimestamp = -1;
354352
let v7Counter = 0;
355353

@@ -434,64 +432,57 @@ function advanceV7(seed) {
434432
}
435433
}
436434

437-
function writeTimestampAndCounterV7(buf, offset) {
438-
const ts = v7LastTimestamp;
439-
const msb = ts / (2 ** 32);
440-
buf[offset] = msb >>> 8;
441-
buf[offset + 1] = msb;
442-
buf[offset + 2] = ts >>> 24;
443-
buf[offset + 3] = ts >>> 16;
444-
buf[offset + 4] = ts >>> 8;
445-
buf[offset + 5] = ts;
446-
buf[offset + 6] = (v7Counter >>> 8) & 0x0f;
447-
buf[offset + 7] = v7Counter & 0xff;
448-
}
449-
450-
function getBufferedUUIDv7(monotonic) {
451-
uuidDataV7 ??= secureBuffer(16 * kBatchSize);
452-
if (uuidDataV7 === undefined)
453-
throw new ERR_OPERATION_FAILED('Out of memory');
454-
455-
if (uuidBatchV7 === 0) randomFillSync(uuidDataV7);
456-
uuidBatchV7 = (uuidBatchV7 + 1) % kBatchSize;
457-
const offset = uuidBatchV7 * 16;
435+
function makeUUIDv7(buf, offset, monotonic = false) {
436+
let timestamp, randA;
458437
if (monotonic) {
459-
const seed = ((uuidDataV7[offset + 6] & 0x0f) << 8) | uuidDataV7[offset + 7];
438+
const seed = ((buf[offset + 6] & 0x0f) << 8) | buf[offset + 7];
460439
advanceV7(seed);
461-
writeTimestampAndCounterV7(uuidDataV7, offset);
440+
timestamp = v7LastTimestamp;
441+
randA = v7Counter;
462442
} else {
463-
const now = DateNow();
464-
const msb = now / (2 ** 32);
465-
uuidDataV7[offset] = msb >>> 8;
466-
uuidDataV7[offset + 1] = msb;
467-
uuidDataV7[offset + 2] = now >>> 24;
468-
uuidDataV7[offset + 3] = now >>> 16;
469-
uuidDataV7[offset + 4] = now >>> 8;
470-
uuidDataV7[offset + 5] = now;
443+
timestamp = DateNow();
444+
randA = ((buf[offset + 6] & 0x0f) << 8) | buf[offset + 7];
471445
}
472-
return serializeUUID(uuidDataV7, 0x70, 0x80, offset);
446+
const kHexBytes = getHexBytes();
447+
const msb = timestamp / (2 ** 32);
448+
return kHexBytes[msb >>> 8] +
449+
kHexBytes[msb & 0xff] +
450+
kHexBytes[timestamp >>> 24] +
451+
kHexBytes[(timestamp >>> 16) & 0xff] +
452+
'-' +
453+
kHexBytes[(timestamp >>> 8) & 0xff] +
454+
kHexBytes[timestamp & 0xff] +
455+
'-' +
456+
kHexBytes[(randA >>> 8) | 0x70] +
457+
kHexBytes[randA & 0xff] +
458+
'-' +
459+
kHexBytes[(buf[offset + 8] & 0x3f) | 0x80] +
460+
kHexBytes[buf[offset + 9]] +
461+
'-' +
462+
kHexBytes[buf[offset + 10]] +
463+
kHexBytes[buf[offset + 11]] +
464+
kHexBytes[buf[offset + 12]] +
465+
kHexBytes[buf[offset + 13]] +
466+
kHexBytes[buf[offset + 14]] +
467+
kHexBytes[buf[offset + 15]];
468+
}
469+
470+
function getBufferedUUIDv7(monotonic = false) {
471+
uuidData ??= secureBuffer(16 * kBatchSize);
472+
if (uuidData === undefined)
473+
throw new ERR_OPERATION_FAILED('Out of memory');
474+
475+
if (uuidBatch === 0) randomFillSync(uuidData);
476+
uuidBatch = (uuidBatch + 1) % kBatchSize;
477+
return makeUUIDv7(uuidData, uuidBatch * 16, monotonic);
473478
}
474479

475-
function getUnbufferedUUIDv7(monotonic) {
480+
function getUnbufferedUUIDv7(monotonic = false) {
476481
uuidNotBuffered ??= secureBuffer(16);
477482
if (uuidNotBuffered === undefined)
478483
throw new ERR_OPERATION_FAILED('Out of memory');
479484
randomFillSync(uuidNotBuffered, 6);
480-
if (monotonic) {
481-
const seed = ((uuidNotBuffered[6] & 0x0f) << 8) | uuidNotBuffered[7];
482-
advanceV7(seed);
483-
writeTimestampAndCounterV7(uuidNotBuffered, 0);
484-
} else {
485-
const now = DateNow();
486-
const msb = now / (2 ** 32);
487-
uuidNotBuffered[0] = msb >>> 8;
488-
uuidNotBuffered[1] = msb;
489-
uuidNotBuffered[2] = now >>> 24;
490-
uuidNotBuffered[3] = now >>> 16;
491-
uuidNotBuffered[4] = now >>> 8;
492-
uuidNotBuffered[5] = now;
493-
}
494-
return serializeUUID(uuidNotBuffered, 0x70, 0x80);
485+
return makeUUIDv7(uuidNotBuffered, 0, monotonic);
495486
}
496487

497488
function randomUUIDv7(options) {

0 commit comments

Comments
 (0)