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
@@ -217,13 +217,13 @@ Finally, given that the strings are 12 hexadecimals long, each string actually h
217
217
218
218
In [Real Need](#RealNeed) our developer used hexadecimal characters for the strings. Let's look at using other characters instead.
219
219
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. For now we'll stick to the provided defaults. `entropy-string` uses `charSet32` characters by default, so we don't need to pass that parameter in to the `random.string` call.
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.
221
221
222
222
```js
223
223
import {Random, Entropy} from'entropy-string'
224
224
225
-
let bits =Entropy.bits(10000, 1000000)
226
225
let random =newRandom()
226
+
let bits =Entropy.bitsWithRiskPower(10000, 6)
227
227
let string =random.string(bits)
228
228
```
229
229
@@ -236,8 +236,8 @@ As another example, let's assume we need to ensure the names of a handful of ite
@@ -259,14 +259,14 @@ Suppose we have a more extreme need. We want less than a 1 in a trillion chance
259
259
```js
260
260
import {Random, Entropy} from'entropy-string'
261
261
262
-
let bits =Entropy.bitsWithPowers(10, 12)
263
262
let random =newRandom()
263
+
let bits =Entropy.bitsWithPowers(10, 12)
264
264
let string =random.string(bits)
265
265
```
266
266
267
267
> String: 4J86pbFG9BqdBjTLfD3rt6
268
268
269
-
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 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 some web site that session IDs should be 128 bits).
269
+
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).
270
270
271
271
```js
272
272
import {Random} from'entropy-string'
@@ -304,7 +304,7 @@ As we've seen in the previous sections, `entropy-string` provides default charac
The available CharSets are `charSet64`, `charSet32`, `charSet16`, `charSet8`, `charSet4` and `charSet2`. The default characters for each were chosen as follows:
307
+
The available `CharSet`s are *charSet64*, *charSet32*, *charSet16*, *charSet8*, *charSet4* and *charSet2*. The default 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).
@@ -350,7 +350,7 @@ The resulting string of __0__'s and __1__'s doesn't look quite right. Perhaps yo
350
350
351
351
> flips: THHTHTTHHT
352
352
353
-
As another example, we saw in [Character Sets](#CharacterSets) the default characters for CharSet 16 are **0123456789abcdef**. Suppose you like uppercase hexadecimal letters instead.
353
+
As another example, we saw in [Character Sets](#CharacterSets) the default characters for `charSet16` are **0123456789abcdef**. Suppose you like uppercase hexadecimal letters instead.
354
354
355
355
```js
356
356
import {Random} from'entropy-string'
@@ -362,14 +362,13 @@ As another example, we saw in [Character Sets](#CharacterSets) the default chara
362
362
363
363
> string: 08BB82C0056A
364
364
365
-
The `Random` constructor allows for 3 separate cases:
365
+
The `Random` constructor allows for three separate cases:
366
366
367
367
- No argument defauls to the `charSet32` characters.
368
-
- One of the six default character sets can be specified.
368
+
- One of six default `CharSet`s can be specified.
369
369
- A string representing the characters to use can be specified.
370
370
371
-
The 3rd option above will throw an `Error` if the characters string isn't appropriate.
372
-
371
+
The 3rd option above will throw an `EntropyStringError` if the characters string isn't appropriate for creating a `CharSet`.
373
372
```js
374
373
import {Random} from'entropy-string'
375
374
@@ -414,7 +413,7 @@ The `entropy-string` scheme is also efficient with regard to the amount of rando
414
413
415
414
> bl0mvxXAqXuz5R3N
416
415
417
-
There are two significant issues with this code. `Math.random` returns a random `float` value. At the very best this value has about 53-bits of entropy. Let's assume it's 52-bits for argument sake, i.e. `Math.random` generates 52 bits of randomness per call. That randomness is in turn used to create an index. Since there are 62 **chars**, each represents 5.95 bits of entropy. So if we're creating strings with **length=16**, the 16 calls generate a total of 816 bits of randomness which are used to inject a total of 95.2 bits of entropy (5.95/char) into **string**. That means 720 bits (88% of the total) of the generated randomness is simply wasted.
416
+
There are two significant issues with this code. `Math.random` returns a random `float` value. At the very best this value has about 53-bits of entropy. Let's assume it's 52-bits for argument sake, i.e. `Math.random` generates 52 bits of randomness per call. That randomness is in turn used to create an index into the 62 **chars**, each which represents 5.95 bits of entropy. So if we're creating strings with **length=16**, the 16 calls generate a total of `16*52 = 816` bits of randomness which are used to inject a total of 95.2 bits of entropy (5.95/char) into **string**. That means 720 bits (88% of the total) of the generated randomness is simply wasted.
418
417
419
418
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*!
420
419
@@ -472,7 +471,7 @@ The __bytes__ provided can come from any source. However, the number of bytes mu
472
471
473
472
> error: Insufficient bytes: need 5 and got 4
474
473
475
-
Note how the number of bytes needed is dependent on the number of characters in our set. In using a string to represent entropy, we can only have multiples of the bits of entropy per character used. So in the example above, to get at least 32 bits of entropy using a character set of 32 characters (5 bits per char), we'll need enough bytes to cover 35 bits, not 32, so an `Error` is thrown.
474
+
Note the number of bytes needed is dependent on the number of characters in our set. In using a string to represent entropy, we can only have multiples of the bits of entropy per character used. So in the example above, to get at least 32 bits of entropy using a character set of 32 characters (5 bits per char), we'll need enough bytes to cover 35 bits, not 32, so an `Error` is thrown.
476
475
477
476
[TOC](#TOC)
478
477
@@ -491,7 +490,7 @@ Note how the number of bytes needed is dependent on the number of characters in
491
490
- The characters are arbitrary.
492
491
- You need `entropy-string`.
493
492
494
-
##### In a *million* strings, a *1 in a billion* chance of a repeat (using 32 possible characters):
493
+
##### Base 32 character string with a 1 in a million chance of a repeat a billion strings:
0 commit comments