Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit feac970

Browse files
committed
Update README
1 parent a0ad572 commit feac970

File tree

5 files changed

+47
-88
lines changed

5 files changed

+47
-88
lines changed

‎README.md‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Efficiently generate cryptographically strong random strings of specified entrop
1515
- [Efficiency](#Efficiency)
1616
- [Custom Bytes](#CustomBytes)
1717
- [No Crypto](#NoCrypto)
18+
- [Browser Version](#BrowserVersion)
1819
- [Entropy Bits](#EntropyBits)
1920
- [Why You Don't Need UUIDs](#UUID)
2021
- [Upgrading](#Upgrading)
@@ -481,9 +482,9 @@ Note the number of bytes needed is dependent on the number of characters in our
481482

482483
[TOC](#TOC)
483484

484-
### <a name="NoCrypto"></a>No crypto
485+
### <a name="NoCrypto"></a>No Crypto
485486

486-
`EntropyString` uses the `crypto` library by default when generating the random bits used to systematically index into the chosen character set. In environments where the `crypto` library is not available, `EntropyString` can use the psuedo-random number generator `Math.random` by passing `prng: true` to the `Entropy` constructor:
487+
By default, `EntropyString` uses the `crypto` library for the cryptographically strong random bits used to systematically index into the chosen character set. If cryptographically strong strings are not required, `EntropyString` can use the psuedo-random number generator `Math.random` by passing `prng: true` to the `Entropy` constructor:
487488

488489
```js
489490
const { Entropy } = require('entropy-string')
@@ -496,6 +497,12 @@ const string = entropy.string()
496497
497498
[TOC](#TOC)
498499

500+
### <a name="BrowserVersion"></a>Browser Version
501+
502+
A browser version of `EntropyString` is packaged as a UMD bundle in the file `entropy-string.browser.js` with an export name of __EntropyString__ . Rather than use the `crypto` library, the browser version uses `window.crypto.getRandomValues` for generating random bits. See `examples/browser.html` for example usage.
503+
504+
[TOC](#TOC)
505+
499506
### <a name="EntropyBits"></a>Entropy Bits
500507

501508
Thus far we've avoided the mathematics behind the calculation of the entropy bits required to specify a risk that some number random strings will not have a repeat. As noted in the [Overview](#Overview), the posting [Hash Collision Probabilities](http://preshing.com/20110504/hash-collision-probabilities/) derives an expression, based on the well-known [Birthday Problem](https://en.wikipedia.org/wiki/Birthday_problem#Approximations), for calculating the probability of a collision in some number of hashes (denoted by `k`) using a perfect hash with an output of `M` bits:

‎dist/entropy-string.js‎

Lines changed: 30 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ var _log2 = _interopRequireDefault(_log);
1414

1515
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1616

17-
var WeakMap = require('weak-map');
18-
1917
var _require = require('./lib/csprng-bytes'),
2018
csprngBytes = _require.csprngBytes;
2119

@@ -81,8 +79,6 @@ var genNdxFn = function genNdxFn(bitsPerChar) {
8179
};
8280
};
8381

84-
var charsetProps = new WeakMap();
85-
8682
var CharSet = function () {
8783
function CharSet(chars) {
8884
(0, _classCallCheck3.default)(this, CharSet);
@@ -96,8 +92,6 @@ var CharSet = function () {
9692
throw new Error('Invalid char count: must be one of 2,4,8,16,32,64');
9793
}
9894

99-
this.bitsPerChar = floor(log2(length));
100-
10195
// Ensure no repeated characters
10296
for (var i = 0; i < length; i += 1) {
10397
var c = chars.charAt(i);
@@ -107,61 +101,20 @@ var CharSet = function () {
107101
}
108102
}
109103
}
110-
var privProps = {
111-
chars: chars,
112-
length: length,
113-
ndxFn: genNdxFn(this.bitsPerChar),
114-
charsPerChunk: lcm(this.bitsPerChar, BITS_PER_BYTE) / this.bitsPerChar
115-
};
116-
charsetProps.set(this, privProps);
104+
105+
this.chars = chars;
106+
this.bitsPerChar = floor(log2(length));
107+
this.length = length;
108+
this.ndxFn = genNdxFn(this.bitsPerChar);
109+
this.charsPerChunk = lcm(this.bitsPerChar, BITS_PER_BYTE) / this.bitsPerChar;
117110
}
118111

119112
(0, _createClass3.default)(CharSet, [{
120-
key: 'getChars',
121-
value: function getChars() {
122-
return charsetProps.get(this).chars;
123-
}
124-
}, {
125-
key: 'getBitsPerChar',
126-
value: function getBitsPerChar() {
127-
return this.bitsPerChar;
128-
}
129-
}, {
130-
key: 'getNdxFn',
131-
value: function getNdxFn() {
132-
return charsetProps.get(this).ndxFn;
133-
}
134-
}, {
135-
key: 'getCharsPerChunk',
136-
value: function getCharsPerChunk() {
137-
return charsetProps.get(this).charsPerChunk;
138-
}
139-
}, {
140-
key: 'length',
141-
value: function length() {
142-
return charsetProps.get(this).length;
143-
}
144-
}, {
145113
key: 'bytesNeeded',
146114
value: function bytesNeeded(bitLen) {
147115
var count = ceil(bitLen / this.bitsPerChar);
148116
return ceil(count * this.bitsPerChar / BITS_PER_BYTE);
149117
}
150-
151-
// Aliases
152-
153-
}, {
154-
key: 'chars',
155-
value: function chars() {
156-
return this.getChars();
157-
}
158-
}, {
159-
key: 'ndxFn',
160-
value: function ndxFn() {
161-
return this.getNdxFn();
162-
}
163-
// bitsPerChar() { return this.getBitsPerChar() }
164-
165118
}]);
166119
return CharSet;
167120
}();
@@ -179,6 +132,7 @@ var _stringWithBytes = function _stringWithBytes(bytes, bitLen, charset) {
179132
}
180133

181134
var bitsPerChar = charset.bitsPerChar;
135+
182136
var count = ceil(bitLen / bitsPerChar);
183137
if (count <= 0) {
184138
return '';
@@ -189,13 +143,14 @@ var _stringWithBytes = function _stringWithBytes(bytes, bitLen, charset) {
189143
throw new Error('Insufficient bytes: need ' + need + ' and got ' + bytes.length);
190144
}
191145

192-
var charsPerChunk = charset.getCharsPerChunk();
146+
var ndxFn = charset.ndxFn,
147+
charsPerChunk = charset.charsPerChunk,
148+
chars = charset.chars;
149+
150+
193151
var chunks = floor(count / charsPerChunk);
194152
var partials = count % charsPerChunk;
195153

196-
var ndxFn = charset.getNdxFn();
197-
var chars = charset.getChars();
198-
199154
var string = '';
200155
for (var chunk = 0; chunk < chunks; chunk += 1) {
201156
for (var slice = 0; slice < charsPerChunk; slice += 1) {
@@ -223,8 +178,6 @@ var entropyBits = function entropyBits(total, risk) {
223178
return N + log2(risk) - 1;
224179
};
225180

226-
var entropyProps = new WeakMap();
227-
228181
var Entropy = function () {
229182
function Entropy() {
230183
var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { bits: 128, charset: charset32 };
@@ -299,89 +252,89 @@ var Entropy = function () {
299252
charset = charset32;
300253
}
301254

302-
varprng=params.prng||false;
303-
304-
entropyProps.set(this,{charset: charset,bitLen: bitLen,prng: prng});
255+
this.charset=charset;
256+
this.bitLen=bitLen;
257+
this.prng=params.prng||false;
305258
}
306259

307260
(0, _createClass3.default)(Entropy, [{
308261
key: 'smallID',
309262
value: function smallID() {
310-
var charset = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : entropyProps.get(this).charset;
263+
var charset = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.charset;
311264

312265
return this.string(29, charset);
313266
}
314267
}, {
315268
key: 'mediumID',
316269
value: function mediumID() {
317-
var charset = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : entropyProps.get(this).charset;
270+
var charset = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.charset;
318271

319272
return this.string(69, charset);
320273
}
321274
}, {
322275
key: 'largeID',
323276
value: function largeID() {
324-
var charset = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : entropyProps.get(this).charset;
277+
var charset = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.charset;
325278

326279
return this.string(99, charset);
327280
}
328281
}, {
329282
key: 'sessionID',
330283
value: function sessionID() {
331-
var charset = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : entropyProps.get(this).charset;
284+
var charset = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.charset;
332285

333286
return this.string(128, charset);
334287
}
335288
}, {
336289
key: 'token',
337290
value: function token() {
338-
var charset = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : entropyProps.get(this).charset;
291+
var charset = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.charset;
339292

340293
return this.string(256, charset);
341294
}
342295
}, {
343296
key: 'string',
344297
value: function string() {
345-
var bitLen = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : entropyProps.get(this).bitLen;
346-
var charset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : entropyProps.get(this).charset;
298+
var bitLen = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.bitLen;
299+
var charset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.charset;
347300

348301
var bytesNeeded = charset.bytesNeeded(bitLen);
349-
var bytes = entropyProps.get(this).prng ? prngBytes(bytesNeeded) : csprngBytes(bytesNeeded);
302+
var bytes = this.prng ? prngBytes(bytesNeeded) : csprngBytes(bytesNeeded);
350303
return this.stringWithBytes(bytes, bitLen, charset);
351304
}
352305
}, {
353306
key: 'stringWithBytes',
354307
value: function stringWithBytes(bytes) {
355-
var bitLen = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : entropyProps.get(this).bitLen;
356-
var charset = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : entropyProps.get(this).charset;
308+
var bitLen = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.bitLen;
309+
var charset = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : this.charset;
357310

358311
return _stringWithBytes(bytes, bitLen, charset);
359312
}
360313
}, {
361314
key: 'bytesNeeded',
362315
value: function bytesNeeded() {
363-
var bitLen = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : entropyProps.get(this).bitLen;
364-
var charset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : entropyProps.get(this).charset;
316+
var bitLen = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.bitLen;
317+
var charset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.charset;
365318

366319
return charset.bytesNeeded(bitLen);
367320
}
368321
}, {
369322
key: 'chars',
370323
value: function chars() {
371-
return entropyProps.get(this).charset.chars();
324+
return this.charset.chars;
372325
}
373326
}, {
374327
key: 'bits',
375328
value: function bits() {
376-
return entropyProps.get(this).bitLen;
329+
return this.bitLen;
377330
}
378331
}, {
379332
key: 'use',
380333
value: function use(charset) {
381334
if (!(charset instanceof CharSet)) {
382335
throw new Error('Invalid CharSet');
383336
}
384-
entropyProps.get(this).charset = charset;
337+
this.charset = charset;
385338
}
386339
}, {
387340
key: 'useChars',

‎examples/browser.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<script src="../entropy-string.browser.js" type="text/javascript"></script>
55
<script>
66
function global() {
7-
entropy = new EntropyString.Entropy({total: 1e5, risk: 1e9});
7+
entropy = new EntropyString.Entropy({total: 1e5, risk: 1e9});
88
}
99
window.onload = global;
1010
</script>

‎examples/charsets.js‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
// Predefined character sets
2-
32
const {
43
charset64, charset32, charset16, charset8, charset4, charset2
54
} = require('./entropy-string')
65

7-
console.log(`\n charset64: ${charset64.chars()}`)
8-
console.log(`\n charset32: ${charset32.chars()}`)
9-
console.log(`\n charset16: ${charset16.chars()}`)
10-
console.log(`\n charset8: ${charset8.chars()}`)
11-
console.log(`\n charset4: ${charset4.chars()}`)
12-
console.log(`\n charset2: ${charset2.chars()}\n`)
6+
console.log(`\n charset64: ${charset64.chars}`)
7+
console.log(`\n charset32: ${charset32.chars}`)
8+
console.log(`\n charset16: ${charset16.chars}`)
9+
console.log(`\n charset8: ${charset8.chars}`)
10+
console.log(`\n charset4: ${charset4.chars}`)
11+
console.log(`\n charset2: ${charset2.chars}\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": "4.1.0",
3+
"version": "4.0.4",
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)

AltStyle によって変換されたページ (->オリジナル) /