@@ -22,12 +22,17 @@ var _lcm2 = _interopRequireDefault(_lcm);
22
22
23
23
function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
24
24
25
- var crypto = require ( 'crypto' ) ;
26
-
27
25
var log2 = _log4 . default ;
28
26
var log10 = _log2 . default ;
29
27
var log2_10 = log2 ( 10 ) ;
30
28
29
+ var endianByteNum = function ( ) {
30
+ var buf32 = new Uint32Array ( 1 ) ;
31
+ var buf8 = new Uint8Array ( buf32 . buffer ) ;
32
+ buf32 [ 0 ] = 0xff ;
33
+ return buf8 [ 0 ] === 0xff ? [ 2 , 3 , 4 , 5 , 6 , 7 ] : [ 0 , 1 , 2 , 3 , 6 , 7 ] ;
34
+ } ( ) ;
35
+
31
36
var bits = function bits ( total , risk ) {
32
37
if ( total == 0 ) {
33
38
return 0 ;
@@ -69,32 +74,22 @@ var bitsWithPowers = function bitsWithPowers(tPower, rPower) {
69
74
return N ;
70
75
} ;
71
76
72
- var randomString = function randomString ( bits , charSet ) {
73
- if ( ! _charSet2 . default . isValid ( charSet ) ) {
74
- throw new Error ( 'Invalid CharSet' ) ;
75
- }
76
-
77
- var count = Math . ceil ( bits / charSet . entropyPerChar ) ;
78
- if ( count <= 0 ) {
79
- return '' ;
80
- }
81
-
82
- var bytes = randomBytes ( count , charSet ) ;
77
+ var randomString = function randomString ( entropy , charSet ) {
78
+ var crypto = arguments . length > 2 && arguments [ 2 ] !== undefined ? arguments [ 2 ] : true ;
83
79
84
- return randomStringWithBytes ( bits , charSet , bytes ) ;
80
+ var bytes = crypto ? cryptoBytes ( entropy , charSet ) : randomBytes ( entropy , charSet ) ;
81
+ return randomStringWithBytes ( entropy , charSet , bytes ) ;
85
82
} ;
86
83
87
- var randomStringWithBytes = function randomStringWithBytes ( bits , charSet , bytes ) {
84
+ var randomStringWithBytes = function randomStringWithBytes ( entropy , charSet , bytes ) {
88
85
if ( ! _charSet2 . default . isValid ( charSet ) ) {
89
- console . log ( 'WTF?' ) ;
90
86
throw new Error ( 'Invalid CharSet' ) ;
91
87
}
92
-
93
- if ( bits <= 0 ) {
88
+ if ( entropy <= 0 ) {
94
89
return '' ;
95
90
}
96
91
97
- var count = Math . ceil ( bits / charSet . entropyPerChar ) ;
92
+ var count = Math . ceil ( entropy / charSet . entropyPerChar ) ;
98
93
if ( count <= 0 ) {
99
94
return '' ;
100
95
}
@@ -152,10 +147,45 @@ var randomStringWithBytes = function randomStringWithBytes(bits, charSet, bytes)
152
147
return string ;
153
148
} ;
154
149
155
- var randomBytes = function randomBytes ( count , charSet ) {
150
+ var bytesNeeded = function bytesNeeded ( entropy , charSet ) {
151
+ if ( ! _charSet2 . default . isValid ( charSet ) ) {
152
+ throw new Error ( 'Invalid CharSet' ) ;
153
+ }
154
+ var count = Math . ceil ( entropy / charSet . entropyPerChar ) ;
155
+ if ( count <= 0 ) {
156
+ return 0 ;
157
+ }
158
+
156
159
var bytesPerSlice = charSet . entropyPerChar / 8 ;
157
- var bytesNeeded = Math . ceil ( count * bytesPerSlice ) ;
158
- return Buffer . from ( crypto . randomBytes ( bytesNeeded ) ) ;
160
+ return Math . ceil ( count * bytesPerSlice ) ;
161
+ } ;
162
+
163
+ var cryptoBytes = function cryptoBytes ( entropy , charSet ) {
164
+ var crypto = require ( 'crypto' ) ;
165
+ return Buffer . from ( crypto . randomBytes ( bytesNeeded ( entropy , charSet ) ) ) ;
166
+ } ;
167
+
168
+ var randomBytes = function randomBytes ( entropy , charSet ) {
169
+ var byteCount = bytesNeeded ( entropy , charSet ) ;
170
+ var randCount = Math . ceil ( byteCount / 6 ) ;
171
+
172
+ var buffer = new Buffer ( byteCount ) ;
173
+ var dataView = new DataView ( new ArrayBuffer ( 8 ) ) ;
174
+ for ( var rNum = 0 ; rNum < randCount ; rNum ++ ) {
175
+ dataView . setFloat64 ( 0 , Math . random ( ) ) ;
176
+ for ( var n = 0 ; n < 6 ; n ++ ) {
177
+ var fByteNum = endianByteNum [ n ] ;
178
+ var bByteNum = 6 * rNum + n ;
179
+ bufferByte ( buffer , bByteNum , fByteNum , byteCount , dataView ) ;
180
+ }
181
+ }
182
+ return buffer ;
183
+ } ;
184
+
185
+ var bufferByte = function bufferByte ( buffer , bByte , nByte , byteCount , dataView ) {
186
+ if ( bByte < byteCount ) {
187
+ buffer [ bByte ] = dataView . getUint8 ( nByte ) ;
188
+ }
159
189
} ;
160
190
161
191
var ndx64 = function ndx64 ( chunk , slice , bytes ) {
@@ -209,6 +239,7 @@ exports.default = {
209
239
bitsWithPowers : bitsWithPowers ,
210
240
randomString : randomString ,
211
241
randomStringWithBytes : randomStringWithBytes ,
242
+ bytesNeeded : bytesNeeded ,
212
243
213
244
charSet64 : _charSet2 . default . charSet64 ,
214
245
charSet32 : _charSet2 . default . charSet32 ,
0 commit comments