File tree Expand file tree Collapse file tree 6 files changed +108
-9
lines changed Expand file tree Collapse file tree 6 files changed +108
-9
lines changed Original file line number Diff line number Diff line change
1
+ import { randomBytes } from "crypto" ;
2
+
1
3
export default class Generator {
4
+
5
+ /**
6
+ * @internal
7
+ * @abstract
8
+ * @param {number } len Length of list.
9
+ * @return {Uint8Array } List of random values.
10
+ */
11
+ getRandom ( len ) { }
12
+
2
13
/**
3
14
* Fetch a list of random char codes for alpha characters.
4
15
*
5
- * @abstract
6
16
* @param {number } len Amount of alpha characters to fetch.
7
- * @return {Array<number> } An array with char-codes (ascii) for the random alpha characters.
17
+ * @return {Uint8Array } An array with char-codes (ascii) for the random alpha characters.
8
18
*/
9
- alpha ( len ) { }
19
+ alpha ( len ) {
20
+ return this . getRandom ( len ) . map ( i => ( Math . round ( i % 25 ) + ( i > 128 ? 65 : 97 ) ) ) ;
21
+ }
10
22
11
23
/**
12
24
* Fetch a list of random char codes of numeric characters.
13
25
*
14
- * @abstract
15
26
* @param {number } len Amount of number characters to fetch.
16
- * @return {Array<number> } An array with char-codes (ascii) for the random numeric characters.
27
+ * @return {Uint8Array } An array with char-codes (ascii) for the random numeric characters.
17
28
*/
18
- numbers ( len ) { }
29
+ numbers ( len ) {
30
+ return this . getRandom ( len ) . map ( i => ( Math . round ( i % 10 ) + 48 ) ) ;
31
+ }
19
32
20
33
21
34
/**
22
35
* Fetch a list of random char codes of special characters.
23
36
*
24
- * @abstract
25
37
* @param {number } len Amount of special characters to fetch.
26
- * @return {Array<number> } An array with char-codes (ascii) for the random special characters.
38
+ * @return {Uint8Array } An array with char-codes (ascii) for the random special characters.
27
39
*/
28
- special ( len ) { }
40
+ special ( len ) {
41
+ return this . getRandom ( len ) . map ( i => {
42
+ return i < 32
43
+ ? Math . round ( i % 15 ) + 33
44
+ : i < 65
45
+ ? Math . round ( i % 7 ) + 58
46
+ : i < 128
47
+ ? Math . round ( i % 4 ) + 91
48
+ : Math . round ( i % 4 ) + 123 ;
49
+ } ) ;
50
+ }
29
51
}
Original file line number Diff line number Diff line change
1
+ import Generator from './Generator' ;
2
+ import { randomBytes } from 'crypto' ;
3
+
4
+ export default class NodeGenerator extends Generator {
5
+
6
+ getRandom ( len ) {
7
+ return randomBytes ( len ) ;
8
+ }
9
+ }
Original file line number Diff line number Diff line change
1
+ import Generator from './Generator' ;
2
+
3
+ export default class WebGenerator extends Generator {
4
+
5
+ getRandom ( len ) {
6
+ const list = new Uint8Array ( len ) ;
7
+ if ( window . crypto ) {
8
+ window . crypto . getRandomValues ( list ) ;
9
+ return list ;
10
+ }
11
+
12
+ // Fallback to random if no crypto on window.
13
+ for ( let i = 0 ; i < len ; i ++ ) {
14
+ list [ i ] = Math . random ( ) ;
15
+ }
16
+
17
+ return list ;
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+ import rand from './rand' ;
2
+
3
+ const generator = ( ) => {
4
+
5
+
6
+ } ;
7
+
8
+ export default rand ( generator ) ;
Original file line number Diff line number Diff line change
1
+ import { shuffle } from './Util'
2
+
3
+ export default ( generator ) => ( length , options = {
4
+ alpha : true ,
5
+ numbers : true ,
6
+ special : true ,
7
+ minAlpha : 1 ,
8
+ minNumbers : 1 ,
9
+ minSpecial : 1 ,
10
+ } ) => {
11
+
12
+ const lists = {
13
+ alpha : ( options . alpha ? generator . alpha ( length ) : [ ] ) ,
14
+ special : ( options . special ? generator . special ( length ) : [ ] ) ,
15
+ numbers : ( options . numbers ? generator . numbers ( length ) : [ ] ) ,
16
+ combined : [ ]
17
+ } ;
18
+
19
+ const result = [ ] ;
20
+ result . push ( ...lists . alpha . splice ( 0 , options . minAlpha ) ) ;
21
+ result . push ( ...lists . special . splice ( 0 , options . minSpecial ) ) ;
22
+ result . push ( ...lists . numbers . splice ( 0 , options . numbers ) ) ;
23
+
24
+ lists . combined = shuffle ( [ ] . concat (
25
+ ...lists . alpha ,
26
+ ...lists . special ,
27
+ ...lists . numbers )
28
+ ) ;
29
+
30
+ // Concat rest.
31
+ result . push ( lists . combined . splice ( 0 , length - (
32
+ ( options . special ? options . minSpecial : 0 )
33
+ +
34
+ ( options . alpha ? options . minAlpha : 0 )
35
+ +
36
+ ( options . numbers ? options . minNumbers : 0 )
37
+ ) ) ) ;
38
+
39
+ return shuffle ( result ) ;
40
+ }
41
+
You can’t perform that action at this time.
0 commit comments