Skip to content

Commit 56d3a99

Browse files
committed
Restructure
Move Random to Entropy
1 parent 0eeec74 commit 56d3a99

27 files changed

+541
-556
lines changed

dist/lib/charSet.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
Object.defineProperty(exports, "__esModule", {
44
value: true
55
});
6-
exports.charSet2 = exports.charSet4 = exports.charSet8 = exports.charSet16 = exports.charSet32 = exports.charSet64 = undefined;
76

87
var _log = require('babel-runtime/core-js/math/log2');
98

@@ -149,10 +148,4 @@ var CharSet = function () {
149148
return CharSet;
150149
}();
151150

152-
exports.default = CharSet;
153-
var charSet64 = exports.charSet64 = new CharSet('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_');
154-
var charSet32 = exports.charSet32 = new CharSet('2346789bdfghjmnpqrtBDFGHJLMNPQRT');
155-
var charSet16 = exports.charSet16 = new CharSet('0123456789abcdef');
156-
var charSet8 = exports.charSet8 = new CharSet('01234567');
157-
var charSet4 = exports.charSet4 = new CharSet('ATCG');
158-
var charSet2 = exports.charSet2 = new CharSet('01');
151+
exports.default = CharSet;

dist/lib/entropy.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
"use strict";
1+
'use strict';
22

33
Object.defineProperty(exports, "__esModule", {
44
value: true
55
});
6+
exports.charset2 = exports.charset4 = exports.charset8 = exports.charset16 = exports.charset32 = exports.charset64 = exports.entropyBits = undefined;
67

7-
var _log = require("babel-runtime/core-js/math/log2");
8+
var _log = require('babel-runtime/core-js/math/log2');
89

910
var _log2 = _interopRequireDefault(_log);
1011

1112
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1213

13-
var bits = function bits(total, risk) {
14+
var CharSet = require('./charset').default;
15+
16+
var entropyBits = exports.entropyBits = function entropyBits(total, risk) {
1417
if (total === 0) {
1518
return 0;
1619
}
@@ -27,6 +30,9 @@ var bits = function bits(total, risk) {
2730
return N + log2(risk) - 1;
2831
};
2932

30-
exports.default = {
31-
bits: bits
32-
};
33+
var charset64 = exports.charset64 = new CharSet('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_');
34+
var charset32 = exports.charset32 = new CharSet('2346789bdfghjmnpqrtBDFGHJLMNPQRT');
35+
var charset16 = exports.charset16 = new CharSet('0123456789abcdef');
36+
var charset8 = exports.charset8 = new CharSet('01234567');
37+
var charset4 = exports.charset4 = new CharSet('ATCG');
38+
var charset2 = exports.charset2 = new CharSet('01');

dist/lib/random.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
1616

1717
var CharSet = require('./charset').default;
1818

19-
var _require = require('./charset'),
19+
var _require = require('./entropy'),
2020
charset32 = _require.charset32;
2121

2222
var Crypto = require('crypto');

entropy-string.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
const Random = require('./dist/lib/random').default
2-
const CharSet = require('./dist/lib/charset').default
1+
const { default: CharSet } = require('./dist/lib/charset')
32
const {
3+
default: Entropy,
44
charset2, charset4, charset8, charset16, charset32, charset64
55
} = require('./dist/lib/entropy')
66

77
module.exports = {
8-
Random,
8+
Entropy,
99
CharSet,
1010
charset2,
1111
charset4,

examples/custom_bytes.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// Custom bytes
22

3-
const { Random } = require('./entropy-string')
3+
const { default: Entropy } = require('./entropy')
44

5-
const random = new Random()
5+
const entropy = new Entropy()
66
const bytes = Buffer.from([250, 200, 150, 100])
7-
let string = random.stringWithBytes(30, bytes)
7+
let string = entropy.stringWithBytes(30, bytes)
88
console.log(`\n Custom bytes string : ${string}\n`)
99

1010
try {
11-
string = random.stringWithBytes(32, bytes)
11+
string = entropy.stringWithBytes(32, bytes)
1212
} catch (error) {
1313
console.log(` Error: ${error.message}\n`)
1414
}

examples/custom_chars_1.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
// Custom characters: HT for coin flip
22

3-
const { Random } = require('./entropy-string')
4-
const { charset2 } = require('./entropy')
3+
const { default: Entropy, charset2 } = require('./entropy')
54

6-
const random = new Random(charset2)
7-
let flips = random.string(10)
5+
const entropy = new Entropy(charset2)
6+
let flips = entropy.string(10)
87
console.log(`\n 10 flips: ${flips}`)
98

10-
random.useChars('HT')
11-
flips = random.string(10)
9+
entropy.useChars('HT')
10+
flips = entropy.string(10)
1211
console.log(`\n 10 flips: ${flips}\n`)

examples/custom_chars_2.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Custom characters: Uppercase hex
22

3-
const { Random } = require('./entropy-string')
3+
const { default: Entropy } = require('./entropy')
44

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

examples/custom_chars_3.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
// Custom character errors
22

3-
const { Random } = require('./entropy-string')
3+
const { default: Entropy } = require('./entropy')
44

55
try {
6-
const random = new Random('123456')
7-
console.error('variable \'random\' should not exist', random)
6+
const entropy = new Entropy('123456')
7+
console.error('variable \'entropy\' should not exist', entropy)
88
} catch (error) {
99
console.log(`\n Error: ${error.message}`)
1010
}
1111

1212
try {
13-
const random = new Random('01233210')
14-
console.error('variable \'random\' should not exist', random)
13+
const entropy = new Entropy('01233210')
14+
console.error('variable \'entropy\' should not exist', entropy)
1515
} catch (error) {
1616
console.log(`\n Error: ${error.message}\n`)
1717
}

examples/gen5.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
const { Random } = require('./entropy-string')
2-
const { entropyBits, charset16 } = require('./entropy')
1+
const { default: Entropy, entropyBits, charset16 } = require('./entropy')
32

4-
const random = new Random(charset16)
3+
const entropy = new Entropy(charset16)
54
const bits = entropyBits(10000, 1000000)
65
const strings = []
76
for (let i = 0; i < 5; i += 1) {
8-
const string = random.string(bits)
7+
const string = entropy.string(bits)
98
strings.push(string)
109
}
1110
console.log(`\n 5 IDs: ${strings.join(', ')}\n`)

examples/more_1.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
// Ten thousand potential strings with 1 in a million risk of repeat
22

3-
const { Random, Entropy } = require('./entropy-string')
4-
const { entropyBits } = require('./entropy')
3+
const { default: Entropy, entropyBits } = require('./entropy')
54

6-
const random = new Random()
5+
const entropy = new Entropy()
76
const bits = entropyBits(10000, 1000000)
8-
const string = random.string(bits)
7+
const string = entropy.string(bits)
98

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

0 commit comments

Comments
 (0)