Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 154051a

Browse files
committed
README and example updates
1 parent b0b1b88 commit 154051a

File tree

9 files changed

+27
-28
lines changed

9 files changed

+27
-28
lines changed

README.md

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,24 +68,24 @@ OWASP session ID using [RFC 4648](https://tools.ietf.org/html/rfc4648#section-5)
6868

6969
> 7973b7cf643c
7070
71-
48-bit string using uppercase hex characters:
71+
96-bit string using uppercase hex characters:
7272

7373
```js
7474
import {Random} from 'entropy-string'
7575

7676
let random = new Random('0123456789ABCDEF')
77-
let string = random.string(48)
77+
let string = random.string(95)
7878
```
7979

80-
> 6D98AA8E6A46
80+
> 134BBC6465B0DF101BFBC44B
8181
8282
Base 32 character string with a 1 in a million chance of a repeat in 30 strings:
8383

8484
```js
8585
import {Random, Entropy} from 'entropy-string'
8686

87-
let bits = Entropy.bits(30, 1000000)
8887
let random = new Random()
88+
let bits = Entropy.bits(30, 1000000)
8989
let string = random.string(bits)
9090
```
9191

@@ -96,8 +96,8 @@ Base 64 character string with a 1 in a trillion chance of a repeat in 100 millio
9696
```js
9797
import {Random, Entropy, charSet64} from 'entropy-string'
9898

99-
let bits = Entropy.bitsWithPowers(7, 12)
10099
let random = new Random(charSet64)
100+
let bits = Entropy.bitsWithPowers(7, 12)
101101
let string = random.string(bits)
102102
```
103103

@@ -175,8 +175,8 @@ Let's use `entropy-string` to help this developer generate 5 IDs:
175175
```js
176176
import {Random, Entropy, charSet16} from 'entropy-string'
177177

178-
let bits = Entropy.bits(10000, 1000000)
179178
let random = new Random(charSet16)
179+
let bits = Entropy.bits(10000, 1000000)
180180
let strings = Array()
181181
for (let i = 0; i < 5; i++) {
182182
let string = random.string(bits)
@@ -217,13 +217,13 @@ Finally, given that the strings are 12 hexadecimals long, each string actually h
217217

218218
In [Real Need](#RealNeed) our developer used hexadecimal characters for the strings. Let's look at using other characters instead.
219219

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.
221221

222222
```js
223223
import {Random, Entropy} from 'entropy-string'
224224

225-
let bits = Entropy.bits(10000, 1000000)
226225
let random = new Random()
226+
let bits = Entropy.bitsWithRiskPower(10000, 6)
227227
let string = random.string(bits)
228228
```
229229

@@ -236,8 +236,8 @@ As another example, let's assume we need to ensure the names of a handful of ite
236236
```js
237237
import {Random, Entropy, charSet16, charSet4} from 'entropy-string'
238238

239-
let bits = Entropy.bits(30, 100000)
240239
let random = new Random(charSet16)
240+
let bits = Entropy.bits(30, 100000)
241241
let string = random.string(bits)
242242
```
243243

@@ -259,14 +259,14 @@ Suppose we have a more extreme need. We want less than a 1 in a trillion chance
259259
```js
260260
import {Random, Entropy} from 'entropy-string'
261261

262-
let bits = Entropy.bitsWithPowers(10, 12)
263262
let random = new Random()
263+
let bits = Entropy.bitsWithPowers(10, 12)
264264
let string = random.string(bits)
265265
```
266266

267267
> String: 4J86pbFG9BqdBjTLfD3rt6
268268
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).
270270

271271
```js
272272
import {Random} from 'entropy-string'
@@ -304,7 +304,7 @@ As we've seen in the previous sections, `entropy-string` provides default charac
304304

305305
> ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_
306306
307-
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:
308308

309309
- CharSet 64: **ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_**
310310
* 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
350350

351351
> flips: THHTHTTHHT
352352
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.
354354

355355
```js
356356
import {Random} from 'entropy-string'
@@ -362,14 +362,13 @@ As another example, we saw in [Character Sets](#CharacterSets) the default chara
362362

363363
> string: 08BB82C0056A
364364
365-
The `Random` constructor allows for 3 separate cases:
365+
The `Random` constructor allows for three separate cases:
366366

367367
- 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.
369369
- A string representing the characters to use can be specified.
370370

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`.
373372
```js
374373
import {Random} from 'entropy-string'
375374

@@ -414,7 +413,7 @@ The `entropy-string` scheme is also efficient with regard to the amount of rando
414413

415414
> bl0mvxXAqXuz5R3N
416415
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.
418417

419418
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*!
420419

@@ -472,7 +471,7 @@ The __bytes__ provided can come from any source. However, the number of bytes mu
472471

473472
> error: Insufficient bytes: need 5 and got 4
474473
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.
476475

477476
[TOC](#TOC)
478477

@@ -491,7 +490,7 @@ Note how the number of bytes needed is dependent on the number of characters in
491490
- The characters are arbitrary.
492491
- You need `entropy-string`.
493492

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:
495494
```js
496495
import {Random, Entropy} from 'entropy-string'
497496

examples/gen5.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {Random, Entropy, charSet16} from './entropy-string'
22

3-
let bits = Entropy.bits(10000, 1000000)
43
let random = new Random(charSet16)
4+
let bits = Entropy.bits(10000, 1000000)
55
let strings = Array()
66
for (let i = 0; i < 5; i++) {
77
let string = random.string(bits)

examples/more_1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {Random, Entropy} from './entropy-string'
22

3-
let bits = Entropy.bits(10000, 1000000)
43
let random = new Random()
4+
let bits = Entropy.bits(10000, 1000000)
55
let string = random.string(bits)
66
console.log('\n Base 32 string : ' + string + '\n')
77

examples/more_2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {Random, Entropy, charSet16, charSet4} from './entropy-string'
22

3-
let bits = Entropy.bits(30, 100000)
43
let random = new Random(charSet16)
4+
let bits = Entropy.bits(30, 100000)
55
let string = random.string(bits)
66
console.log('\n Base 16 string : ' + string)
77

examples/more_3.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Random, Entropy} from './entropy-string'
22

3-
let bits = Entropy.bitsWithPowers(10, 12)
43
let random = new Random()
4+
let bits = Entropy.bitsWithPowers(10, 12)
55
let string = random.string(bits)
66
console.log('\n Base 32 string : ' + string + '\n')

examples/tldr2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Random, Entropy} from './entropy-string'
22

3-
let bits = Entropy.bitsWithPowers(6,9)
43
let random = new Random()
4+
let bits = Entropy.bitsWithPowers(6,9)
55
let string = random.string(bits)
66
console.log('\n Base32 string with a 1 in a billion chance of repeat in a million strings: ' + string + '\n')

examples/tldr_5.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Random, Entropy} from './entropy-string'
22

3-
let bits = Entropy.bits(30, 1000000)
43
let random = new Random()
4+
let bits = Entropy.bits(30, 1000000)
55
let string = random.string(bits)
66
console.log('\n Base 32 character string with a 1 in a million chance of a repeat in 30 strings: ' + string + '\n')

examples/tldr_6.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Random, Entropy, charSet64} from './entropy-string'
22

3-
let bits = Entropy.bitsWithPowers(7, 12)
43
let random = new Random(charSet64)
4+
let bits = Entropy.bitsWithPowers(7, 12)
55
let string = random.string(bits)
66
console.log('\n Base 64 character string with a 1 in a trillion chance of a repeat in 100 million strings: ' + string + '\n')

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "entropy-string",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "Efficiently generate cryptographically strong random strings of specified entropy from various character sets.",
55
"main": "entropy-string.js",
66
"directories": {

0 commit comments

Comments
 (0)