Skip to content

Commit 632dfa7

Browse files
committed
Add eslint
Adhere to airbnb linting style Replace all ‘import’ usages with ‘require’ Restructure examples inclusion of lib files
1 parent 629edc6 commit 632dfa7

39 files changed

+1872
-1110
lines changed

.eslintrc.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"extends": "airbnb",
3+
"env": {
4+
"node": true
5+
},
6+
"rules": {
7+
"semi": ["error", "never"],
8+
"comma-dangle": ["error", "never"],
9+
"no-bitwise": ["error", { "allow": ["<<", ">>", "&"] }],
10+
"no-console": ["error", { "allow": ["error", "log"] }]
11+
}
12+
}

.vscode/settings.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"files.exclude": {
3+
".vscode": true,
4+
".eslintrc.json": true,
5+
"**/node_modules": true,
6+
".babelrc": true,
7+
".gitignore": true,
8+
".npmignore": true,
9+
".travis.yml": true,
10+
"yarn.lock": true,
11+
"package-lock.json": true,
12+
"dist": true,
13+
"images": true,
14+
"ISC.svg": true,
15+
"LICENSE": true,
16+
"**/*~": true
17+
},
18+
"git.ignoreLimitWarning": true
19+
}

README.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Run any of the examples in the `examples` directory by:
4949
Generate a potential of _1 million_ random strings with _1 in a billion_ chance of repeat:
5050

5151
```js
52-
import {Random, Entropy} from 'entropy-string'
52+
const {Random, Entropy} = require('entropy-string')
5353

5454
const random = new Random()
5555
const bits = Entropy.bits(1e6, 1e9)
@@ -64,7 +64,7 @@ See [Real Need](#RealNeed) for description of what entropy bits represents.
6464
`EntropyString` uses predefined `charset32` characters by default (see [Character Sets](#CharacterSets)). To get a random hexadecimal string with the same entropy `bits` as above:
6565

6666
```js
67-
import {Random, Entropy, charSet16} from 'entropy-string'
67+
const {Random, Entropy, charSet16} = require('entropy-string')
6868

