@@ -80,59 +80,48 @@ class PromiseUtil {
80
80
}
81
81
}
82
82
83
- const seq = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' . split ( '' ) ;
84
-
85
83
class RandomUtil {
86
- static randomIntRange ( min , max ) {
87
- return Math . floor ( Math . random ( ) * ( max - min ) + min ) ;
84
+ static getSeq ( { hasNumbers = true , hasLowercase = true , hasUppercase = true } = { } ) {
85
+ let seq = '' ;
86
+ if ( hasNumbers ) seq += "0123456789" ;
87
+ if ( hasLowercase ) seq += "abcdefghijklmnopqrstuvwxyz" ;
88
+ if ( hasUppercase ) seq += "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
89
+ return seq ;
88
90
}
89
91
90
- static randomInt ( n ) {
91
- return this . randomIntRange ( 0 , n ) ;
92
+ static randomInteger ( min , max ) {
93
+ const range = max - min + 1 ;
94
+ const randomBuffer = new Uint32Array ( 1 ) ;
95
+ window . crypto . getRandomValues ( randomBuffer ) ;
96
+ return Math . floor ( ( randomBuffer [ 0 ] / ( 0xFFFFFFFF + 1 ) ) * range ) + min ;
92
97
}
93
98
94
- static randomSeq ( count ) {
95
- let str = '' ;
96
- for ( let i = 0 ; i < count ; ++ i ) {
97
- str += seq [ this . randomInt ( 62 ) ] ;
98
- }
99
- return str ;
99
+ static randomSeq ( count , options = { } ) {
100
+ const seq = this . getSeq ( options ) ;
101
+ const seqLength = seq . length ;
102
+ const randomValues = new Uint32Array ( count ) ;
103
+ window . crypto . getRandomValues ( randomValues ) ;
104
+ return Array . from ( randomValues , v => seq [ v % seqLength ] ) . join ( '' ) ;
100
105
}
101
106
102
107
static randomShortIds ( ) {
103
- const lengths = [ 2 , 4 , 6 , 8 , 10 , 12 , 14 , 16 ] ;
104
- for ( let i = lengths . length - 1 ; i > 0 ; i -- ) {
105
- const j = Math . floor ( Math . random ( ) * ( i + 1 ) ) ;
106
- [ lengths [ i ] , lengths [ j ] ] = [ lengths [ j ] , lengths [ i ] ] ;
107
- }
108
-
109
- let shortIds = [ ] ;
110
- for ( let length of lengths ) {
111
- let shortId = '' ;
112
- for ( let i = 0 ; i < length ; i ++ ) {
113
- shortId += seq [ this . randomInt ( 16 ) ] ;
114
- }
115
- shortIds . push ( shortId ) ;
116
- }
117
- return shortIds . join ( ',' ) ;
108
+ const lengths = [ 2 , 4 , 6 , 8 , 10 , 12 , 14 , 16 ] . sort ( ( ) => Math . random ( ) - 0.5 ) ;
109
+ const seq = this . getSeq ( ) ;
110
+ return lengths . map ( len => this . randomSeq ( len ) ) . join ( ',' ) ;
118
111
}
119
112
120
113
static randomLowerAndNum ( len ) {
121
- let str = '' ;
122
- for ( let i = 0 ; i < len ; ++ i ) {
123
- str += seq [ this . randomInt ( 36 ) ] ;
124
- }
125
- return str ;
114
+ return this . randomSeq ( len , { hasUppercase : false } ) ;
126
115
}
127
116
128
117
static randomUUID ( ) {
129
- return window . crypto . randomUUID ( )
118
+ return window . crypto . randomUUID ( ) ;
130
119
}
131
120
132
121
static randomShadowsocksPassword ( ) {
133
- let array = new Uint8Array ( 32 ) ;
122
+ const array = new Uint8Array ( 32 ) ;
134
123
window . crypto . getRandomValues ( array ) ;
135
- return Base64 . encode ( String . fromCharCode . apply ( null , array ) ) ;
124
+ return Base64 . encode ( String . fromCharCode ( ... array ) ) ;
136
125
}
137
126
}
138
127
0 commit comments