Skip to content

Commit 343f7f0

Browse files
committed
crypto: refactor randomUUIDv7() to use serializeUUID for improved readability
1 parent d5a5fea commit 343f7f0

File tree

1 file changed

+4
-27
lines changed

1 file changed

+4
-27
lines changed

lib/internal/crypto/random.js

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -430,10 +430,8 @@ function randomUUIDv7() {
430430
if (uuidV7Buffer === undefined)
431431
throw new ERR_OPERATION_FAILED('Out of memory');
432432

433-
// Fill all 16 bytes with random data first.
434433
randomFillSync(uuidV7Buffer);
435434

436-
// Write 48-bit timestamp (ms since Unix epoch) into bytes 0-5, big-endian.
437435
const now = DateNow();
438436
uuidV7Buffer[0] = (now / 0x10000000000) & 0xff;
439437
uuidV7Buffer[1] = (now / 0x100000000) & 0xff;
@@ -442,34 +440,13 @@ function randomUUIDv7() {
442440
uuidV7Buffer[4] = (now / 0x100) & 0xff;
443441
uuidV7Buffer[5] = now & 0xff;
444442

445-
// Set version (7) in byte 6 high nibble, keep rand_a in low nibble.
446443
uuidV7Buffer[6] = (uuidV7Buffer[6] & 0x0f) | 0x70;
447444

448-
// Set variant (0b10) in byte 8 high 2 bits, keep rand_b in low 6 bits.
449445
uuidV7Buffer[8] = (uuidV7Buffer[8] & 0x3f) | 0x80;
450-
451-
const kHexBytes = getHexBytes();
452-
return kHexBytes[uuidV7Buffer[0]] +
453-
kHexBytes[uuidV7Buffer[1]] +
454-
kHexBytes[uuidV7Buffer[2]] +
455-
kHexBytes[uuidV7Buffer[3]] +
456-
'-' +
457-
kHexBytes[uuidV7Buffer[4]] +
458-
kHexBytes[uuidV7Buffer[5]] +
459-
'-' +
460-
kHexBytes[uuidV7Buffer[6]] +
461-
kHexBytes[uuidV7Buffer[7]] +
462-
'-' +
463-
kHexBytes[uuidV7Buffer[8]] +
464-
kHexBytes[uuidV7Buffer[9]] +
465-
'-' +
466-
kHexBytes[uuidV7Buffer[10]] +
467-
kHexBytes[uuidV7Buffer[11]] +
468-
kHexBytes[uuidV7Buffer[12]] +
469-
kHexBytes[uuidV7Buffer[13]] +
470-
kHexBytes[uuidV7Buffer[14]] +
471-
kHexBytes[uuidV7Buffer[15]];
472-
}
446+
447+
const result = serializeUUID(uuidV7Buffer);
448+
return result;
449+
}
473450

474451
function createRandomPrimeJob(type, size, options) {
475452
validateObject(options, 'options');

0 commit comments

Comments
 (0)