6969
const random = new Random(charSet16)
7070
const bits = Entropy.bits(1e6, 1e9)
@@ -77,7 +77,7 @@ See [Real Need](#RealNeed) for description of what entropy bits represents.
7777
Custom characters may be specified. Using uppercase hexadecimal characters:
7878

7979
```js
80-
import {Random, Entropy} from 'entropy-string'
80+
const {Random, Entropy} = require('entropy-string')
8181

8282
const random = new Random('0123456789ABCDEF')
8383
const bits = Entropy.bits(1e6, 1e9)
@@ -90,7 +90,7 @@ Custom characters may be specified. Using uppercase hexadecimal characters:
9090
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:
9191

9292
```js
93-
import {Random} from 'entropy-string'
93+
const {Random} = require('entropy-string')
9494

9595
const random = new Random()
9696
const string = random.smallID()
@@ -101,7 +101,7 @@ Convenience functions `smallID`, `mediumID`, `largeID`, `sessionID` and `token`
101101
Or, to generate an OWASP session ID:
102102

103103
```js
104-
import {Random} from 'entropy-string'
104+
const {Random} = require('entropy-string')
105105

106106
const random = new Random()
107107
const string = random.sessionID()
@@ -111,7 +111,7 @@ Or, to generate an OWASP session ID:
111111
112112
Or perhaps you need an 256-bit token using [RFC 4648](https://tools.ietf.org/html/rfc4648#section-5) file system and URL safe characters:
113113
```js
114-
import {Random, Entropy, charSet64} from 'entropy-string'
114+
const {Random, Entropy, charSet64} = require('entropy-string')
115115

116116
const random = new Random(charSet64)
117117

@@ -181,7 +181,7 @@ How do you address this need using a library designed to generate strings of spe
181181
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:
182182

183183
```js
184-
import {Random, Entropy, charSet16} from 'entropy-string'
184+
const {Random, Entropy, charSet16} = require('entropy-string')
185185

186186
const bits = Entropy.bits(10000, 1000000)
187187
const random = new Random(charSet16)
@@ -228,7 +228,7 @@ In [Real Need](#RealNeed) our developer used hexadecimal characters for the stri
228228
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()`.
229229

230230
```js
231-
import {Random, Entropy} from 'entropy-string'
231+
const {Random, Entropy} = require('entropy-string')
232232

233233
const random = new Random()
234234
const bits = Entropy.bits(10000, 1e6)
@@ -242,7 +242,7 @@ We're using the same `Entropy.bits` calculation since we haven't changed the num
242242
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:
243243

244244
```js
245-
import {Random, Entropy, charSet16, charSet4} from 'entropy-string'
245+
const {Random, Entropy, charSet16, charSet4} = require('entropy-string')
246246

247247
const random = new Random(charSet16)
248248
const bits = Entropy.bits(30, 100000)
@@ -265,7 +265,7 @@ Okay, we probably wouldn't use 4 characters (and what's up with those characters
265265
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:
266266

267267
```js
268-
import {Random, Entropy} from 'entropy-string'
268+
const {Random, Entropy} = require('entropy-string')
269269

270270
const random = new Random()
271271
const bits = Entropy.bits(1e10, 1e12)
@@ -277,7 +277,7 @@ Suppose we have a more extreme need. We want less than a 1 in a trillion chance
277277
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).
278278

279279
```js
280-
import {Random} from 'entropy-string'
280+
const {Random} = require('entropy-string')
281281

282282
const random = new Random()
283283
const string = random.string(128)
@@ -288,7 +288,7 @@ Finally, let say we're generating session IDs. Since session IDs are ephemeral,
288288
Since session ID are such an important need, `entropy-string` provides a convenience function for generating them:
289289

290290
```js
291-
import {Random, charSet64} from 'entropy-string'
291+
const {Random, charSet64} = require('entropy-string')
292292

293293
const random = new Random(charSet64)
294294
const string = random.sessionID()
@@ -306,7 +306,7 @@ In using 64 characters, note our string length is 22 characters. That's actually
306306
As we've seen in the previous sections, `entropy-string` provides predefined character sets. Let's see what's under the hood.
307307

308308
```js
309-
import {charSet64} from 'entropy-string'
309+
const {charSet64} = require('entropy-string')
310310
const chars = charSet64.chars()
311311
```
312312

@@ -345,7 +345,7 @@ You may, of course, want to choose the characters used, which is covered next in
345345
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.
346346

347347
```js
348-
import {Random, charSet2} from 'entropy-string'
348+
const {Random, charSet2} = require('entropy-string')
349349

350350
const random = new Random(charSet2)
351351
let flips = random.string(10)
@@ -365,7 +365,7 @@ The resulting string of __0__'s and __1__'s doesn't look quite right. Perhaps yo
365365
As another example, we saw in [Character Sets](#CharacterSets) the predefined hex characters for `charSet16` are lowercase. Suppose you like uppercase hexadecimal letters instead.
366366

367367
```js
368-
import {Random} from 'entropy-string'
368+
const {Random} = require('entropy-string')
369369

370370
const random = new Random('0123456789ABCDEF')
371371
const string = random.string(48)
@@ -382,7 +382,7 @@ The `Random` constructor allows for three separate cases:
382382

383383
The last option above will throw an `EntropyStringError` if the characters string isn't appropriate for creating a `CharSet`.
384384
```js
385-
import {Random} from 'entropy-string'
385+
const {Random} = require('entropy-string')
386386

387387
try {
388388
const random = new Random('123456')
@@ -430,7 +430,7 @@ There are two significant issues with this code. `Math.random` returns a random
430430
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*!
431431

432432
```js
433-
import {Random} from 'entropy-string'
433+
const {Random} = require('entropy-string')
434434

435435
const random = new Random()
436436
let string = random.string(80)
@@ -461,7 +461,7 @@ As described in [Efficiency](#Efficiency), `entropy-string` automatically genera
461461
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:
462462

463463
```js
464-
import {Random} from 'entropy-string'
464+
const {Random} = require('entropy-string')
465465

466466
const random = new Random()
467467
const bytes = 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
526526

527527
##### Base 32 character string with a 1 in a million chance of a repeat a billion strings:
528528
```js
529-
import {Random, Entropy} from 'entropy-string'
529+
const {Random, Entropy} = require('entropy-string')
530530

531531
const random = new Random()
532532
const bits = Entropy.bits(1e6,1e9)

dist/lib/charSet.js

Lines changed: 45 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,52 @@ var _createClass2 = require('babel-runtime/helpers/createClass');
1717

1818
var _createClass3 = _interopRequireDefault(_createClass2);
1919

20-
var _lcm = require('./lcm');
20+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
2121

22-
var _lcm2 = _interopRequireDefault(_lcm);
22+
var WeakMap = require('weak-map');
2323

24-
var _weakMap = require('weak-map');
24+
var lcm = require('./lcm').default;
2525

26-
var _weakMap2 = _interopRequireDefault(_weakMap);
26+
var propMap = new WeakMap();
2727

28-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
28+
var BITS_PER_BYTE = 8;
29+
30+
var genNdxFn = function genNdxFn(bitsPerChar) {
31+
// If BITS_PER_BYTEs is a multiple of bitsPerChar, we can slice off an integer number
32+
// of chars per byte.
33+
if (lcm(bitsPerChar, BITS_PER_BYTE) === BITS_PER_BYTE) {
34+
return function (chunk, slice, bytes) {
35+
var lShift = bitsPerChar;
36+
var rShift = BITS_PER_BYTE - bitsPerChar;
37+
return (bytes[chunk] << lShift * slice & 0xff) >> rShift;
38+
};
39+
}
40+
// Otherwise, while slicing off bits per char, we will possibly straddle a couple
41+
// of bytes, so a bit more work is involved
2942

30-
var propMap = new _weakMap2.default();
43+
var slicesPerChunk = lcm(bitsPerChar, BITS_PER_BYTE) / BITS_PER_BYTE;
44+
return function (chunk, slice, bytes) {
45+
var bNum = chunk * slicesPerChunk;
3146

32-
var BITS_PER_BYTE = 8;
47+
var offset = slice * bitsPerChar / BITS_PER_BYTE;
48+
var lOffset = Math.floor(offset);
49+
var rOffset = Math.ceil(offset);
50+
51+
var rShift = BITS_PER_BYTE - bitsPerChar;
52+
var lShift = slice * bitsPerChar % BITS_PER_BYTE;
53+
54+
var ndx = (bytes[bNum + lOffset] << lShift & 0xff) >> rShift;
55+
56+
var r1Bits = (rOffset + 1) * BITS_PER_BYTE;
57+
var s1Bits = (slice + 1) * bitsPerChar;
58+
59+
var rShiftIt = (r1Bits - s1Bits) % BITS_PER_BYTE;
60+
if (rShift < rShiftIt) {
61+
ndx += bytes[bNum + rOffset] >> rShiftIt;
62+
}
63+
return ndx;
64+
};
65+
};
3366

3467
var CharSet = function () {
3568
function CharSet(chars) {
@@ -39,14 +72,15 @@ var CharSet = function () {
3972
throw new Error('Invalid chars: Must be string');
4073
}
4174
var length = chars.length;
75+
4276
if (![2, 4, 8, 16, 32, 64].includes(length)) {
4377
throw new Error('Invalid char count: must be one of 2,4,8,16,32,64');
4478
}
4579
var bitsPerChar = Math.floor((0, _log2.default)(length));
4680
// Ensure no repeated characters
47-
for (var i = 0; i < length; i++) {
81+
for (var i = 0; i < length; i += 1) {
4882
var c = chars.charAt(i);
49-
for (var j = i + 1; j < length; j++) {
83+
for (var j = i + 1; j < length; j += 1) {
5084
if (c === chars.charAt(j)) {
5185
throw new Error('Characters not unique');
5286
}
@@ -56,8 +90,8 @@ var CharSet = function () {
5690
chars: chars,
5791
bitsPerChar: bitsPerChar,
5892
length: length,
59-
ndxFn: _ndxFn(bitsPerChar),
60-
charsPerChunk: (0, _lcm2.default)(bitsPerChar, BITS_PER_BYTE) / bitsPerChar
93+
ndxFn: genNdxFn(bitsPerChar),
94+
charsPerChunk: lcm(bitsPerChar, BITS_PER_BYTE) / bitsPerChar
6195
};
6296
propMap.set(this, privProps);
6397
}
@@ -87,11 +121,6 @@ var CharSet = function () {
87121
value: function length() {
88122
return propMap.get(this).length;
89123
}
90-
}, {
91-
key: 'getNdxFn',
92-
value: function getNdxFn() {
93-
return propMap.get(this).ndxFn;
94-
}
95124
}, {
96125
key: 'bytesNeeded',
97126
value: function bytesNeeded(entropyBits) {
@@ -121,43 +150,6 @@ var CharSet = function () {
121150
}();
122151

123152
exports.default = CharSet;
124-
125-
126-
var _ndxFn = function _ndxFn(bitsPerChar) {
127-
// If BITS_PER_BYTEs is a multiple of bitsPerChar, we can slice off an integer number
128-
// of chars per byte.
129-
if ((0, _lcm2.default)(bitsPerChar, BITS_PER_BYTE) === BITS_PER_BYTE) {
130-
return function (chunk, slice, bytes) {
131-
var lShift = bitsPerChar;
132-
var rShift = BITS_PER_BYTE - bitsPerChar;
133-
return (bytes[chunk] << lShift * slice & 0xff) >> rShift;
134-
};
135-
}
136-
// Otherwise, while slicing off bits per char, we will possibly straddle a couple
137-
// of bytes, so a bit more work is involved
138-
else {
139-
var slicesPerChunk = (0, _lcm2.default)(bitsPerChar, BITS_PER_BYTE) / BITS_PER_BYTE;
140-
return function (chunk, slice, bytes) {
141-
var bNum = chunk * slicesPerChunk;
142-
143-
var offset = slice * bitsPerChar / BITS_PER_BYTE;
144-
var lOffset = Math.floor(offset);
145-
var rOffset = Math.ceil(offset);
146-
147-
var rShift = BITS_PER_BYTE - bitsPerChar;
148-
var lShift = slice * bitsPerChar % BITS_PER_BYTE;
149-
150-
var ndx = (bytes[bNum + lOffset] << lShift & 0xff) >> rShift;
151-
152-
var rShiftIt = ((rOffset + 1) * BITS_PER_BYTE - (slice + 1) * bitsPerChar) % BITS_PER_BYTE;
153-
if (rShift < rShiftIt) {
154-
ndx += bytes[bNum + rOffset] >> rShiftIt;
155-
}
156-
return ndx;
157-
};
158-
}
159-
};
160-
161153
var charSet64 = exports.charSet64 = new CharSet('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_');
162154
var charSet32 = exports.charSet32 = new CharSet('2346789bdfghjmnpqrtBDFGHJLMNPQRT');
163155
var charSet16 = exports.charSet16 = new CharSet('0123456789abcdef');

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