@@ -3,14 +3,33 @@ import CryptoJS from "crypto-js";
33
44
55
6-
6+ const shuffleCharacterAt = ( content , rNumber ) => {
7+ if ( rNumber < 1 ) {
8+ return content . slice ( 1 ) + content . charAt ( 0 ) ;
9+ }
10+ const rIndex = rNumber % content . length ;
11+ if ( ( rIndex + 1 ) >= content . length ) {
12+ return content . charAt ( content . length - 1 ) + content . slice ( 0 , content . length - 1 ) ;
13+ }
14+ return content . slice ( 0 , rIndex ) + content . slice ( rIndex + 1 ) + content . charAt ( rIndex ) ;
15+ }
16+ const randomNumberGenerator = ( ) => {
17+ const indexString = CryptoJS . enc . Hex . stringify ( CryptoJS . lib . WordArray . random ( 1 ) ) ;
18+ return parseInt ( indexString , 16 ) ;
19+ }
20+ let possibleCharactersSeed = "Abm6fixYq;rMh9sSkjaGvl@*$tOVDZRyQF:8WzonIT41K0wL3PHp7XCEecB&Jgu£2dUN5" ;
21+ const startUpTime = new Date ( ) . getMilliseconds ( ) ;
722export const generateRandomString = ( length = 10 ) => {
8- const randPassword = Array ( length ) . fill ( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@£$&*:;" ) . map ( function ( x ) {
9- const indexString = CryptoJS . enc . Hex . stringify ( CryptoJS . lib . WordArray . random ( 1 ) ) ;
10- const indexValue = parseInt ( indexString , 16 ) ;
11- return x [ indexValue % x . length ]
12- } ) . join ( '' ) ;
13- return randPassword ;
23+ let result = '' ;
24+ for ( let loop = 0 ; loop < length ; loop ++ ) {
25+ possibleCharactersSeed = shuffleCharacterAt ( possibleCharactersSeed , Math . random ( ) * possibleCharactersSeed . length ) ; //reshuffle with browser random
26+ possibleCharactersSeed = shuffleCharacterAt ( possibleCharactersSeed , new Date ( ) . getMilliseconds ( ) ) ; //reshuffle with time
27+ possibleCharactersSeed = shuffleCharacterAt ( possibleCharactersSeed , startUpTime ) ; //reshuffle with application start time
28+ const indexValue = randomNumberGenerator ( ) ; //generate random using the crypto
29+ result += possibleCharactersSeed . charAt ( indexValue % possibleCharactersSeed . length ) ; //get the character from the seed using the crypto random
30+ possibleCharactersSeed = shuffleCharacterAt ( possibleCharactersSeed , indexValue + new Date ( ) . getMilliseconds ( ) ) ; //reshuffle using the crypto random and time
31+ }
32+ return result ;
1433} ;
1534export const encrypt = ( content , password ) => escape ( CryptoJS . AES . encrypt ( content , password ) . toString ( ) ) ;
1635
0 commit comments