Commit 12bce7d 1 parent a177a01 commit 12bce7d Copy full SHA for 12bce7d
File tree 1 file changed +19
-3
lines changed
1 file changed +19
-3
lines changed Original file line number Diff line number Diff line change 4
4
* @returns {string } required secret
5
5
*/
6
6
export const generateRandomString = ( length : number = 28 ) : string => {
7
- const arr = new Uint8Array ( length / 2 ) ;
8
- crypto . getRandomValues ( arr ) ;
9
- return Array . from ( arr , dec2hex ) . join ( "" ) ;
7
+ if ( crypto ) {
8
+ const arr = new Uint8Array ( length / 2 ) ;
9
+ crypto . getRandomValues ( arr ) ;
10
+ return Array . from ( arr , dec2hex ) . join ( "" ) ;
11
+ } else {
12
+ return generateRandomStringNonCrypto ( length ) ;
13
+ }
10
14
} ;
11
15
12
16
function dec2hex ( dec : number ) {
13
17
return dec . toString ( 16 ) . padStart ( 2 , "0" ) ;
14
18
}
19
+
20
+ function generateRandomStringNonCrypto ( length : number = 28 ) {
21
+ const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' ;
22
+ let result = '' ;
23
+ const charactersLength = characters . length ;
24
+
25
+ for ( let i = 0 ; i < length ; i ++ ) {
26
+ result += characters . charAt ( Math . floor ( Math . random ( ) * charactersLength ) ) ;
27
+ }
28
+
29
+ return result ;
30
+ }
You can’t perform that action at this time.
0 commit comments