mirror of
https://github.com/shuaiplus/nodewarden.git
synced 2026-06-20 21:00:41 +00:00
refactor: optimize random byte generation for recovery and JWT secret functions
This commit is contained in:
@@ -16,9 +16,16 @@ interface SettingsPageProps {
|
||||
|
||||
function randomBase32Secret(length: number): string {
|
||||
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
|
||||
const random = crypto.getRandomValues(new Uint8Array(length));
|
||||
let out = '';
|
||||
for (const x of random) out += alphabet[x % alphabet.length];
|
||||
const maxUnbiasedByte = Math.floor(256 / alphabet.length) * alphabet.length;
|
||||
while (out.length < length) {
|
||||
const random = crypto.getRandomValues(new Uint8Array(length));
|
||||
for (const x of random) {
|
||||
if (x >= maxUnbiasedByte) continue;
|
||||
out += alphabet[x % alphabet.length];
|
||||
if (out.length >= length) break;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user