Skip to content

Commit 4207e84

Browse files
committed
Refactor
charSet -> charset
1 parent 2135d4e commit 4207e84

File tree

15 files changed

+323
-239
lines changed

15 files changed

+323
-239
lines changed

dist/lib/entropy.js

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,50 +10,23 @@ var _log2 = _interopRequireDefault(_log);
1010

1111
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1212

13-
var log2 = _log2.default;
14-
15-
var LOG2_OF_10 = log2(10);
16-
17-
var totalOf = function totalOf(numStrings, log2Risk) {
18-
if (numStrings === 0) {
19-
return 0;
20-
}
21-
22-
var N = void 0;
23-
if (numStrings < 1000) {
24-
N = log2(numStrings) + log2(numStrings - 1);
25-
} else {
26-
N = 2 * log2(numStrings);
27-
}
28-
return N + log2Risk - 1;
29-
};
30-
3113
var bits = function bits(total, risk) {
3214
if (total === 0) {
3315
return 0;
3416
}
35-
return totalOf(total, log2(risk));
36-
};
3717

38-
// CxTBD Mark as obsolete
39-
var bitsWithRiskPower = function bitsWithRiskPower(total, rPower) {
40-
var log2Risk = LOG2_OF_10 * rPower;
41-
return totalOf(total, log2Risk);
42-
};
18+
var log2 = _log2.default;
19+
4320

