util/jwt: ensure uniform distribution of characters
This commit is contained in:
parent
d8b7a6b559
commit
a4e6b49d7f
1 changed files with 11 additions and 2 deletions
|
@ -4,8 +4,17 @@ const makeSecureString = (length = 64) => {
|
|||
const alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-';
|
||||
const out = [];
|
||||
|
||||
for (const byte of crypto.getRandomValues(new Uint8Array(length)))
|
||||
out.push(alphabet[byte % alphabet.length]);
|
||||
while (out.length < 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('');
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue