1 | # node-rand-token
|
2 |
|
3 | Generate random tokens from your choice of randomness.
|
4 |
|
5 | # Installation
|
6 |
|
7 | Add it to your node.js project via:
|
8 |
|
9 | npm install rand-token --save
|
10 |
|
11 | # Usage
|
12 |
|
13 | // Create a token generator with the default settings:
|
14 | var randtoken = require('rand-token');
|
15 |
|
16 | // Generate a 16 character alpha-numeric token:
|
17 | var token = randtoken.generate(16);
|
18 |
|
19 | # Defaults
|
20 |
|
21 | The default set of allowed characters is all alpha-numeric characters. Specifically, lower case a through z, upper case A through Z, and the number 0 through 9. This gives you `(26 + 26 + 10)` = `62` possibilities per character.
|
22 |
|
23 | Using 8 character random tokens will give you a possible token space of 62^8 = 2.18 x 10^21 ~= 2^47
|
24 |
|
25 | Using 12 character random tokens will give you a possible token space of 62^12 = 3.22 x 10^21 ~= 2^71
|
26 |
|
27 | Using 16 character random tokens will give you a possible token space of 62^16 = 4.76 x 10^28 ~= 2^95
|
28 |
|
29 | # Functions
|
30 |
|
31 | ## generate(size, [chars])
|
32 |
|
33 | Generates a token of `size` characters using either the specified `chars` or the default for the generator.
|
34 |
|
35 | If `chars` is specified it will be treated as an array of characters to use. Each character in the list has an equal chance of being used so if a character is repeated twice, it will appear twice as often in the randomly generated tokens.
|
36 |
|
37 | __Note__: Unlike the `generator(...)` function this function does not treat any string values of `chars` to be special and all values will be simply treated as an array of possible characters.
|
38 |
|
39 | ## generator([options])
|
40 |
|
41 | Creates a custom token generator.
|
42 |
|
43 | Available options:
|
44 |
|
45 | * `source` - source of random bytes
|
46 | This should be either a string or a function with a signature that matches `crypto.randomBytes(size, [callback])` (returning a buffer).
|
47 |
|
48 | The following string values are also accepted:
|
49 |
|
50 | * `default` - This is a synonym for using the default of `crypto.pseudoRandomBytes`. You do not need to specify `default` and can simply create the generator without this option specified to get the same effect.
|
51 |
|
52 | * `crypto` - This is a synonym for [`crypto.randomBytes`](http://nodejs.org/api/crypto.html#crypto_crypto_randombytes_size_callback). Note that this may throw an error during use if there is not enough entropy.
|
53 |
|
54 | * `math` - This is a synonym for using the Math.random() function.
|
55 |
|
56 | * `chars` - string representing the list of allowed characters to use for `generate(size)` calls.
|
57 |
|
58 | The following string values have special meaning:
|
59 |
|
60 | * `default` - default set of token characters which is numbers, lower case letters, and upper case letters (i.e. `0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`)
|
61 | * `a-z` || `alpha` - lower case characters (i.e. `abcdefghijklmnopqrstuvwxyz`)
|
62 | * `A-Z` || `ALPHA` - upper case characters (i.e. `ABCDEFGHIJKLMNOPQRSTUVWXYZ`)
|
63 | * `0-9` || `numeric` - numbers only (i.e. `0123456789`)
|
64 | * `base32` - use characters from the base32 alphabet, specifically `A-Z` and `2-7`
|
65 |
|
66 | Any other string value will be treated as an array of characters to use.
|
67 |
|
68 | Each character in the list has an equal chance of being used. For example if a character is repeated twice, it will appear twice as often in randomly generated tokens. `chars` may be at most 255 characters long.
|
69 |
|
70 | # Examples
|
71 |
|
72 | To generate alpha-numeric (lower case a-z, upper case A-Z, and digits 0-9):
|
73 |
|
74 | // Create a token generator with the default settings:
|
75 | var randtoken = require('rand-token');
|
76 |
|
77 | // Generate a 16 character alpha-numeric token:
|
78 | var token = randtoken.generate(16);
|
79 |
|
80 | To generate only lower case letters (a-z):
|
81 |
|
82 | // Create a token generator with the default settings:
|
83 | var randtoken = require('rand-token').generator({
|
84 | chars: 'a-z'
|
85 | });
|
86 |
|
87 | // Generate a 16 character token:
|
88 | var token = randtoken.generate(16);
|
89 |
|
90 | Alternatively, you can create a generator with the default options and pass the characters to use as the second parameter to `generate`:
|
91 |
|
92 | // Create a token generator with the default settings:
|
93 | var randtoken = require('rand-token').generator();
|
94 |
|
95 | // Generate a 16 character token:
|
96 | var token = randtoken.generate(16, "abcdefghijklnmopqrstuvwxyz");
|
97 |
|
98 | To generate only upper case letters with `crypto.randomBytes` as the random source:
|
99 |
|
100 | var crypto = require('crypto');
|
101 | // Create the generator:
|
102 | var randtoken = require('rand-token').generator({
|
103 | chars: 'A-Z',
|
104 | source: crypto.randomBytes
|
105 | });
|
106 |
|
107 | // Generate a 16 character token:
|
108 | var token = randtoken.generate(16);
|
109 |
|
110 |
|
111 | # Dependencies
|
112 |
|
113 | * [lodash](lodash.com)
|
114 |
|
115 | # License
|
116 |
|
117 | This plugin is released under the MIT license. See the file [LICENSE](LICENSE).
|