Skip to content

Commit dea7418

Browse files
committed
Rename randomString() to string()
Keep randomString until version 2.0
1 parent 5c6978b commit dea7418

File tree

6 files changed

+55
-51
lines changed

6 files changed

+55
-51
lines changed

README.md

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ Efficiently generate cryptographically strong random strings of specified entrop
4141
const entropy = require('entropy-string')
4242

4343
let bits = 48
44-
let string = entropy.randomString(bits, entropy.charSet32)
44+
let string = entropy.string(bits, entropy.charSet32)
4545
```
4646

4747
> MRd272t4G3
4848
4949
48-bit string using hex characters:
5050

5151
```js
52-
string = entropy.randomString(bits, entropy.charSet16)
52+
string = entropy.string(bits, entropy.charSet16)
5353
```
5454

5555
> 7973b7cf643c
@@ -58,7 +58,7 @@ Efficiently generate cryptographically strong random strings of specified entrop
5858

5959
```js
6060
entropy.charSet16.use('0123456789ABCDEF')
61-
string = entropy.randomString(bits, entropy.charSet16)
61+
string = entropy.string(bits, entropy.charSet16)
6262
```
6363

6464
> 6D98AA8E6A46
@@ -67,7 +67,7 @@ Base 32 character string with a 1 in a million chance of a repeat in 30 such str
6767

6868
```js
6969
bits = entropy.bits(30, 1000000)
70-
string = entropy.randomString(bits, entropy.charSet32)
70+
string = entropy.string(bits, entropy.charSet32)
7171
```
7272

7373
> BqMhJM
@@ -76,7 +76,7 @@ Base 32 character string with a 1 in a trillion chance of a repeat in 10 million
7676

7777
```js
7878
bits = entropy.bitsWithPowers(7, 12)
79-
string = entropy.randomString(bits, entropy.charSet32)
79+
string = entropy.string(bits, entropy.charSet32)
8080
```
8181

8282
> H9fT8qmMBd9qLfqmpm
@@ -85,7 +85,7 @@ OWASP session ID using file system and URL safe characters:
8585

8686
```js
8787
bits = 128
88-
string = entropy.randomString(bits, entropy.charSet64)
88+
string = entropy.string(bits, entropy.charSet64)
8989
```
9090

9191
> RX3FzLm2YZmeBT2Y5n_79C
@@ -156,7 +156,7 @@ Let's use `entropy-string` to help this developer by generating 5 IDs:
156156
let bits = entropy.bits(10000, 1000000)
157157
let strings = Array()
158158
for (let i = 0; i < 5; i++) {
159-
let string = entropy.randomString(bits, entropy.charSet16)
159+
let string = entropy.string(bits, entropy.charSet16)
160160
strings.push(string)
161161
}
162162
```
@@ -172,7 +172,7 @@ To generate the IDs, we first use
172172
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. Then inside a loop we used
173173

174174
```js
175-
let string = entropy.randomString(bits, entropy.charSet16)
175+
let string = entropy.string(bits, entropy.charSet16)
176176
```
177177

178178
to actually generate a random string of the specified entropy using hexadecimal (charSet16) characters. 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.
@@ -191,7 +191,7 @@ We'll start with using 32 characters. What 32 characters, you ask? Well, the [Ch
191191
const entropy = require('entropy-string')
192192

