Skip to content

Commit 37d45f7

Browse files
committed
Remove overloaded functions
Remove use of parameter types and flags to achieve “overload” function names
1 parent 0aa2ad1 commit 37d45f7

File tree

7 files changed

+38
-46
lines changed

7 files changed

+38
-46
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,20 +202,20 @@ 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.string(bits, entropy.charSet16, false)
205+
string = entropy.string(bits, entropy.charSet16)
206206
```
207207

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

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

216216
> String: CGCCGTAGGATAT
217217
218-
Okay, we probably wouldn't use 4 characters (and what's up with those characters?), but you get the idea. The [Efficiency](#Efficiency) explains the 3rd argument of `false`.
218+
Okay, we probably wouldn't use 4 characters (and what's up with those characters?), but you get the idea.
219219

220220
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 risk (trillion) is 10 to the 12th and our total (10 billion) is 10 to the 10th, so:
221221

@@ -372,11 +372,11 @@ Compare that to the `entropy-string` scheme. For the example above, slicing off
372372
373373
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.
374374

375-
However, if you don't need cryptographically strong random strings, you can request `entropy-string` use `Math.random` rather than the `crypto` library by passing in a 3rd argument of `false`:
375+
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`:
376376

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

382382
> fdRp9Q3rTMF7TdFN

dist/lib/charSet.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,6 @@ var CharSet = function () {
6363
return CharSet;
6464
}();
6565

66-
var charSet64 = new CharSet("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_", _ndx64);
67-
var charSet32 = new CharSet("2346789bdfghjmnpqrtBDFGHJLMNPQRT", _ndx32);
68-
var charSet16 = new CharSet("0123456789abcdef", _ndx16);
69-
var charSet8 = new CharSet("01234567", _ndx8);
70-
var charSet4 = new CharSet("ATCG", _ndx4);
71-
var charSet2 = new CharSet("01", _ndx2);
72-
73-
var isValid = function isValid(charSet) {
74-
return charSet instanceof CharSet;
75-
};
76-
7766
var _ndx64 = function _ndx64(chunk, slice, bytes) {
7867
return _ndxGen(chunk, slice, bytes, 6);
7968
};
@@ -119,6 +108,17 @@ var _ndxGen = function _ndxGen(chunk, slice, bytes, bitsPerSlice) {
119108
return ndx;
120109
};
121110

