Skip to content

Commit 5938449

Browse files
committed
Remove CharSet use of WeakMap
1 parent 0cf0db2 commit 5938449

File tree

6 files changed

+63
-105
lines changed

6 files changed

+63
-105
lines changed

dist/entropy-string.js

+16-37
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
1616

1717
var WeakMap = require('weak-map');
1818

19-
var csprngBytes = require('lib/csprng-bytes');
20-
var prngBytes = require('lib/prng-bytes');
19+
var _require = require('./lib/csprng-bytes'),
20+
csprngBytes = _require.csprngBytes;
21+
22+
var _require2 = require('./lib/prng-bytes'),
23+
prngBytes = _require2.prngBytes;
2124

2225
var BITS_PER_BYTE = 8;
2326
var abs = Math.abs,
@@ -92,7 +95,9 @@ var CharSet = function () {
9295
if (![2, 4, 8, 16, 32, 64].includes(length)) {
9396
throw new Error('Invalid char count: must be one of 2,4,8,16,32,64');
9497
}
95-
var bitsPerChar = floor(log2(length));
98+
99+
this.bitsPerChar = floor(log2(length));
100+
96101
// Ensure no repeated characters
97102
for (var i = 0; i < length; i += 1) {
98103
var c = chars.charAt(i);
@@ -104,10 +109,9 @@ var CharSet = function () {
104109
}
105110
var privProps = {
106111
chars: chars,
107-
bitsPerChar: bitsPerChar,
108112
length: length,
109-
ndxFn: genNdxFn(bitsPerChar),
110-
charsPerChunk: lcm(bitsPerChar, BITS_PER_BYTE) / bitsPerChar
113+
ndxFn: genNdxFn(this.bitsPerChar),
114+
charsPerChunk: lcm(this.bitsPerChar, BITS_PER_BYTE) / this.bitsPerChar
111115
};
112116
charsetProps.set(this, privProps);
113117
}
@@ -120,7 +124,7 @@ var CharSet = function () {
120124
}, {
121125
key: 'getBitsPerChar',
122126
value: function getBitsPerChar() {
123-
return charsetProps.get(this).bitsPerChar;
127+
return this.bitsPerChar;
124128
}
125129
}, {
126130
key: 'getNdxFn',
@@ -140,8 +144,8 @@ var CharSet = function () {
140144
}, {
141145
key: 'bytesNeeded',
142146
value: function bytesNeeded(bitLen) {
143-
var count = ceil(bitLen / this.bitsPerChar());
144-
return ceil(count * this.bitsPerChar() / BITS_PER_BYTE);
147+
var count = ceil(bitLen / this.bitsPerChar);
148+
return ceil(count * this.bitsPerChar / BITS_PER_BYTE);
145149
}
146150

147151
// Aliases
@@ -156,11 +160,8 @@ var CharSet = function () {
156160
value: function ndxFn() {
157161
return this.getNdxFn();
158162
}
159-
}, {
160-
key: 'bitsPerChar',
161-
value: function bitsPerChar() {
162-
return this.getBitsPerChar();
163-
}
163+
// bitsPerChar() { return this.getBitsPerChar() }
164+
164165
}]);
165166
return CharSet;
166167
}();
@@ -177,7 +178,7 @@ var _stringWithBytes = function _stringWithBytes(bytes, bitLen, charset) {
177178
return '';
178179
}
179180

