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
Copy file name to clipboardExpand all lines: README.md
+70-74
Original file line number
Diff line number
Diff line change
@@ -49,12 +49,11 @@ Run any of the examples in the `examples` directory by:
49
49
Generate a potential of _1 million_ random strings with _1 in a billion_ chance of repeat:
50
50
51
51
```js
52
-
const {Random,Entropy} =require('entropy-string')
52
+
const { Entropy} =require('entropy-string')
53
53
54
-
constrandom=newRandom()
55
54
constbits=Entropy.bits(1e6, 1e9)
56
-
57
-
conststring=random.string(bits)
55
+
constentropy=newEntropy()
56
+
conststring=entropy.string(bits)
58
57
```
59
58
60
59
> pbbnBD4MQ3rbRN
@@ -64,58 +63,56 @@ See [Real Need](#RealNeed) for description of what entropy bits represents.
64
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:
Custom characters may be specified. Using uppercase hexadecimal characters:
78
76
79
77
```js
80
-
const {Random,Entropy} =require('entropy-string')
78
+
const { Entropy} =require('entropy-string')
81
79
82
-
constrandom=newRandom('0123456789ABCDEF')
83
80
constbits=Entropy.bits(1e6, 1e9)
84
-
85
-
conststring=random.string(bits)
81
+
constentropy=newEntropy('0123456789ABCDEF')
82
+
conststring=entropy.string(bits)
86
83
```
87
84
88
85
> 16E26779479356B516
89
86
90
87
Convenience functions `smallID`, `mediumID`, `largeID`, `sessionID` and `token` provide random strings for various predefined bits of entropy. For example, a small id represents a potential of 30 strings with a 1 in a million chance of repeat:
91
88
92
89
```js
93
-
const {Random} =require('entropy-string')
90
+
const {Entropy} =require('entropy-string')
94
91
95
-
constrandom=newRandom()
96
-
conststring=random.smallID()
92
+
constentropy=newEntropy()
93
+
conststring=entropy.smallID()
97
94
```
98
95
99
96
> DpTQqg
100
97
101
98
Or, to generate an OWASP session ID:
102
99
103
100
```js
104
-
const {Random} =require('entropy-string')
101
+
const {Entropy} =require('entropy-string')
105
102
106
-
constrandom=newRandom()
107
-
conststring=random.sessionID()
103
+
constentropy=newEntropy()
104
+
conststring=entropy.sessionID()
108
105
```
109
106
110
107
> nqqBt2P669nmjPQRqh4NtmTPn9
111
108
112
109
Or perhaps you need an 256-bit token using [RFC 4648](https://tools.ietf.org/html/rfc4648#section-5) file system and URL safe characters:
@@ -181,14 +178,13 @@ How do you address this need using a library designed to generate strings of spe
181
178
Let's use `entropy-string` to help this developer generate 5 hexadecimal IDs from a pool of a potentail 10,000 IDs with a 1 in a milllion chance of a repeat:
is used 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** potential strings. We didn't print the result, but if you did you'd see it's about **45.51** bits. Then
205
201
206
202
```js
207
-
constrandom=newRandom(charSet16)
203
+
constentropy=newEntropy(charSet16)
208
204
```
209
205
210
-
creates a `Random` instance configured to generated strings using the predefined hexadecimal characters provided by `charSet16`. Finally
206
+
creates a `Entropy` instance configured to generated strings using the predefined hexadecimal characters provided by `charSet16`. Finally
211
207
212
208
```js
213
-
conststring=random.string(bits)
209
+
conststring=entropy.string(bits)
214
210
```
215
211
216
212
is used to actually generate a random string of the specified entropy.
@@ -225,14 +221,14 @@ Given that the strings are 12 hexadecimals long, each string actually has an inf
225
221
226
222
In [Real Need](#RealNeed) our developer used hexadecimal characters for the strings. Let's look at using other characters instead.
227
223
228
-
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()`.
224
+
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 Entropy()`.
229
225
230
226
```js
231
-
const {Random,Entropy} =require('entropy-string')
227
+
const { Entropy} =require('entropy-string')
232
228
233
-
constrandom=newRandom()
234
229
constbits=Entropy.bits(10000, 1e6)
235
-
conststring=random.string(bits)
230
+
constentropy=newEntropy()
231
+
conststring=entropy.string(bits)
236
232
```
237
233
238
234
> String: MD8r3BpTH3
@@ -242,20 +238,20 @@ We're using the same `Entropy.bits` calculation since we haven't changed the num
242
238
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:
Using the same `Random` instance, we can switch to the predefined `charSet4` characters and generate a string of the same amount of entropy:
250
+
Using the same `Entropy` instance, we can switch to the predefined `charSet4` characters and generate a string of the same amount of entropy:
255
251
256
252
```js
257
-
random.use(charSet4)
258
-
string =random.string(bits)
253
+
entropy.use(charSet4)
254
+
string =entropy.string(bits)
259
255
```
260
256
261
257
> String: CAATAGTGGACTG
@@ -265,33 +261,33 @@ Okay, we probably wouldn't use 4 characters (and what's up with those characters
265
261
Suppose we have a more extreme need. We want less than a 1 in a trillion chance that 10 billion base 32 strings repeat. Let's see, our total (10 billion) is 10<sup>10</sup> and our risk (1 trillion) is 10<sup>12</sup>, so:
266
262
267
263
```js
268
-
const {Random,Entropy} =require('entropy-string')
264
+
const { Entropy} =require('entropy-string')
269
265
270
-
constrandom=newRandom()
271
266
constbits=Entropy.bits(1e10, 1e12)
272
-
conststring=random.string(bits)
267
+
constentropy=newEntropy()
268
+
conststring=entropy.string(bits)
273
269
```
274
270
275
271
> String: 4J86pbFG9BqdBjTLfD3rt6
276
272
277
273
Finally, let say we're generating session IDs. Since session IDs are ephemeral, we aren't interested in uniqueness per se, but in ensuring our IDs aren't predictable since we can't have the bad guys guessing a valid session ID. In this case, we're using entropy as a measure of unpredictability of the IDs. Rather than calculate our entropy, we declare it as 128 bits (since we read on the OWASP web site that session IDs should be 128 bits).
278
274
279
275
```js
280
-
const {Random} =require('entropy-string')
276
+
const {Entropy} =require('entropy-string')
281
277
282
-
constrandom=newRandom()
283
-
conststring=random.string(128)
278
+
constentropy=newEntropy()
279
+
conststring=entropy.string(128)
284
280
```
285
281
286
282
> String: Rm9gDFn6Q9DJ9rbrtrttBjR97r
287
283
288
284
Since session ID are such an important need, `entropy-string` provides a convenience function for generating them:
@@ -345,47 +341,47 @@ You may, of course, want to choose the characters used, which is covered next in
345
341
Being able to easily generate random strings is great, but what if you want to specify your own characters. For example, suppose you want to visualize flipping a coin to produce entropy of 10 bits.
The resulting string of __0__'s and __1__'s doesn't look quite right. Perhaps you want to use the characters __H__ and __T__ instead.
357
353
358
354
```js
359
-
random.useChars('HT')
360
-
flips =random.string(10)
355
+
entropy.useChars('HT')
356
+
flips =entropy.string(10)
361
357
```
362
358
363
359
> flips: THHTHTTHHT
364
360
365
361
As another example, we saw in [Character Sets](#CharacterSets) the predefined hex characters for `charSet16` are lowercase. Suppose you like uppercase hexadecimal letters instead.
366
362
367
363
```js
368
-
const {Random} =require('entropy-string')
364
+
const {Entropy} =require('entropy-string')
369
365
370
-
constrandom=newRandom('0123456789ABCDEF')
371
-
conststring=random.string(48)
366
+
constentropy=newEntropy('0123456789ABCDEF')
367
+
conststring=entropy.string(48)
372
368
373
369
```
374
370
375
371
> string: 08BB82C0056A
376
372
377
-
The `Random` constructor allows for three separate cases:
373
+
The `Entropy` constructor allows for three separate cases:
378
374
379
375
- No argument defauls to the `charSet32` characters.
380
376
- One of six predefined `CharSet`s can be specified.
381
377
- A string representing the characters to use can be specified.
382
378
383
379
The last option above will throw an `EntropyStringError` if the characters string isn't appropriate for creating a `CharSet`.
384
380
```js
385
-
const {Random} =require('entropy-string')
381
+
const {Entropy} =require('entropy-string')
386
382
387
383
try {
388
-
constrandom=newRandom('123456')
384
+
constentropy=newEntropy('123456')
389
385
}
390
386
catch(error) {
391
387
console.log('Error: '+error.message)
@@ -396,7 +392,7 @@ The last option above will throw an `EntropyStringError` if the characters strin
396
392
397
393
```js
398
394
try {
399
-
constrandom=newRandom('01233210')
395
+
constentropy=newEntropy('01233210')
400
396
}
401
397
catch(error) {
402
398
console.log(error.message)
@@ -430,20 +426,20 @@ There are two significant issues with this code. `Math.random` returns a random
430
426
Compare that to the `entropy-string` scheme. For the example above, slicing off 5 bits at a time requires a total of 80 bits (10 bytes). Creating the same strings as above, `entropy-string` uses 80 bits of randomness per string with no wasted bits. In general, the `entropy-string` scheme can waste up to 7 bits per string, but that's the worst case scenario and that's *per string*, not *per character*!
431
427
432
428
```js
433
-
const {Random} =require('entropy-string')
429
+
const {Entropy} =require('entropy-string')
434
430
435
-
constrandom=newRandom()
436
-
let string =random.string(80)
431
+
constentropy=newEntropy()
432
+
let string =entropy.string(80)
437
433
```
438
434
439
435
> HFtgHQ9q9fH6B8HM
440
436
441
437
But there is an even bigger issue with the previous code from a security perspective. `Math.random`*is not a cryptographically strong random number generator*. **_Do not_** use `Math.random` to create strings used for security purposes! This highlights an important point. Strings are only capable of carrying information (entropy); it's the random bytes that actually provide the entropy itself. `entropy-string` automatically generates the necessary bytes needed to create cryptographically strong random strings using the `crypto` library.
442
438
443
-
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`:
439
+
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 `entropy.stringRandom`:
444
440
445
441
```js
446
-
string =random.stringRandom(80)
442
+
string =entropy.stringRandom(80)
447
443
```
448
444
449
445
> fdRp9Q3rTMF7TdFN
@@ -456,25 +452,25 @@ Fortunately you don't need to really understand how the bytes are efficiently sl
456
452
457
453
### <aname="CustomBytes"></a>Custom Bytes
458
454
459
-
As described in [Efficiency](#Efficiency), `entropy-string` automatically generates random bytes using the `crypto` library. But you may have a need to provide your own bytes, say for deterministic testing or to use a specialized byte generator. The `random.string` function allows passing in your own bytes to create a string.
455
+
As described in [Efficiency](#Efficiency), `entropy-string` automatically generates random bytes using the `crypto` library. But you may have a need to provide your own bytes, say for deterministic testing or to use a specialized byte generator. The `entropy.string` function allows passing in your own bytes to create a string.
460
456
461
457
Suppose we want a string capable of 30 bits of entropy using 32 characters. We pass in 4 bytes to cover the 30 bits needed to generate six base 32 characters:
462
458
463
459
```js
464
-
const {Random} =require('entropy-string')
460
+
const {Entropy} =require('entropy-string')
465
461
466
-
constrandom=newRandom()
462
+
constentropy=newEntropy()
467
463
constbytes=Buffer.from([250, 200, 150, 100])
468
-
let string =random.stringWithBytes(30, bytes)
464
+
let string =entropy.stringWithBytes(30, bytes)
469
465
```
470
466
471
467
> Th7fjL
472
468
473
-
The __bytes__ provided can come from any source. However, the number of bytes must be sufficient to generate the string as described in the [Efficiency](#Efficiency) section. `random.stringWithBytes` throws an `Error` if the string cannot be formed from the passed bytes.
469
+
The __bytes__ provided can come from any source. However, the number of bytes must be sufficient to generate the string as described in the [Efficiency](#Efficiency) section. `entropy.stringWithBytes` throws an `Error` if the string cannot be formed from the passed bytes.
474
470
475
471
```js
476
472
try {
477
-
string =random.stringWithBytes(32, bytes)
473
+
string =entropy.stringWithBytes(32, bytes)
478
474
}
479
475
catch(error) {
480
476
console.log(' Error: '+error.message)
@@ -526,13 +522,13 @@ The final line represents the number of entropy bits `N` as a function of the nu
526
522
527
523
##### Base 32 character string with a 1 in a million chance of a repeat a billion strings:
0 commit comments