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
@@ -64,7 +64,7 @@ See [Real Need](#RealNeed) for description of what entropy bits represents.
64
64
`EntropyString` uses predefined `charset32` characters by default (see [Character Sets](#CharacterSets)). To get a random hexadecimal string with the same entropy `bits` as above:
@@ -90,7 +90,7 @@ Custom characters may be specified. Using uppercase hexadecimal characters:
90
90
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:
@@ -181,7 +181,7 @@ How do you address this need using a library designed to generate strings of spe
181
181
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:
@@ -228,7 +228,7 @@ In [Real Need](#RealNeed) our developer used hexadecimal characters for the stri
228
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()`.
@@ -242,7 +242,7 @@ We're using the same `Entropy.bits` calculation since we haven't changed the num
242
242
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:
@@ -265,7 +265,7 @@ Okay, we probably wouldn't use 4 characters (and what's up with those characters
265
265
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:
@@ -277,7 +277,7 @@ Suppose we have a more extreme need. We want less than a 1 in a trillion chance
277
277
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
278
279
279
```js
280
-
import {Random} from'entropy-string'
280
+
const {Random} =require('entropy-string')
281
281
282
282
constrandom=newRandom()
283
283
conststring=random.string(128)
@@ -288,7 +288,7 @@ Finally, let say we're generating session IDs. Since session IDs are ephemeral,
288
288
Since session ID are such an important need, `entropy-string` provides a convenience function for generating them:
@@ -306,7 +306,7 @@ In using 64 characters, note our string length is 22 characters. That's actually
306
306
As we've seen in the previous sections, `entropy-string` provides predefined character sets. Let's see what's under the hood.
307
307
308
308
```js
309
-
import {charSet64} from'entropy-string'
309
+
const {charSet64} =require('entropy-string')
310
310
constchars=charSet64.chars()
311
311
```
312
312
@@ -345,7 +345,7 @@ You may, of course, want to choose the characters used, which is covered next in
345
345
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.
@@ -365,7 +365,7 @@ The resulting string of __0__'s and __1__'s doesn't look quite right. Perhaps yo
365
365
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
366
367
367
```js
368
-
import {Random} from'entropy-string'
368
+
const {Random} =require('entropy-string')
369
369
370
370
constrandom=newRandom('0123456789ABCDEF')
371
371
conststring=random.string(48)
@@ -382,7 +382,7 @@ The `Random` constructor allows for three separate cases:
382
382
383
383
The last option above will throw an `EntropyStringError` if the characters string isn't appropriate for creating a `CharSet`.
384
384
```js
385
-
import {Random} from'entropy-string'
385
+
const {Random} =require('entropy-string')
386
386
387
387
try {
388
388
constrandom=newRandom('123456')
@@ -430,7 +430,7 @@ There are two significant issues with this code. `Math.random` returns a random
430
430
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
431
432
432
```js
433
-
import {Random} from'entropy-string'
433
+
const {Random} =require('entropy-string')
434
434
435
435
constrandom=newRandom()
436
436
let string =random.string(80)
@@ -461,7 +461,7 @@ As described in [Efficiency](#Efficiency), `entropy-string` automatically genera
461
461
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
462
463
463
```js
464
-
import {Random} from'entropy-string'
464
+
const {Random} =require('entropy-string')
465
465
466
466
constrandom=newRandom()
467
467
constbytes=Buffer.from([250, 200, 150, 100])
@@ -526,7 +526,7 @@ The final line represents the number of entropy bits `N` as a function of the nu
526
526
527
527
##### Base 32 character string with a 1 in a million chance of a repeat a billion strings:
0 commit comments