From 1917f8e8d4f206641138a8af9f197bdbe1bb7e4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Fri, 8 Aug 2025 18:42:30 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E5=AE=89=E5=85=A8=E9=9A=8F=E6=9C=BA=E6=95=B0=E7=94=9F=E6=88=90?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/src/utils/common/common.ts | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/web/src/utils/common/common.ts b/web/src/utils/common/common.ts index 56b27a18..79540a9e 100644 --- a/web/src/utils/common/common.ts +++ b/web/src/utils/common/common.ts @@ -30,10 +30,28 @@ export function toTimestamp(time: any) { /** 生成随机字符串 */ export function generateRandomString(length: number) { const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' + const charactersLength = characters.length let result = '' - for (let i = 0; i < length; i++) { - const randomIndex = Math.floor(Math.random() * characters.length) - result += characters[randomIndex] + if (!window.crypto || !window.crypto.getRandomValues) { + // fallback to insecure Math.random if crypto is not available + for (let i = 0; i < length; i++) { + const randomIndex = Math.floor(Math.random() * charactersLength) + result += characters[randomIndex] + } + return result + } + // Use Uint8Array for random bytes + while (result.length < length) { + const randomBytes = new Uint8Array(length - result.length) + window.crypto.getRandomValues(randomBytes) + for (let i = 0; i < randomBytes.length && result.length < length; i++) { + // Only use values that map evenly to the character set to avoid bias + const maxValue = Math.floor(256 / charactersLength) * charactersLength + if (randomBytes[i] < maxValue) { + const randomIndex = randomBytes[i] % charactersLength + result += characters[randomIndex] + } + } } return result }