193193
let bits = entropy.bits(10000, 1000000)
194-
let string = entropy.randomString(bits, entropy.charSet32)
194+
let string = entropy.string(bits, entropy.charSet32)
195195
```
196196

197197
> String: jGjfNbdGQD
@@ -202,15 +202,15 @@ Now let's suppose we need to ensure the names of a handful of items are unique.
202202

203203
```js
204204
bits = entropy.bits(30, 100000)
205-
string = entropy.randomString(bits, entropy.charSet16, false)
205+
string = entropy.string(bits, entropy.charSet16, false)
206206
```
207207

208208
> String: dbf40a6
209209
210210
Using 4 characters:
211211

212212
```js
213-
string = entropy.randomString(bits, entropy.charSet4, false)
213+
string = entropy.string(bits, entropy.charSet4, false)
214214
```
215215

216216
> String: CGCCGTAGGATAT
@@ -221,15 +221,15 @@ Suppose we have a more extreme need. We want less than a 1 in a trillion chance
221221

222222
```js
223223
bits = entropy.bitsWithPowers(10, 12)
224-
string = entropy.randomString(bits, entropy.charSet32)
224+
string = entropy.string(bits, entropy.charSet32)
225225
```
226226

227227
> String: gQ4F7M2Rmp8GFmtPd9R78d
228228
229229
Finally, let say we're generating session IDs. We're not 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 needs to be 128 bits (since we read on some web site that session IDs should be 128 bits).
230230

231231
```js
232-
string = entropy.randomString(128, entropy.charSet64)
232+
string = entropy.string(128, entropy.charSet64)
233233
```
234234

235235
> String: lstRHhvumC-4DRKM_HAvCk
@@ -281,7 +281,7 @@ Being able to easily generate random strings is great, but what if you want to s
281281

282282
```js
283283
const entropy = require('entropy-string')
284-
let flips = entropy.randomString(10, entropy.charSet2)
284+
let flips = entropy.string(10, entropy.charSet2)
285285
```
286286

287287
> flips: 1111001011
@@ -290,7 +290,7 @@ The resulting string of __0__'s and __1__'s doesn't look quite right. Perhaps yo
290290

291291
```js
292292
entropy.charSet2.use('HT')
293-
flips = entropy.randomString(10, entropy.charSet2)
293+
flips = entropy.string(10, entropy.charSet2)
294294
```
295295

296296
> flips: THHTHTTHHT
@@ -299,7 +299,7 @@ As another example, we saw in [Character Sets](#CharacterSets) the default chara
299299

300300
```js
301301
entropy.charSet16.use('0123456789ABCDEF')
302-
let string = entropy.randomString(48, entropy.charSet16)
302+
let string = entropy.string(48, entropy.charSet16)
303303

304304
```
305305

@@ -308,7 +308,7 @@ As another example, we saw in [Character Sets](#CharacterSets) the default chara
308308
Or suppose you want a random password with numbers, lowercase letters and special characters.
309309

310310
```js
311-
let password = randomString.entropy(of: 64, using: .charSet64)
311+
let password = string.entropy(of: 64, using: .charSet64)
312312
print("password: \(password)")
313313
```
314314

@@ -365,7 +365,7 @@ Compare that to the `entropy-string` scheme. For the example above, slicing off
365365

366366
```js
367367
const entropy = require('entropy-string')
368-
let string = entropy.randomString(80, entropy.charSet32)
368+
let string = entropy.string(80, entropy.charSet32)
369369
```
370370

371371
> HFtgHQ9q9fH6B8HM
@@ -376,7 +376,7 @@ However, if you don't need cryptographically strong random strings, you can requ
376376

377377
```js
378378
const entropy = require('entropy-string')
379-
let string = entropy.randomString(80, entropy.charSet32, false)
379+
let string = entropy.string(80, entropy.charSet32, false)
380380
```
381381

382382
> fdRp9Q3rTMF7TdFN
@@ -389,24 +389,24 @@ Fortunately you don't need to really understand how the bytes are efficiently sl
389389

390390
## <a name="CustomBytes"></a>Custom Bytes
391391

392-
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.randomString` function allows passing in your own bytes to create a string.
392+
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.
393393

394394
Suppose we want a string capable of 30 bits of entropy using 32 characters. We pass in 4 bytes (to cover the 30 bits):
395395

396396
```js
397397
const entropy = require('entropy-string')
398398

399399
let bytes: RandomString.Bytes = new Uint8Array[250, 200, 150, 100]
400-
let string = entropy.randomString(30, entropy.charSet32, bytes)
400+
let string = entropy.string(30, entropy.charSet32, bytes)
401401
```
402402

403403
> string: Th7fjL
404404
405-
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.randomString` throws an `Error` if the string cannot be formed from the passed bytes.
405+
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.string` throws an `Error` if the string cannot be formed from the passed bytes.
406406

407407
```js
408408
try {
409-
string = entropy.randomString(32, entropy.charSet32, bytes)
409+
string = entropy.string(32, entropy.charSet32, bytes)
410410
}
411411
catch(error) {
412412
console.log(error.message)
@@ -442,7 +442,7 @@ Note how the number of bytes needed is dependent on the number of characters in
442442
let N = 1000000
443443
let n = 1000000000
444444
let bits = entropy.bits(N, n)
445-
let string = entropy.randomString(bits, entropy.charSet32)
445+
let string = entropy.string(bits, entropy.charSet32)
446446
```
447447

448448
> DdHrT2NdrHf8tM

dist/lib/entropy.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,18 @@ var bitsWithPowers = function bitsWithPowers(tPower, rPower) {
7474
return N;
7575
};
7676

