Skip to content

Commit 565431c

Browse files
committed
Update README and examples
1 parent 2240349 commit 565431c

19 files changed

+296
-165
lines changed

README.md

Lines changed: 181 additions & 105 deletions
Large diffs are not rendered by default.

dist/lib/entropy.js

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ var createCharset = function createCharset(arg) {
129129

130130
var _class = function () {
131131
function _class() {
132-
var arg = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { bits: 128, charset: CharSet.charset32 };
132+
var arg = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { bits: 128, charset: charset32 };
133133
(0, _classCallCheck3.default)(this, _class);
134134

135135
var charset = void 0;
@@ -138,12 +138,14 @@ var _class = function () {
138138
if (arg instanceof CharSet || arg instanceof String || typeof arg === 'string') {
139139
charset = createCharset(arg);
140140
} else if (arg instanceof Object) {
141+
var round = Math.round;
142+
141143
if (typeof arg.bits === 'number') {
142-
bitLen = arg.bits;
144+
bitLen = round(arg.bits);
143145
} else if (typeof arg.total === 'number' && typeof arg.risk === 'number') {
144-
bitLen = entropyBits(arg.total, arg.risk);
146+
bitLen = round(entropyBits(arg.total, arg.risk));
145147
} else {
146-
throw new Error('Entropy params must include either bits or both total and risk');
148+
bitLen = 128;
147149
}
148150
charset = createCharset(arg.charset);
149151
} else {
@@ -152,16 +154,15 @@ var _class = function () {
152154

153155
if (charset === undefined) {
154156
throw new Error('Invalid constructor CharSet declaration');
155-
} else if (bits < 0) {
157+
} else if (bitLen < 0) {
156158
throw new Error('Invalid constructor declaration of bits less than zero');
157159
}
158160

159-
var hideProps = {
161+
propMap.set(this, {
160162
charset: charset,
161163
bitLen: bitLen,
162164
bytesNeeded: charset.bytesNeeded(bitLen)
163-
};
164-
propMap.set(this, hideProps);
165+
});
165166
}
166167

167168
(0, _createClass3.default)(_class, [{
@@ -202,28 +203,34 @@ var _class = function () {
202203
}, {
203204
key: 'string',
204205
value: function string() {
205-
var bitLen = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined;
206+
var bitLen = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : propMap.get(this).bitLen;
206207
var charset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : propMap.get(this).charset;
207208

208-
if (bitLen === undefined) {
209-
var _propMap$get = propMap.get(this),
210-
classCharset = _propMap$get.charset,
211-
classBitLen = _propMap$get.bitLen,
212-
_bytesNeeded = _propMap$get.bytesNeeded;
213-
214-
return this.stringWithBytes(classBitLen, cryptoBytes(_bytesNeeded), classCharset);
215-
}
216209
var bytesNeeded = charset.bytesNeeded(bitLen);
217210
return this.stringWithBytes(bitLen, cryptoBytes(bytesNeeded), charset);
218211
}
219212
}, {
220-
key: 'stringRandom',
221-
value: function stringRandom(bitLen) {
213+
key: 'stringPRNG',
214+
value: function stringPRNG() {
215+
var bitLen = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : propMap.get(this).bitLen;
222216
var charset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : propMap.get(this).charset;
223217

224218
var bytesNeeded = charset.bytesNeeded(bitLen);
225219
return this.stringWithBytes(bitLen, randomBytes(bytesNeeded), charset);
226220
}
221+
222+
/**
223+
* @deprecated Since version 3.1. Will be deleted in version 4.0. Use stringPRNG instead.
224+
*/
225+
226+
}, {
227+
key: 'stringRandom',
228+
value: function stringRandom() {
229+
var bitLen = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : propMap.get(this).bitLen;
230+
var charset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : propMap.get(this).charset;
231+
232+
return this.stringPRNG(bitLen, charset);
233+
}
227234
}, {
228235
key: 'stringWithBytes',
229236
value: function stringWithBytes(bitLen, bytes) {
@@ -233,7 +240,8 @@ var _class = function () {
233240
}
234241
}, {
235242
key: 'bytesNeeded',
236-
value: function bytesNeeded(bitLen) {
243+
value: function bytesNeeded() {
244+
var bitLen = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : propMap.get(this).bitLen;
237245
var charset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : propMap.get(this).charset;
238246

239247
return charset.bytesNeeded(bitLen);

examples/custom_chars_1.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
const { default: Entropy, charset2 } = require('./entropy')
44

5-
const entropy = new Entropy(charset2)
6-
let flips = entropy.string(10)
5+
const entropy = new Entropy({ charset: charset2, bits: 10 })
6+
let flips = entropy.string()
77
console.log(`\n 10 flips: ${flips}`)
88

99
entropy.useChars('HT')
10-
flips = entropy.string(10)
10+
flips = entropy.string()
1111
console.log(`\n 10 flips: ${flips}\n`)

examples/custom_chars_2.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
const { default: Entropy } = require('./entropy')
44

5-
const entropy = new Entropy('0123456789ABCDEF')
6-
const string = entropy.string(48)
5+
const entropy = new Entropy({ charset: '0123456789ABCDEF', bits: 48 })
6+
const string = entropy.string()
77
console.log(`\n Uppercase hex: ${string}\n`)

examples/gen5.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
const { default: Entropy, charset16 } = require('./entropy')
22

3-
const bits = Entropy.bits(10000, 1000000)
4-
const entropy = new Entropy(charset16)
5-
const strings = []
6-
for (let i = 0; i < 5; i += 1) {
7-
const string = entropy.string(bits)
8-
strings.push(string)
9-
}
3+
const entropy = new Entropy({ total: 10000,
4+
risk: 1000000,
5+
charset: charset16 })
6+
7+
const strings = Array(5).fill('').map(e => entropy.string())
108
console.log(`\n 5 IDs: ${strings.join(', ')}\n`)

examples/more_1.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
const { default: Entropy } = require('./entropy')
44

5-
const bits = Entropy.bits(10000, 1000000)
6-
const entropy = new Entropy()
7-
const string = entropy.string(bits)
5+
const entropy = new Entropy({ total: 10000, risk: 1e6 })
6+
const string = entropy.string()
87

98
console.log(`\n Ten thousand potential strings with 1 in a million risk of repeat: ${string}\n`)
10-

examples/more_2.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ const {
77

88
console.log('\n 30 potential strings with 1 in a million risk of repeat: \n')
99

10-
const entropy = new Entropy(charset16)
11-
const bits = Entropy.bits(30, 100000)
12-
let string = entropy.string(bits)
10+
const entropy = new Entropy({ total: 30,
11+
risk: 100000,
12+
charset: charset16 })
13+
let string = entropy.string()
1314
console.log(` Base 16: ${string}\n`)
1415

1516
entropy.use(charset4)
16-
string = entropy.string(bits)
17+
string = entropy.string()
1718
console.log(` Base 4 : ${string}\n`)

examples/more_3.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
const { default: Entropy } = require('./entropy')
44

5-
const bits = Entropy.bits(1e10, 1e12)
6-
const entropy = new Entropy()
7-
const string = entropy.string(bits)
5+
const entropy = new Entropy({ total: 1e10, risk: 1e12 })
6+
const string = entropy.string()
87

98
console.log(`\n Ten billion potential strings with 1 in a trillion risk of repeat: ${string}\n`)

examples/more_4.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// 128 bit
2+
3+
const { default: Entropy } = require('./entropy')
4+
5+
const entropy = new Entropy({ bits: 128 })
6+
const string = entropy.string()
7+
8+
console.log(`\n 128-bit entropy string: ${string}\n`)

examples/more_5.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// 128 bit
2+
3+
const { default: Entropy, charset64 } = require('./entropy')
4+
5+
const entropy = new Entropy({ charset: charset64 })
6+
const string = entropy.sessionID()
7+
8+
console.log(`\n 128-bit entropy session ID: ${string}\n`)

0 commit comments

Comments
 (0)