From 616eb9d14be29d0a84ecde1da98c5685e2b0e177 Mon Sep 17 00:00:00 2001 From: dakkar Date: Wed, 27 Nov 2024 09:33:20 +0000 Subject: [PATCH] use a better random integer generator - fixes #810 --- packages/backend/src/misc/secure-rndstr.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/backend/src/misc/secure-rndstr.ts b/packages/backend/src/misc/secure-rndstr.ts index 7853100d8..709e584cc 100644 --- a/packages/backend/src/misc/secure-rndstr.ts +++ b/packages/backend/src/misc/secure-rndstr.ts @@ -14,11 +14,8 @@ export function secureRndstr(length = 32, { chars = LU_CHARS } = {}): string { let str = ''; for (let i = 0; i < length; i++) { - let rand = Math.floor((crypto.randomBytes(1).readUInt8(0) / 0xFF) * chars_len); - if (rand === chars_len) { - rand = chars_len - 1; - } - str += chars.charAt(rand); + const rand = crypto.randomInt(0, chars_len); + str += chars.charAt(rand); } return str;