refactor: optimize random byte generation for recovery and JWT secret functions

This commit is contained in:
shuaiplus
2026-03-12 01:59:28 +08:00
parent 3eb517a92f
commit 0bb1baf768
5 changed files with 25 additions and 22 deletions
+8 -3
View File
@@ -74,10 +74,15 @@ export default function JwtWarningPage(props: JwtWarningPageProps) {
function generateJwtSecret(length: number): string {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
const bytes = crypto.getRandomValues(new Uint8Array(length));
let out = '';
for (let i = 0; i < length; i += 1) {
out += chars[bytes[i] % chars.length];
const maxUnbiasedByte = Math.floor(256 / chars.length) * chars.length;
while (out.length < length) {
const bytes = crypto.getRandomValues(new Uint8Array(length));
for (const value of bytes) {
if (value >= maxUnbiasedByte) continue;
out += chars[value % chars.length];
if (out.length >= length) break;
}
}
return out;
}