From 63126a7dbbfaf13d4e9d35a563496071d84adac5 Mon Sep 17 00:00:00 2001 From: David Luis <95457148+babiabeo@users.noreply.github.com> Date: Sun, 11 Aug 2024 12:05:19 +0700 Subject: [PATCH] refactor(generate): make generator more unguessable (#5) --- .github/workflows/title.yml | 3 ++- src/generate.ts | 18 +++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/.github/workflows/title.yml b/.github/workflows/title.yml index bcdf76e..a6c4b9d 100644 --- a/.github/workflows/title.yml +++ b/.github/workflows/title.yml @@ -30,4 +30,5 @@ jobs: test ci chore - revert \ No newline at end of file + revert + release \ No newline at end of file diff --git a/src/generate.ts b/src/generate.ts index 911df0e..b3f52a5 100644 --- a/src/generate.ts +++ b/src/generate.ts @@ -32,7 +32,7 @@ import { stringify } from "./_stringify.ts"; export function generate(timestamp?: number): string { const uuid = new Uint8Array(16); const tm = timestamp ?? Date.now(); - const rand = crypto.getRandomValues(new Uint8Array(9)); + const rand = crypto.getRandomValues(new Uint8Array(10)); const seq = getSeq(tm, rand); // [octets 0-5]: timestamp (48 bits) @@ -53,13 +53,13 @@ export function generate(timestamp?: number): string { uuid[8] = 0x80 | (seq & 0x3f); // [octets 9-15]: random (56 bits) - uuid[9] = rand[2]; - uuid[10] = rand[3]; - uuid[11] = rand[4]; - uuid[12] = rand[5]; - uuid[13] = rand[6]; - uuid[14] = rand[7]; - uuid[15] = rand[8]; + uuid[9] = rand[0]; + uuid[10] = rand[1]; + uuid[11] = rand[2]; + uuid[12] = rand[3]; + uuid[13] = rand[4]; + uuid[14] = rand[5]; + uuid[15] = rand[6]; return stringify(uuid); } @@ -71,7 +71,7 @@ let _seq = 0; function getSeq(now: number, rand: Uint8Array): number { if (now > _lastTime) { - _seq = ((now & 0x03) << 16) | (rand[0] << 8) | rand[1]; + _seq = ((rand[7] & 0x03) << 16) | (rand[8] << 8) | rand[9]; _lastTime = now; return _seq;