180-
var bitsPerChar = charset.getBitsPerChar();
181+
var bitsPerChar = charset.bitsPerChar;
181182
var count = ceil(bitLen / bitsPerChar);
182183
if (count <= 0) {
183184
return '';
@@ -209,28 +210,6 @@ var _stringWithBytes = function _stringWithBytes(bytes, bitLen, charset) {
209210
return string;
210211
};
211212

212-
// const csprngBytes = count => Buffer.from(Crypto.randomBytes(count))
213-
214-
// const prngBytes = (count) => {
215-
// console.log('CxDebug prng: true')
216-
// const BYTES_USED_PER_RANDOM_CALL = 6
217-
// const randCount = ceil(count / BYTES_USED_PER_RANDOM_CALL)
218-
219-
// const buffer = new ArrayBuffer(count)
220-
// const dataView = new DataView(new ArrayBuffer(BITS_PER_BYTE))
221-
// for (let rNum = 0; rNum < randCount; rNum += 1) {
222-
// dataView.setFloat64(0, random())
223-
// for (let n = 0; n < BYTES_USED_PER_RANDOM_CALL; n += 1) {
224-
// const fByteNum = endianByteNum[n]
225-
// const bByteNum = (rNum * BYTES_USED_PER_RANDOM_CALL) + n
226-
// if (bByteNum < count) {
227-
// buffer[bByteNum] = dataView.getUint8(fByteNum)
228-
// }
229-
// }
230-
// }
231-
// return buffer
232-
// }
233-
234213
var entropyBits = function entropyBits(total, risk) {
235214
if (total === 0) {
236215
return 0;

dist/lib/csprng-bytes-browser.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
3+
var csprngBytes = function csprngBytes(count) {
4+
return window.crypto.getRandomValues(new Uint8Array(count));
5+
};
6+
7+
module.exports = {
8+
csprngBytes: csprngBytes
9+
};

dist/lib/prng-bytes.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
'use strict';
1+
"use strict";
22

3-
var random = Math.random;
3+
var ceil = Math.ceil,
4+
random = Math.random;
45

56

67
var BITS_PER_BYTE = 8;
@@ -13,7 +14,6 @@ var endianByteNum = function () {
1314
}();
1415

1516
var prngBytes = function prngBytes(count) {
16-
console.log('CxDebug prng: true');
1717
var BYTES_USED_PER_RANDOM_CALL = 6;
1818
var randCount = ceil(count / BYTES_USED_PER_RANDOM_CALL);
1919

entropy-string.js

+12-42
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ const genNdxFn = (bitsPerChar) => {
5555
}
5656
}
5757

58-
const charsetProps = new WeakMap()
5958
class CharSet {
6059
constructor(chars) {
6160
if (!(typeof chars === 'string' || chars instanceof String)) {
@@ -65,7 +64,7 @@ class CharSet {
6564
if (![2, 4, 8, 16, 32, 64].includes(length)) {
6665
throw new Error('Invalid char count: must be one of 2,4,8,16,32,64')
6766
}
68-
const bitsPerChar = floor(log2(length))
67+
6968
// Ensure no repeated characters
7069
for (let i = 0; i < length; i += 1) {
7170
const c = chars.charAt(i)
@@ -75,45 +74,18 @@ class CharSet {
7574
}
7675
}
7776
}
78-
const privProps = {
79-
chars,
80-
bitsPerChar,
81-
length,
82-
ndxFn: genNdxFn(bitsPerChar),
83-
charsPerChunk: lcm(bitsPerChar, BITS_PER_BYTE) / bitsPerChar
84-
}
85-
charsetProps.set(this, privProps)
86-
}
87-
88-
getChars() {
89-
return charsetProps.get(this).chars
90-
}
91-
92-
getBitsPerChar() {
93-
return charsetProps.get(this).bitsPerChar
94-
}
9577

96-
getNdxFn() {
97-
return charsetProps.get(this).ndxFn
98-
}
99-
100-
getCharsPerChunk() {
101-
return charsetProps.get(this).charsPerChunk
102-
}
103-
104-
length() {
105-
return charsetProps.get(this).length
78+
this.chars = chars
79+
this.bitsPerChar = floor(log2(length))
80+
this.length = length
81+
this.ndxFn = genNdxFn(this.bitsPerChar)
82+
this.charsPerChunk = lcm(this.bitsPerChar, BITS_PER_BYTE) / this.bitsPerChar
10683
}
10784

10885
bytesNeeded(bitLen) {
109-
const count = ceil(bitLen / this.bitsPerChar())
110-
return ceil((count * this.bitsPerChar()) / BITS_PER_BYTE)
86+
const count = ceil(bitLen / this.bitsPerChar)
87+
return ceil((count * this.bitsPerChar) / BITS_PER_BYTE)
11188
}
112-
113-
// Aliases
114-
chars() { return this.getChars() }
115-
ndxFn() { return this.getNdxFn() }
116-
bitsPerChar() { return this.getBitsPerChar() }
11789
}
11890

11991
const charset64 = new CharSet('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_')
@@ -126,7 +98,7 @@ const charset2 = new CharSet('01')
12698
const stringWithBytes = (bytes, bitLen, charset) => {
12799
if (bitLen <= 0) { return '' }
128100

129-
const bitsPerChar = charset.getBitsPerChar()
101+
const { bitsPerChar } = charset
130102
const count = ceil(bitLen / bitsPerChar)
131103
if (count <= 0) { return '' }
132104

@@ -135,13 +107,11 @@ const stringWithBytes = (bytes, bitLen, charset) => {
135107
throw new Error(`Insufficient bytes: need ${need} and got ${bytes.length}`)
136108
}
137109

138-
const charsPerChunk = charset.getCharsPerChunk()
110+
const { ndxFn, charsPerChunk, chars } = charset
111+
139112
const chunks = floor(count / charsPerChunk)
140113
const partials = count % charsPerChunk
141114

142-
const ndxFn = charset.getNdxFn()
143-
const chars = charset.getChars()
144-
145115
let string = ''
146116
for (let chunk = 0; chunk < chunks; chunk += 1) {
147117
for (let slice = 0; slice < charsPerChunk; slice += 1) {
@@ -289,7 +259,7 @@ class Entropy {
289259
}
290260

291261
chars() {
292-
return entropyProps.get(this).charset.chars()
262+
return entropyProps.get(this).charset.chars
293263
}
294264

295265
bits() {

tests/charset.js

+18-18
Original file line numberDiff line numberDiff line change
@@ -5,56 +5,56 @@ const {
55

66
test('charset64', () => {
77
const charset = new CharSet('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_')
8-
const { length } = charset.getChars()
8+
const { length } = charset.chars
99
expect(length).toBe(64)
1010
const bitsPerChar = Math.log2(length)
11-
expect(charset.getBitsPerChar()).toBe(bitsPerChar)
12-
expect(charset.getCharsPerChunk()).toBe(4)
11+
expect(charset.bitsPerChar).toBe(bitsPerChar)
12+
expect(charset.charsPerChunk).toBe(4)
1313
})
1414

1515
test('charset32', () => {
1616
const charset = new CharSet('2346789bdfghjmnpqrtBDFGHJLMNPQRT')
17-
const { length } = charset.getChars()
17+
const { length } = charset.chars
1818
expect(length).toBe(32)
1919
const bitsPerChar = Math.log2(length)
20-
expect(charset.getBitsPerChar()).toBe(bitsPerChar)
21-
expect(charset.getCharsPerChunk()).toBe(8)
20+
expect(charset.bitsPerChar).toBe(bitsPerChar)
21+
expect(charset.charsPerChunk).toBe(8)
2222
})
2323

2424
test('charset16', () => {
2525
const charset = new CharSet('0123456789abcdef')
26-
const { length } = charset.getChars()
26+
const { length } = charset.chars
2727
expect(length).toBe(16)
2828
const bitsPerChar = Math.log2(length)
29-
expect(charset.getBitsPerChar()).toBe(bitsPerChar)
30-
expect(charset.getCharsPerChunk()).toBe(2)
29+
expect(charset.bitsPerChar).toBe(bitsPerChar)
30+
expect(charset.charsPerChunk).toBe(2)
3131
})
3232

3333
test('charset8', () => {
3434
const charset = new CharSet('01234567')
35-
const { length } = charset.getChars()
35+
const { length } = charset.chars
3636
expect(length).toBe(8)
3737
const bitsPerChar = Math.log2(length)
38-
expect(charset.getBitsPerChar()).toBe(bitsPerChar)
39-
expect(charset.getCharsPerChunk()).toBe(8)
38+
expect(charset.bitsPerChar).toBe(bitsPerChar)
39+
expect(charset.charsPerChunk).toBe(8)
4040
})
4141

4242
test('charset4', () => {
4343
const charset = new CharSet('ATCG')
44-
const { length } = charset.getChars()
44+
const { length } = charset.chars
4545
expect(length).toBe(4)
4646
const bitsPerChar = Math.log2(length)
47-
expect(charset.getBitsPerChar()).toBe(bitsPerChar)
48-
expect(charset.getCharsPerChunk()).toBe(4)
47+
expect(charset.bitsPerChar).toBe(bitsPerChar)
48+
expect(charset.charsPerChunk).toBe(4)
4949
})
5050

5151
test('charset2', () => {
5252
const charset = new CharSet('01')
53-
const { length } = charset.getChars()
53+
const { length } = charset.chars
5454
expect(length).toBe(2)
5555
const bitsPerChar = Math.log2(length)
56-
expect(charset.getBitsPerChar()).toBe(bitsPerChar)
57-
expect(charset.getCharsPerChunk()).toBe(8)
56+
expect(charset.bitsPerChar).toBe(bitsPerChar)
57+
expect(charset.charsPerChunk).toBe(8)
5858
})
5959

6060
test('Custom chars: 64', () => {

tests/entropy.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,27 @@ const rbits = (t, r) => round(Entropy.bits(t, r))
1111
test('Entropy constructor', () => {
1212
let entropy = new Entropy()
1313
expect(entropy.bits()).toBe(128)
14-
expect(entropy.chars()).toBe(charset32.chars())
14+
expect(entropy.chars()).toBe(charset32.chars)
1515
expect(entropy.bytesNeeded()).toBe(17)
1616

1717
entropy = new Entropy({ bits: 32 })
1818
expect(entropy.bits()).toBe(32)
19-
expect(entropy.chars()).toBe(charset32.chars())
19+
expect(entropy.chars()).toBe(charset32.chars)
2020
expect(entropy.bytesNeeded()).toBe(5)
2121

2222
entropy = new Entropy({ total: 1000, risk: 1e9 })
2323
expect(entropy.bits()).toBe(49)
24-
expect(entropy.chars()).toBe(charset32.chars())
24+
expect(entropy.chars()).toBe(charset32.chars)
2525
expect(entropy.bytesNeeded()).toBe(7)
2626

2727
entropy = new Entropy({ bits: 32, charset: charset16 })
2828
expect(entropy.bits()).toBe(32)
29-
expect(entropy.chars()).toBe(charset16.chars())
29+
expect(entropy.chars()).toBe(charset16.chars)
3030
expect(entropy.bytesNeeded()).toBe(4)
3131

3232
entropy = new Entropy({ total: 1e6, risk: 10000000, charset: charset16 })
3333
expect(entropy.bits()).toBe(62)
34-
expect(entropy.chars()).toBe(charset16.chars())
34+
expect(entropy.chars()).toBe(charset16.chars)
3535
expect(entropy.bytesNeeded()).toBe(8)
3636
})
3737

0 commit comments

Comments
 (0)