77-
var randomString = function randomString(entropy, charSet) {
77+
var string = function string(entropy, charSet) {
7878
var opt = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
7979

8080
if (!(opt === null || opt instanceof Uint8Array || typeof opt == 'boolean')) {
8181
throw new Error('Optional 3rd argument must be either an Uint8Array or a boolean');
8282
}
8383

8484
var bytes = opt instanceof Uint8Array ? opt : opt === false ? _randomBytes(entropy, charSet) : _cryptoBytes(entropy, charSet);
85-
return randomStringWithBytes(entropy, charSet, bytes);
85+
return stringWithBytes(entropy, charSet, bytes);
8686
};
8787

88-
var randomStringWithBytes = function randomStringWithBytes(entropy, charSet, bytes) {
88+
var stringWithBytes = function stringWithBytes(entropy, charSet, bytes) {
8989
if (!_charSet2.default.isValid(charSet)) {
9090
throw new Error('Invalid CharSet');
9191
}
@@ -241,8 +241,10 @@ exports.default = {
241241
bits: bits,
242242
bitsWithRiskPower: bitsWithRiskPower,
243243
bitsWithPowers: bitsWithPowers,
244-
randomString: randomString,
245-
randomStringWithBytes: randomStringWithBytes,
244+
string: string,
245+
stringWithBytes: stringWithBytes,
246+
randomString: string,
247+
randomStringWithBytes: stringWithBytes,
246248
bytesNeeded: bytesNeeded,
247249

248250
charSet64: _charSet2.default.charSet64,

example.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@ const entropy = require('./entropy-string')
22

33
let bits = 48
44
console.log('\n48-bit string using base32 characters')
5-
console.log(' ' + entropy.randomString(bits, entropy.charSet32))
5+
console.log(' ' + entropy.string(bits, entropy.charSet32))
66

77
console.log('\n48-bit string using hex characters')
8-
console.log(' ' + entropy.randomString(bits, entropy.charSet16))
8+
console.log(' ' + entropy.string(bits, entropy.charSet16))
99

1010
console.log('\n48-bit string using uppercase hex characters')
1111
entropy.charSet16.use('1234567890ABCDEF')
12-
console.log(' ' + entropy.randomString(bits, entropy.charSet16))
12+
console.log(' ' + entropy.string(bits, entropy.charSet16))
1313

1414
console.log('\nBase 32 character string with a 1 in a million chance of a repeat in 30 such strings')
1515
bits = entropy.bits(30, 1000000)
16-
console.log(' ' + entropy.randomString(bits, entropy.charSet32))
16+
console.log(' ' + entropy.string(bits, entropy.charSet32))
1717

1818
console.log('\nBase 32 character string with a 1 in a trillion chance of a repeat in 10 million such strings')
1919
bits = entropy.bitsWithPowers(7, 12)
20-
console.log(' ' + entropy.randomString(bits, entropy.charSet32))
20+
console.log(' ' + entropy.string(bits, entropy.charSet32))
2121

2222
bits = 128
2323
console.log('\nOWASP session ID using file system and URL safe characters')
24-
console.log(' ' + entropy.randomString(bits, entropy.charSet64))
24+
console.log(' ' + entropy.string(bits, entropy.charSet64))
2525

2626
console.log('')

lib/entropy.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,17 @@ const bitsWithPowers = (tPower, rPower) => {
5252
return N
5353
}
5454

55-
const randomString = (entropy, charSet, opt = null) => {
55+
const string = (entropy, charSet, opt = null) => {
5656
if ( !( (opt === null) || (opt instanceof Uint8Array) || (typeof opt == 'boolean') ) ) {
5757
throw new Error('Optional 3rd argument must be either an Uint8Array or a boolean')
5858
}
5959

6060
let bytes = (opt instanceof Uint8Array) ? opt :
6161
(opt === false) ? _randomBytes(entropy, charSet) : _cryptoBytes(entropy, charSet)
62-
return randomStringWithBytes(entropy, charSet, bytes)
62+
return stringWithBytes(entropy, charSet, bytes)
6363
}
6464

65-
const randomStringWithBytes = (entropy, charSet, bytes) => {
65+
const stringWithBytes = (entropy, charSet, bytes) => {
6666
if (!CharSet.isValid(charSet)) {
6767
throw new Error('Invalid CharSet')
6868
}
@@ -209,8 +209,10 @@ export default {
209209
bits : bits,
210210
bitsWithRiskPower : bitsWithRiskPower,
211211
bitsWithPowers : bitsWithPowers,
212-
randomString : randomString,
213-
randomStringWithBytes : randomStringWithBytes,
212+
string : string,
213+
stringWithBytes : stringWithBytes,
214+
randomString : string,
215+
randomStringWithBytes : stringWithBytes,
214216
bytesNeeded : bytesNeeded,
215217

216218
charSet64: CharSet.charSet64,

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": "1.2.1",
3+
"version": "1.3.0",
44
"description": "Efficiently generate cryptographically strong random strings of specified entropy from various character sets.",
55
"main": "entropy-string.js",
66
"directories": {

test/entropy.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ test('Invalid bytes', t => {
441441

442442
test('Invalid Char Set', t => {
443443
try {
444-
entropy.randomString(5, entropy.charSet6)
444+
entropy.string(5, entropy.charSet6)
445445
t.fail()
446446
}
447447
catch(error) {
@@ -454,7 +454,7 @@ test('Custom 64 chars', t => {
454454
try {
455455
charSet.use('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ9876543210_-')
456456
let bytes = new Uint8Array([0x9d, 0x99, 0x4e, 0xa5, 0xd2, 0x3f, 0x8c, 0x86, 0x80])
457-
let string = entropy.randomString(72, charSet, bytes)
457+
let string = entropy.string(72, charSet, bytes)
458458

459459
t.is(string, 'NzLoPDi-JiAa')
460460
}
@@ -472,7 +472,7 @@ test('Custom 32 chars', t => {
472472
try {
473473
charSet.use('2346789BDFGHJMNPQRTbdfghjlmnpqrt')
474474
let bytes = new Uint8Array([0xd2, 0xe3, 0xe9, 0xda, 0x19, 0x97, 0x52])
475-
let string = entropy.randomString(55, charSet, bytes)
475+
let string = entropy.string(55, charSet, bytes)
476476
t.is(string, 'mHRrbgQlTqF')
477477
}
478478
catch(error) {
@@ -488,7 +488,7 @@ test('Custom 16 chars', t => {
488488
let charSet = entropy.charSet16
489489
try {
490490
charSet.use('0123456789ABCDEF')
491-
let string = entropy.randomString(20, charSet, new Uint8Array([0xc7, 0xc9, 0x00]))
491+
let string = entropy.string(20, charSet, new Uint8Array([0xc7, 0xc9, 0x00]))
492492
t.is(string, 'C7C90')
493493
}
494494
catch(error) {
@@ -504,7 +504,7 @@ test('Custom 8 chars', t => {
504504
let charSet = entropy.charSet8
505505
try {
506506
charSet.use('abcdefgh')
507-
let string = entropy.randomString(30, charSet, new Uint8Array([0xc7, 0xc9, 0x07, 0xc9]))
507+
let string = entropy.string(30, charSet, new Uint8Array([0xc7, 0xc9, 0x07, 0xc9]))
508508
t.is(string, 'gbheeeahgc')
509509
}
510510
catch(error) {
@@ -520,7 +520,7 @@ test('Custom 4 chars', t => {
520520
let charSet = entropy.charSet4
521521
try {
522522
charSet.use('atcg')
523-
let string = entropy.randomString(16, charSet, new Uint8Array([0x20, 0xf1]))
523+
let string = entropy.string(16, charSet, new Uint8Array([0x20, 0xf1]))
524524
t.is(string, 'acaaggat')
525525
}
526526
catch(error) {
@@ -536,7 +536,7 @@ test('Custom 2 chars', t => {
536536
let charSet = entropy.charSet2
537537
try {
538538
charSet.use('HT')
539-
let string = entropy.randomString(16, charSet, new Uint8Array([0xe3, 0xe9]))
539+
let string = entropy.string(16, charSet, new Uint8Array([0xe3, 0xe9]))
540540
t.is(string, 'TTTHHHTTTTTHTHHT')
541541
}
542542
catch(error) {
@@ -576,15 +576,15 @@ test('No crypto', t => {
576576

577577
const entropyString = (bits, charSet, arr) => {
578578
let bytes = Buffer.from(arr)
579-
return entropy.randomString(bits, charSet, bytes)
579+
return entropy.string(bits, charSet, bytes)
580580
}
581581

582582
const entropyStringLength = (bits, charSet) => {
583-
return entropy.randomString(bits, charSet).length
583+
return entropy.string(bits, charSet).length
584584
}
585585

586586
const entropyStringLengthNoCrypto = (bits, charSet) => {
587-
return entropy.randomString(bits, charSet, false).length
587+
return entropy.string(bits, charSet, false).length
588588
}
589589

590590
const bitsStringLength = (total, risk, charSet) => {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy