Skip to content

Commit 5cb229f

Browse files
committed
Separate csprng and prng
Into separate files to facilitate not browserfying Crypto
1 parent b69c6e4 commit 5cb229f

File tree

7 files changed

+118
-75
lines changed

7 files changed

+118
-75
lines changed

dist/entropy-string.js

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@ var _log2 = _interopRequireDefault(_log);
1414

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

17-
var Crypto = require('crypto');
1817
var WeakMap = require('weak-map');
1918

19+
var csprngBytes = require('lib/csprng-bytes');
20+
var prngBytes = require('lib/prng-bytes');
21+
2022
var BITS_PER_BYTE = 8;
2123
var abs = Math.abs,
2224
ceil = Math.ceil,
2325
floor = Math.floor,
2426
log2 = _log2.default,
25-
random = Math.random,
2627
round = Math.round;
2728

2829

@@ -171,13 +172,6 @@ var charset8 = new CharSet('01234567');
171172
var charset4 = new CharSet('ATCG');
172173
var charset2 = new CharSet('01');
173174

174-
var endianByteNum = function () {
175-
var buf32 = new Uint32Array(1);
176-
var buf8 = new Uint8Array(buf32.buffer);
177-
buf32[0] = 0xff;
178-
return buf8[0] === 0xff ? [2, 3, 4, 5, 6, 7] : [0, 1, 2, 3, 6, 7];
179-
}();
180-
181175
var _stringWithBytes = function _stringWithBytes(bytes, bitLen, charset) {
182176
if (bitLen <= 0) {
183177
return '';
@@ -215,34 +209,27 @@ var _stringWithBytes = function _stringWithBytes(bytes, bitLen, charset) {
215209
return string;
216210
};
217211

218-
var csprngBytes = function csprngBytes(count) {
219-
return Buffer.from(Crypto.randomBytes(count));
220-
};
221-
222-
// const csprngBytes = count => (
223-
// process.browser ?
224-
// window.crypto.getRandomValues(new Uint8Array(count)) :
225-
// Buffer.from(Crypto.randomBytes(count))
226-
// )
227-
228-
var prngBytes = function prngBytes(count) {
229-
var BYTES_USED_PER_RANDOM_CALL = 6;
230-
var randCount = ceil(count / BYTES_USED_PER_RANDOM_CALL);
231-
232-
var buffer = Buffer.alloc(count);
233-
var dataView = new DataView(new ArrayBuffer(BITS_PER_BYTE));
234-
for (var rNum = 0; rNum < randCount; rNum += 1) {
235-
dataView.setFloat64(0, random());
236-
for (var n = 0; n < BYTES_USED_PER_RANDOM_CALL; n += 1) {
237-
var fByteNum = endianByteNum[n];
238-
var bByteNum = rNum * BYTES_USED_PER_RANDOM_CALL + n;
239-
if (bByteNum < count) {
240-
buffer[bByteNum] = dataView.getUint8(fByteNum);
241-
}
242-
}
243-
}
244-
return buffer;
245-
};
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+
// }
246233

247234
var entropyBits = function entropyBits(total, risk) {
248235
if (total === 0) {

dist/lib/csprng-bytes.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
var Crypto = require('crypto');
4+
5+
var csprngBytes = function csprngBytes(count) {
6+
return Buffer.from(Crypto.randomBytes(count));
7+
};
8+
9+
module.exports = {
10+
csprngBytes: csprngBytes
11+
};

dist/lib/prng-bytes.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
3+
var random = Math.random;
4+
5+
6+
var BITS_PER_BYTE = 8;
7+
8+
var endianByteNum = function () {
9+
var buf32 = new Uint32Array(1);
10+
var buf8 = new Uint8Array(buf32.buffer);
11+
buf32[0] = 0xff;
12+
return buf8[0] === 0xff ? [2, 3, 4, 5, 6, 7] : [0, 1, 2, 3, 6, 7];
13+
}();
14+
15+
var prngBytes = function prngBytes(count) {
16+
console.log('CxDebug prng: true');
17+
var BYTES_USED_PER_RANDOM_CALL = 6;
18+
var randCount = ceil(count / BYTES_USED_PER_RANDOM_CALL);
19+
20+
var buffer = new ArrayBuffer(count);
21+
var dataView = new DataView(new ArrayBuffer(BITS_PER_BYTE));
22+
for (var rNum = 0; rNum < randCount; rNum += 1) {
23+
dataView.setFloat64(0, random());
24+
for (var n = 0; n < BYTES_USED_PER_RANDOM_CALL; n += 1) {
25+
var fByteNum = endianByteNum[n];
26+
var bByteNum = rNum * BYTES_USED_PER_RANDOM_CALL + n;
27+
if (bByteNum < count) {
28+
buffer[bByteNum] = dataView.getUint8(fByteNum);
29+
}
30+
}
31+
}
32+
return buffer;
33+
};
34+
35+
module.exports = {
36+
prngBytes: prngBytes
37+
};

entropy-string.js

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
const Crypto = require('crypto')
21
const WeakMap = require('weak-map')
32

3+
const { csprngBytes } = require('./lib/csprng-bytes')
4+
const { prngBytes } = require('./lib/prng-bytes')
5+
46
const BITS_PER_BYTE = 8
57
const {
6-
abs, ceil, floor, log2, random, round
8+
abs, ceil, floor, log2, round
79
} = Math
810

911
const gcd = (a, b) => {
@@ -121,13 +123,6 @@ const charset8 = new CharSet('01234567')
121123
const charset4 = new CharSet('ATCG')
122124
const charset2 = new CharSet('01')
123125

124-
const endianByteNum = (() => {
125-
const buf32 = new Uint32Array(1)
126-
const buf8 = new Uint8Array(buf32.buffer)
127-
buf32[0] = 0xff
128-
return (buf8[0] === 0xff) ? [2, 3, 4, 5, 6, 7] : [0, 1, 2, 3, 6, 7]
129-
})()
130-
131126
const stringWithBytes = (bytes, bitLen, charset) => {
132127
if (bitLen <= 0) { return '' }
133128

@@ -161,33 +156,6 @@ const stringWithBytes = (bytes, bitLen, charset) => {
161156
return string
162157
}
163158

164-
const csprngBytes = count => Buffer.from(Crypto.randomBytes(count))
165-
166-
// const csprngBytes = count => (
167-
// process.browser ?
168-
// window.crypto.getRandomValues(new Uint8Array(count)) :
169-
// Buffer.from(Crypto.randomBytes(count))
170-
// )
171-
172-
const prngBytes = (count) => {
173-
const BYTES_USED_PER_RANDOM_CALL = 6
174-
const randCount = ceil(count / BYTES_USED_PER_RANDOM_CALL)
175-
176-
const buffer = Buffer.alloc(count)
177-
const dataView = new DataView(new ArrayBuffer(BITS_PER_BYTE))
178-
for (let rNum = 0; rNum < randCount; rNum += 1) {
179-
dataView.setFloat64(0, random())
180-
for (let n = 0; n < BYTES_USED_PER_RANDOM_CALL; n += 1) {
181-
const fByteNum = endianByteNum[n]
182-
const bByteNum = (rNum * BYTES_USED_PER_RANDOM_CALL) + n
183-
if (bByteNum < count) {
184-
buffer[bByteNum] = dataView.getUint8(fByteNum)
185-
}
186-
}
187-
}
188-
return buffer
189-
}
190-
191159
const entropyBits = (total, risk) => {
192160
if (total === 0) { return 0 }
193161
let N

lib/csprng-bytes.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const Crypto = require('crypto')
2+
3+
const csprngBytes = count => Buffer.from(Crypto.randomBytes(count))
4+
5+
module.exports = {
6+
csprngBytes
7+
}

lib/prng-bytes.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const { ceil, random } = Math
2+
3+
const BITS_PER_BYTE = 8
4+
5+
const endianByteNum = (() => {
6+
const buf32 = new Uint32Array(1)
7+
const buf8 = new Uint8Array(buf32.buffer)
8+
buf32[0] = 0xff
9+
return (buf8[0] === 0xff) ? [2, 3, 4, 5, 6, 7] : [0, 1, 2, 3, 6, 7]
10+
})()
11+
12+
const prngBytes = (count) => {
13+
const BYTES_USED_PER_RANDOM_CALL = 6
14+
const randCount = ceil(count / BYTES_USED_PER_RANDOM_CALL)
15+
16+
const buffer = new ArrayBuffer(count)
17+
const dataView = new DataView(new ArrayBuffer(BITS_PER_BYTE))
18+
for (let rNum = 0; rNum < randCount; rNum += 1) {
19+
dataView.setFloat64(0, random())
20+
for (let n = 0; n < BYTES_USED_PER_RANDOM_CALL; n += 1) {
21+
const fByteNum = endianByteNum[n]
22+
const bByteNum = (rNum * BYTES_USED_PER_RANDOM_CALL) + n
23+
if (bByteNum < count) {
24+
buffer[bByteNum] = dataView.getUint8(fByteNum)
25+
}
26+
}
27+
}
28+
return buffer
29+
}
30+
31+
module.exports = {
32+
prngBytes
33+
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
"test": "test"
99
},
1010
"scripts": {
11-
"build": "yarn clean && BABEL_ENV=production babel --out-dir=dist entropy-string.js",
11+
"build": "yarn clean && BABEL_ENV=production babel --out-dir=dist entropy-string.js lib/*.js",
1212
"clean": "rm -f dist/*.js",
1313
"examples": "cd examples && BABEL_ENV=development babel --out-dir=dist *.js && cd ..",
14-
"lint": "eslint entropy-string.js tests/*js examples/*js",
14+
"lint": "eslint entropy-string.js lib/*.js tests/*js examples/*js",
1515
"prepare": "yarn build",
1616
"test": "yarn lint && yarn jest"
1717
},

0 commit comments

Comments
 (0)