44-
// CxTBD Mark as obsolete
45-
var bitsWithPowers = function bitsWithPowers(tPower, rPower) {
46-
var nBits = void 0;
47-
if (tPower < 4) {
48-
nBits = bitsWithRiskPower(10 ** tPower, rPower);
21+
var N = void 0;
22+
if (total < 1000) {
23+
N = log2(total) + log2(total - 1);
4924
} else {
50-
nBits = (2 * tPower + rPower) * LOG2_OF_10 - 1;
25+
N = 2 * log2(total);
5126
}
52-
return nBits;
27+
return N + log2(risk) - 1;
5328
};
5429

5530
exports.default = {
56-
bits: bits,
57-
bitsWithRiskPower: bitsWithRiskPower,
58-
bitsWithPowers: bitsWithPowers
31+
bits: bits
5932
};

dist/lib/random.js

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ var _createClass2 = require('babel-runtime/helpers/createClass');
1212

1313
var _createClass3 = _interopRequireDefault(_createClass2);
1414

15-
var _charSet = require('./charSet');
15+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1616

17-
var _charSet2 = _interopRequireDefault(_charSet);
17+
var CharSet = require('./charset').default;
1818

19-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19+
var _require = require('./charset'),
20+
charset32 = _require.charset32;
2021

2122
var Crypto = require('crypto');
2223
var WeakMap = require('weak-map');
@@ -32,12 +33,12 @@ var endianByteNum = function () {
3233
return buf8[0] === 0xff ? [2, 3, 4, 5, 6, 7] : [0, 1, 2, 3, 6, 7];
3334
}();
3435

35-
var _stringWithBytes = function _stringWithBytes(entropyBits, bytes, charSet) {
36+
var _stringWithBytes = function _stringWithBytes(entropyBits, bytes, charset) {
3637
if (entropyBits <= 0) {
3738
return '';
3839
}
3940

40-
var bitsPerChar = charSet.getBitsPerChar();
41+
var bitsPerChar = charset.getBitsPerChar();
4142
var count = Math.ceil(entropyBits / bitsPerChar);
4243
if (count <= 0) {
4344
return '';
@@ -48,12 +49,12 @@ var _stringWithBytes = function _stringWithBytes(entropyBits, bytes, charSet) {
4849
throw new Error('Insufficient bytes: need ' + need + ' and got ' + bytes.length);
4950
}
5051

51-
var charsPerChunk = charSet.getCharsPerChunk();
52+
var charsPerChunk = charset.getCharsPerChunk();
5253
var chunks = Math.floor(count / charsPerChunk);
5354
var partials = count % charsPerChunk;
5455

55-
var ndxFn = charSet.getNdxFn();
56-
var chars = charSet.getChars();
56+
var ndxFn = charset.getNdxFn();
57+
var chars = charset.getChars();
5758

5859
var string = '';
5960
for (var chunk = 0; chunk < chunks; chunk += 1) {
@@ -96,107 +97,107 @@ var _class = function () {
9697
function _class(arg) {
9798
(0, _classCallCheck3.default)(this, _class);
9899

99-
var charSet = void 0;
100+
var charset = void 0;
100101
if (arg === undefined) {
101-
charSet = _charSet.charSet32;
102-
} else if (arg instanceof _charSet2.default) {
103-
charSet = arg;
102+
charset = charset32;
103+
} else if (arg instanceof CharSet) {
104+
charset = arg;
104105
} else if (typeof arg === 'string' || arg instanceof String) {
105-
charSet = new _charSet2.default(arg);
106+
charset = new CharSet(arg);
106107
} else {
107108
throw new Error('Invalid arg: must be either valid CharSet or valid chars');
108109
}
109110
var hideProps = {
110-
charSet: charSet
111+
charset: charset
111112
};
112113
propMap.set(this, hideProps);
113114
}
114115

115116
(0, _createClass3.default)(_class, [{
116117
key: 'smallID',
117118
value: function smallID() {
118-
var charSet = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : propMap.get(this).charSet;
119+
var charset = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : propMap.get(this).charset;
119120

120-
return this.string(29, charSet);
121+
return this.string(29, charset);
121122
}
122123
}, {
123124
key: 'mediumID',
124125
value: function mediumID() {
125-
var charSet = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : propMap.get(this).charSet;
126+
var charset = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : propMap.get(this).charset;
126127

127-
return this.string(69, charSet);
128+
return this.string(69, charset);
128129
}
129130
}, {
130131
key: 'largeID',
131132
value: function largeID() {
132-
var charSet = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : propMap.get(this).charSet;
133+
var charset = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : propMap.get(this).charset;
133134

134-
return this.string(99, charSet);
135+
return this.string(99, charset);
135136
}
136137
}, {
137138
key: 'sessionID',
138139
value: function sessionID() {
139-
var charSet = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : propMap.get(this).charSet;
140+
var charset = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : propMap.get(this).charset;
140141

141-
return this.string(128, charSet);
142+
return this.string(128, charset);
142143
}
143144
}, {
144145
key: 'token',
145146
value: function token() {
146-
var charSet = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : propMap.get(this).charSet;
147+
var charset = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : propMap.get(this).charset;
147148

148-
return this.string(256, charSet);
149+
return this.string(256, charset);
149150
}
150151
}, {
151152
key: 'string',
152153
value: function string(entropyBits) {
153-
var charSet = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : propMap.get(this).charSet;
154+
var charset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : propMap.get(this).charset;
154155

155-
var bytesNeeded = charSet.bytesNeeded(entropyBits);
156-
return this.stringWithBytes(entropyBits, cryptoBytes(bytesNeeded), charSet);
156+
var bytesNeeded = charset.bytesNeeded(entropyBits);
157+
return this.stringWithBytes(entropyBits, cryptoBytes(bytesNeeded), charset);
157158
}
158159
}, {
159160
key: 'stringRandom',
160161
value: function stringRandom(entropyBits) {
161-
var charSet = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : propMap.get(this).charSet;
162+
var charset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : propMap.get(this).charset;
162163

163-
var bytesNeeded = charSet.bytesNeeded(entropyBits);
164-
return this.stringWithBytes(entropyBits, randomBytes(bytesNeeded), charSet);
164+
var bytesNeeded = charset.bytesNeeded(entropyBits);
165+
return this.stringWithBytes(entropyBits, randomBytes(bytesNeeded), charset);
165166
}
166167
}, {
167168
key: 'stringWithBytes',
168169
value: function stringWithBytes(entropyBits, bytes) {
169-
var charSet = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : propMap.get(this).charSet;
170+
var charset = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : propMap.get(this).charset;
170171

171-
return _stringWithBytes(entropyBits, bytes, charSet);
172+
return _stringWithBytes(entropyBits, bytes, charset);
172173
}
173174
}, {
174175
key: 'bytesNeeded',
175176
value: function bytesNeeded(entropyBits) {
176-
var charSet = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : propMap.get(this).charSet;
177+
var charset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : propMap.get(this).charset;
177178

178-
return charSet.bytesNeeded(entropyBits);
179+
return charset.bytesNeeded(entropyBits);
179180
}
180181
}, {
181182
key: 'chars',
182183
value: function chars() {
183-
return propMap.get(this).charSet.chars();
184+
return propMap.get(this).charset.chars();
184185
}
185186
}, {
186187
key: 'use',
187-
value: function use(charSet) {
188-
if (!(charSet instanceof _charSet2.default)) {
188+
value: function use(charset) {
189+
if (!(charset instanceof CharSet)) {
189190
throw new Error('Invalid CharSet');
190191
}
191-
propMap.get(this).charSet = charSet;
192+
propMap.get(this).charset = charset;
192193
}
193194
}, {
194195
key: 'useChars',
195196
value: function useChars(chars) {
196197
if (!(typeof chars === 'string' || chars instanceof String)) {
197198
throw new Error('Invalid chars: Must be string');
198199
}
199-
this.use(new _charSet2.default(chars));
200+
this.use(new CharSet(chars));
200201
}
201202
}]);
202203
return _class;

entropy-string.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
const Random = require('./dist/lib/random').default
22
const Entropy = require('./dist/lib/entropy').default
3-
const CharSet = require('./dist/lib/charSet').default
3+
const CharSet = require('./dist/lib/charset').default
44
const {
5-
charSet2, charSet4, charSet8, charSet16, charSet32, charSet64
6-
} = require('./dist/lib/charSet')
5+
charset2, charset4, charset8, charset16, charset32, charset64
6+
} = require('./dist/lib/charset')
77

88
module.exports = {
99
Random,
1010
Entropy,
1111
CharSet,
12-
charSet2,
13-
charSet4,
14-
charSet8,
15-
charSet16,
16-
charSet32,
17-
charSet64
12+
charset2,
13+
charset4,
14+
charset8,
15+
charset16,
16+
charset32,
17+
charset64
1818
}

examples/charSet.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/charSet.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
const WeakMap = require('weak-map')
2+
3+
const lcm = require('./lcm').default
4+
5+
const propMap = new WeakMap()
6+
7+
const BITS_PER_BYTE = 8
8+
9+
const genNdxFn = (bitsPerChar) => {
10+
// If BITS_PER_BYTEs is a multiple of bitsPerChar, we can slice off an integer number
11+
// of chars per byte.
12+
if (lcm(bitsPerChar, BITS_PER_BYTE) === BITS_PER_BYTE) {
13+
return (chunk, slice, bytes) => {
14+
const lShift = bitsPerChar
15+
const rShift = BITS_PER_BYTE - bitsPerChar
16+
return ((bytes[chunk] << (lShift * slice)) & 0xff) >> rShift
17+
}
18+
}
19+
// Otherwise, while slicing off bits per char, we will possibly straddle a couple
20+
// of bytes, so a bit more work is involved
21+
22+
const slicesPerChunk = lcm(bitsPerChar, BITS_PER_BYTE) / BITS_PER_BYTE
23+
return (chunk, slice, bytes) => {
24+
const bNum = chunk * slicesPerChunk
25+
26+
const offset = (slice * bitsPerChar) / BITS_PER_BYTE
27+
const lOffset = Math.floor(offset)
28+
const rOffset = Math.ceil(offset)
29+
30+
const rShift = BITS_PER_BYTE - bitsPerChar
31+
const lShift = (slice * bitsPerChar) % BITS_PER_BYTE
32+
33+
let ndx = ((bytes[bNum + lOffset] << lShift) & 0xff) >> rShift
34+
35+
const r1Bits = (rOffset + 1) * BITS_PER_BYTE
36+
const s1Bits = (slice + 1) * bitsPerChar
37+
38+
const rShiftIt = (r1Bits - s1Bits) % BITS_PER_BYTE
39+
if (rShift < rShiftIt) {
40+
ndx += bytes[bNum + rOffset] >> rShiftIt
41+
}
42+
return ndx
43+
}
44+
}
45+
46+
export default class CharSet {
47+
constructor(chars) {
48+
if (!(typeof chars === 'string' || chars instanceof String)) {
49+
throw new Error('Invalid chars: Must be string')
50+
}
51+
const { length } = chars
52+
if (![2, 4, 8, 16, 32, 64].includes(length)) {
53+
throw new Error('Invalid char count: must be one of 2,4,8,16,32,64')
54+
}
55+
const bitsPerChar = Math.floor(Math.log2(length))
56+
// Ensure no repeated characters
57+
for (let i = 0; i < length; i += 1) {
58+
const c = chars.charAt(i)
59+
for (let j = i + 1; j < length; j += 1) {
60+
if (c === chars.charAt(j)) {
61+
throw new Error('Characters not unique')
62+
}
63+
}
64+
}
65+
const privProps = {
66+
chars,
67+
bitsPerChar,
68+
length,
69+
ndxFn: genNdxFn(bitsPerChar),
70+
charsPerChunk: lcm(bitsPerChar, BITS_PER_BYTE) / bitsPerChar
71+
}
72+
propMap.set(this, privProps)
73+
}
74+
75+
getChars() {
76+
return propMap.get(this).chars
77+
}
78+
79+
getBitsPerChar() {
80+
return propMap.get(this).bitsPerChar
81+
}
82+
83+
getNdxFn() {
84+
return propMap.get(this).ndxFn
85+
}
86+
87+
getCharsPerChunk() {
88+
return propMap.get(this).charsPerChunk
89+
}
90+
91+
length() {
92+
return propMap.get(this).length
93+
}
94+
95+
bytesNeeded(entropyBits) {
96+
const count = Math.ceil(entropyBits / this.bitsPerChar())
97+
return Math.ceil((count * this.bitsPerChar()) / BITS_PER_BYTE)
98+
}
99+
100+
// Aliases
101+
chars() { return this.getChars() }
102+
ndxFn() { return this.getNdxFn() }
103+
bitsPerChar() { return this.getBitsPerChar() }
104+
}
105+
106+
export const charset64 = new CharSet('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_')
107+
export const charset32 = new CharSet('2346789bdfghjmnpqrtBDFGHJLMNPQRT')
108+
export const charset16 = new CharSet('0123456789abcdef')
109+
export const charset8 = new CharSet('01234567')
110+
export const charset4 = new CharSet('ATCG')
111+
export const charset2 = new CharSet('01')

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