You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -34,84 +34,102 @@ Efficiently generate cryptographically strong random strings of specified entrop
34
34
35
35
### <aname="TLDR"></a>TL;DR
36
36
37
-
##### Example Usage
37
+
##### Examples
38
+
39
+
Run any of the examples in the `examples` directory by:
40
+
41
+
```bash
42
+
yarn examples
43
+
node examples/dist/tldr_1.js
44
+
```
45
+
46
+
##### Usage
47
+
48
+
Generate a potential of _1 million_ random strings with _1 in a billion_ chance of repeat:
38
49
39
-
OWASP session ID using base 32 characters:
40
50
```js
41
-
import {Random} from'entropy-string'
51
+
import {Random, Entropy} from'entropy-string'
42
52
43
-
let random =newRandom()
44
-
let string =random.sessionID()
53
+
constrandom=newRandom()
54
+
constbits=Entropy.bitsWithPowers(6, 9)
55
+
56
+
conststring=random.string(bits)
45
57
```
46
58
47
-
> QMdD37QNg2QmH3fHDRMhH2HpG4
59
+
> pbbnBD4MQ3rbRN
48
60
49
-
OWASP session ID using [RFC 4648](https://tools.ietf.org/html/rfc4648#section-5) file system and URL safe characters:
61
+
See [Real Need](#RealNeed) for description of what entropy bits represents.
62
+
63
+
`EntropyString` uses predefined `charset32` characters by default (see [Character Sets](#CharacterSets)). To get a random hexadecimal string with the same entropy `bits` as above:
64
+
50
65
```js
51
-
import {Random, charSet64} from'entropy-string'
66
+
import {Random, Entropy} from'entropy-string'
67
+
68
+
constrandom=newRandom(charSet16)
69
+
constbits=Entropy.bitsWithPowers(6, 9)
52
70
53
-
let random =newRandom()
54
-
let string =random.sessionID(charSet64)
71
+
conststring=random.string(bits)
55
72
```
56
73
57
-
> HRU1M7VR5u-N6B0Xo4ZSjx
74
+
> 878114ac513a538e22
58
75
59
-
60
-
48-bit string using hex characters:
76
+
Custom characters may be specified. Using uppercase hexadecimal characters:
61
77
62
78
```js
63
-
import {Random, charSet16} from'entropy-string'
79
+
import {Random, Entropy} from'entropy-string'
64
80
65
-
let random =newRandom(charSet16)
66
-
let string =random.string(48)
81
+
constrandom=newRandom('0123456789ABCDEF')
82
+
constbits=Entropy.bitsWithPowers(6, 9)
83
+
84
+
conststring=random.string(bits)
67
85
```
68
86
69
-
> 7973b7cf643c
87
+
> 16E26779479356B516
88
+
89
+
Convenience functions `smallID`, `mediumID`, `largeID`, `sessionID` and `token` provide random strings of various entropy bits.
70
90
71
-
96-bit string using uppercase hex characters:
91
+
Small ID represents a potential of 30 strings with a 1 in a million chance of repeat:
72
92
73
93
```js
74
94
import {Random} from'entropy-string'
75
95
76
-
let random =newRandom('0123456789ABCDEF')
77
-
let string =random.string(95)
96
+
constrandom=newRandom()
97
+
conststring=random.smallID()
78
98
```
79
99
80
-
> 134BBC6465B0DF101BFBC44B
81
-
82
-
Base 32 character string with a 1 in a million chance of a repeat in 30 strings:
100
+
OWASP session ID using base 32 characters:
83
101
84
102
```js
85
-
import {Random, Entropy} from'entropy-string'
103
+
import {Random} from'entropy-string'
86
104
87
-
let random =newRandom()
88
-
let bits =Entropy.bits(30, 1000000)
89
-
let string =random.string(bits)
105
+
constrandom=newRandom()
106
+
conststring=random.sessionID()
90
107
```
91
108
92
-
> BqMhJM
109
+
> nqqBt2P669nmjPQRqh4NtmTPn9
93
110
94
-
Base 64 character string with a 1 in a trillion chance of a repeat in 100 million strings:
95
-
111
+
OWASP session ID using [RFC 4648](https://tools.ietf.org/html/rfc4648#section-5) file system and URL safe characters:
@@ -189,22 +207,21 @@ Let's use `entropy-string` to help this developer generate 5 IDs:
189
207
To generate the IDs, we first use
190
208
191
209
```js
192
-
let bits =Entropy.bits(10000, 1000000)
210
+
constbits=Entropy.bits(10000, 1000000)
193
211
```
194
212
195
213
to determine how much entropy is needed to satisfy the probabilistic uniqueness of a **1 in a million** risk of repeat in a total of **10,000** strings. We didn't print the result, but if you did you'd see it's about **45.51** bits.
196
214
197
-
The following line creates a `Random` instance configured to generated strings using the default hexadecimal characters provided by `charSet16`:
215
+
The following line creates a `Random` instance configured to generated strings using the predefined hexadecimal characters provided by `charSet16`:
198
216
199
217
```js
200
-
let random =newRandom(charSet16)
201
-
218
+
constrandom=newRandom(charSet16)
202
219
```
203
220
204
221
Then inside a loop we used
205
222
206
223
```js
207
-
let string =random.string(bits)
224
+
conststring=random.string(bits)
208
225
```
209
226
210
227
to actually generate a random string of the specified entropy. Looking at the IDs, we can see each is 12 characters long. Again, the string length is a by-product of the characters used to represent the entropy we needed. And it seems the developer didn't really need 16 characters after all.
@@ -217,33 +234,33 @@ Finally, given that the strings are 12 hexadecimals long, each string actually h
217
234
218
235
In [Real Need](#RealNeed) our developer used hexadecimal characters for the strings. Let's look at using other characters instead.
219
236
220
-
We'll start with using 32 characters. What 32 characters, you ask? The [Character Sets](#CharacterSets) section discusses the default characters available in `entropy-string` and the [Custom Characters](#CustomCharacters) section describes how you can use whatever characters you want. By default, `entropy-string` uses `charSet32` characters, so we don't need to pass that parameter into `new Random()`. We also use `bitsWithRiskPower` that allows passing the `risk` as a power of 10.
237
+
We'll start with using 32 characters. What 32 characters, you ask? The [Character Sets](#CharacterSets) section discusses the predefined characters available in `entropy-string` and the [Custom Characters](#CustomCharacters) section describes how you can use whatever characters you want. By default, `entropy-string` uses `charSet32` characters, so we don't need to pass that parameter into `new Random()`. We also use `bitsWithRiskPower` that allows passing the `risk` as a power of 10.
221
238
222
239
```js
223
240
import {Random, Entropy} from'entropy-string'
224
241
225
-
let random =newRandom()
226
-
let bits =Entropy.bitsWithRiskPower(10000, 6)
227
-
let string =random.string(bits)
242
+
constrandom=newRandom()
243
+
constbits=Entropy.bitsWithRiskPower(10000, 6)
244
+
conststring=random.string(bits)
228
245
```
229
246
230
247
> String: MD8r3BpTH3
231
248
232
249
We're using the same `Entropy.bits` calculation since we haven't changed the number of IDs or the accepted risk of probabilistic uniqueness. But this time we use 32 characters and our resulting ID only requires 10 characters (and can carry 50 bits of entropy).
233
250
234
-
As another example, let's assume we need to ensure the names of a handful of items are unique. Let's say 30 items. And suppose we decide we can live with a 1 in 100,000 probability of collision (we're just futzing with some coding ideas). Using the default provided hex characters:
251
+
As another example, let's assume we need to ensure the names of a handful of items are unique. Let's say 30 items. And suppose we decide we can live with a 1 in 100,000 probability of collision (we're just futzing with some coding ideas). Using the predefined provided hex characters:
The available `CharSet`s are *charSet64*, *charSet32*, *charSet16*, *charSet8*, *charSet4* and *charSet2*. The default characters for each were chosen as follows:
324
+
The available `CharSet`s are *charSet64*, *charSet32*, *charSet16*, *charSet8*, *charSet4* and *charSet2*. The predefined characters for each were chosen as follows:
* The file system and URL safe char set from [RFC 4648](https://tools.ietf.org/html/rfc4648#section-5).
@@ -335,7 +352,7 @@ Being able to easily generate random strings is great, but what if you want to s
335
352
```js
336
353
import {Random, charSet2} from'entropy-string'
337
354
338
-
let random =newRandom(charSet2)
355
+
constrandom=newRandom(charSet2)
339
356
let flips =random.string(10)
340
357
```
341
358
@@ -350,13 +367,13 @@ The resulting string of __0__'s and __1__'s doesn't look quite right. Perhaps yo
350
367
351
368
> flips: THHTHTTHHT
352
369
353
-
As another example, we saw in [Character Sets](#CharacterSets) the default characters for `charSet16` are **0123456789abcdef**. Suppose you like uppercase hexadecimal letters instead.
370
+
As another example, we saw in [Character Sets](#CharacterSets) the predefined characters for `charSet16` are **0123456789abcdef**. Suppose you like uppercase hexadecimal letters instead.
354
371
355
372
```js
356
373
import {Random} from'entropy-string'
357
374
358
-
let random =newRandom('0123456789ABCDEF')
359
-
let string =random.string(48)
375
+
constrandom=newRandom('0123456789ABCDEF')
376
+
conststring=random.string(48)
360
377
361
378
```
362
379
@@ -365,15 +382,15 @@ As another example, we saw in [Character Sets](#CharacterSets) the default chara
365
382
The `Random` constructor allows for three separate cases:
366
383
367
384
- No argument defauls to the `charSet32` characters.
368
-
- One of six default`CharSet`s can be specified.
385
+
- One of six predefined`CharSet`s can be specified.
369
386
- A string representing the characters to use can be specified.
370
387
371
388
The 3rd option above will throw an `EntropyStringError` if the characters string isn't appropriate for creating a `CharSet`.
372
389
```js
373
390
import {Random} from'entropy-string'
374
391
375
392
try {
376
-
let random =newRandom('123456')
393
+
constrandom=newRandom('123456')
377
394
}
378
395
catch(error) {
379
396
console.log('Error: '+error.message)
@@ -384,7 +401,7 @@ The 3rd option above will throw an `EntropyStringError` if the characters string
384
401
385
402
```js
386
403
try {
387
-
let random =newRandom('01233210')
404
+
constrandom=newRandom('01233210')
388
405
}
389
406
catch(error) {
390
407
console.log(error.message)
@@ -404,7 +421,7 @@ To generate the indices, `entropy-string` slices just enough bits from the array
404
421
The `entropy-string` scheme is also efficient with regard to the amount of randomness used. Consider the following common JavaScript solution to generating random strings. To generate a character, an index into the available characters is create using `Math.random`. The code looks something like:
405
422
406
423
```js
407
-
let chars ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
@@ -420,7 +437,7 @@ Compare that to the `entropy-string` scheme. For the example above, slicing off
420
437
```js
421
438
import {Random} from'entropy-string'
422
439
423
-
let random =newRandom()
440
+
constrandom=newRandom()
424
441
let string =random.string(80)
425
442
```
426
443
@@ -431,7 +448,7 @@ But there is an even bigger issue with the previous code from a security perspec
431
448
However, if you don't need cryptographically strong random strings, you can request `entropy-string` use `Math.random` rather than the `crypto` library by using `random.stringRandom`:
432
449
433
450
```js
434
-
letstring =random.stringRandom(80)
451
+
string =random.stringRandom(80)
435
452
```
436
453
437
454
> fdRp9Q3rTMF7TdFN
@@ -451,8 +468,8 @@ Suppose we want a string capable of 30 bits of entropy using 32 characters. We p
451
468
```js
452
469
import {Random} from'entropy-string'
453
470
454
-
let random =newRandom()
455
-
let bytes =Buffer.from([250, 200, 150, 100])
471
+
constrandom=newRandom()
472
+
constbytes=Buffer.from([250, 200, 150, 100])
456
473
let string =random.stringWithBytes(30, bytes)
457
474
```
458
475
@@ -494,9 +511,9 @@ Note the number of bytes needed is dependent on the number of characters in our
0 commit comments