File tree Expand file tree Collapse file tree 1 file changed +11
-5
lines changed
Expand file tree Collapse file tree 1 file changed +11
-5
lines changed Original file line number Diff line number Diff line change @@ -27,13 +27,19 @@ async function getRandomValues(size: number) {
2727async function random ( size : number ) {
2828 const mask =
2929 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~" ;
30+ const evenDistCutoff = Math . pow ( 2 , 8 ) - Math . pow ( 2 , 8 ) % mask . length ;
31+
3032 let result = "" ;
31- const randomUints = await getRandomValues ( size ) ;
32- for ( let i = 0 ; i < size ; i ++ ) {
33- // cap the value of the randomIndex to mask.length - 1
34- const randomIndex = randomUints [ i ] % mask . length ;
35- result += mask [ randomIndex ] ;
33+ while ( result . length < size ) {
34+ const randomBytes = await getRandomValues ( size - result . length ) ;
35+
36+ for ( const randomByte of randomBytes ) {
37+ if ( randomByte < evenDistCutoff ) {
38+ result += mask [ randomByte % mask . length ] ;
39+ }
40+ }
3641 }
42+
3743 return result ;
3844}
3945
You can’t perform that action at this time.
0 commit comments