Skip to content

Commit 0e4a0c7

Browse files
TeoSlayerteovl
andauthored
Generate message IDs with CSPRNG instead of Date.now+random (#5)
Co-authored-by: TeoSlayer <teodor@vulturelabs.io>
1 parent 9781ec6 commit 0e4a0c7

1 file changed

Lines changed: 4 additions & 6 deletions

File tree

plugin/src/wire.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
// fragmentation. If text exceeds that, the sender splits into N envelopes
66
// sharing the same `id` and assembles via (`seq`, `total`).
77

8+
import { randomUUID } from "node:crypto";
9+
810
export const WIRE_VERSION = 1 as const;
911

1012
/** Maximum bytes for a single envelope's JSON encoding. */
@@ -202,13 +204,9 @@ export async function verifyEnvelope(env: Envelope, secret: string): Promise<boo
202204
return timingSafeEqual(providedBuf, expected);
203205
}
204206

205-
/** Generate a short, monotonically-tending message id (24 base36 chars). */
207+
/** Generate a cryptographically-random, collision-resistant message id. */
206208
export function newId(): string {
207-
const t = Date.now().toString(36).padStart(9, "0");
208-
const r = Math.floor(Math.random() * 36 ** 12)
209-
.toString(36)
210-
.padStart(12, "0");
211-
return `${t}${r}`;
209+
return randomUUID();
212210
}
213211

214212
/**

0 commit comments

Comments
 (0)