111+
var charSet64 = new CharSet("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_", _ndx64);
112+
var charSet32 = new CharSet("2346789bdfghjmnpqrtBDFGHJLMNPQRT", _ndx32);
113+
var charSet16 = new CharSet("0123456789abcdef", _ndx16);
114+
var charSet8 = new CharSet("01234567", _ndx8);
115+
var charSet4 = new CharSet("ATCG", _ndx4);
116+
var charSet2 = new CharSet("01", _ndx2);
117+
118+
var isValid = function isValid(charSet) {
119+
return charSet instanceof CharSet;
120+
};
121+
122122
exports.default = {
123123
charSet64: charSet64,
124124
charSet32: charSet32,

dist/lib/entropy.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,11 @@ var bitsWithPowers = function bitsWithPowers(tPower, rPower) {
7575
};
7676

7777
var string = function string(entropy, charSet) {
78-
var opt = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
79-
80-
if (!(opt === null || opt instanceof Uint8Array || typeof opt == 'boolean')) {
81-
throw new Error('Optional 3rd argument must be either an Uint8Array or a boolean');
82-
}
78+
return stringWithBytes(entropy, charSet, _cryptoBytes(entropy, charSet));
79+
};
8380

84-
var bytes = opt instanceof Uint8Array ? opt : opt === false ? _randomBytes(entropy, charSet) : _cryptoBytes(entropy, charSet);
85-
return stringWithBytes(entropy, charSet, bytes);
81+
var stringRandom = function stringRandom(entropy, charSet) {
82+
return stringWithBytes(entropy, charSet, _randomBytes(entropy, charSet));
8683
};
8784

8885
var stringWithBytes = function stringWithBytes(entropy, charSet, bytes) {
@@ -166,9 +163,8 @@ exports.default = {
166163
bitsWithRiskPower: bitsWithRiskPower,
167164
bitsWithPowers: bitsWithPowers,
168165
string: string,
166+
stringRandom: stringRandom,
169167
stringWithBytes: stringWithBytes,
170-
randomString: string,
171-
randomStringWithBytes: stringWithBytes,
172168
bytesNeeded: bytesNeeded,
173169

174170
charSet64: _charSet2.default.charSet64,

example.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ print('\nBase 32 character string with a 1 in a trillion chance of a repeat in 1
2020
bits = entropy.bitsWithPowers(7, 12)
2121
print(' ' + entropy.string(bits, entropy.charSet32))
2222

23-
print('\nAs above, but with "my" characters"')
23+
print('\nAs above, but with "my" base 8 characters')
2424
entropy.charSet8.use('dingosky')
2525
print(' ' + entropy.string(bits, entropy.charSet8))
2626

lib/entropy.js

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

55-
const string = (entropy, charSet, opt = null) => {
56-
if ( !( (opt === null) || (opt instanceof Uint8Array) || (typeof opt == 'boolean') ) ) {
57-
throw new Error('Optional 3rd argument must be either an Uint8Array or a boolean')
58-
}
59-
60-
let bytes = (opt instanceof Uint8Array) ? opt :
61-
(opt === false) ? _randomBytes(entropy, charSet) : _cryptoBytes(entropy, charSet)
62-
return stringWithBytes(entropy, charSet, bytes)
55+
const string = (entropy, charSet) => {
56+
return stringWithBytes(entropy, charSet, _cryptoBytes(entropy, charSet))
57+
}
58+
59+
const stringRandom = (entropy, charSet) => {
60+
return stringWithBytes(entropy, charSet, _randomBytes(entropy, charSet))
6361
}
6462

6563
const stringWithBytes = (entropy, charSet, bytes) => {
@@ -135,9 +133,8 @@ export default {
135133
bitsWithRiskPower : bitsWithRiskPower,
136134
bitsWithPowers : bitsWithPowers,
137135
string : string,
136+
stringRandom : stringRandom,
138137
stringWithBytes : stringWithBytes,
139-
randomString : string,
140-
randomStringWithBytes : stringWithBytes,
141138
bytesNeeded : bytesNeeded,
142139

143140
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.3.1",
3+
"version": "1.4.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: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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.string(72, charSet, bytes)
457+
let string = entropy.stringWithBytes(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.string(55, charSet, bytes)
475+
let string = entropy.stringWithBytes(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.string(20, charSet, new Uint8Array([0xc7, 0xc9, 0x00]))
491+
let string = entropy.stringWithBytes(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.string(30, charSet, new Uint8Array([0xc7, 0xc9, 0x07, 0xc9]))
507+
let string = entropy.stringWithBytes(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.string(16, charSet, new Uint8Array([0x20, 0xf1]))
523+
let string = entropy.stringWithBytes(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.string(16, charSet, new Uint8Array([0xe3, 0xe9]))
539+
let string = entropy.stringWithBytes(16, charSet, new Uint8Array([0xe3, 0xe9]))
540540
t.is(string, 'TTTHHHTTTTTHTHHT')
541541
}
542542
catch(error) {
@@ -575,16 +575,15 @@ test('No crypto', t => {
575575
})
576576

577577
const entropyString = (bits, charSet, arr) => {
578-
let bytes = Buffer.from(arr)
579-
return entropy.string(bits, charSet, bytes)
578+
return entropy.stringWithBytes(bits, charSet, Buffer.from(arr))
580579
}
581580

582581
const entropyStringLength = (bits, charSet) => {
583582
return entropy.string(bits, charSet).length
584583
}
585584

586585
const entropyStringLengthNoCrypto = (bits, charSet) => {
587-
return entropy.string(bits, charSet, false).length
586+
return entropy.stringRandom(bits, charSet).length
588587
}
589588

590589
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