mirror of
https://github.com/wukko/cobalt.git
synced 2025-06-13 05:37:44 +02:00
util/jwt: ensure uniform distribution of characters
This commit is contained in:
@ -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('');
|
||||
}
|
||||
|
Reference in New Issue
Block a user