util/jwt: ensure uniform distribution of characters

This commit is contained in:
jj 2024-10-26 18:28:25 +00:00
parent d8b7a6b559
commit a4e6b49d7f
No known key found for this signature in database

View file

@ -4,8 +4,17 @@ const makeSecureString = (length = 64) => {
const alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-'; const alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-';
const out = []; const out = [];
for (const byte of crypto.getRandomValues(new Uint8Array(length))) while (out.length < length) {
out.push(alphabet[byte % alphabet.length]); for (const byte of crypto.getRandomValues(new Uint8Array(length))) {
if (byte < alphabet.length) {
out.push(alphabet[byte]);
}
if (out.length === length) {
break;
}
}
}
return out.join(''); return out.join('');
} }