@@ -22,12 +22,17 @@ var _lcm2 = _interopRequireDefault(_lcm);
2222
2323function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
2424
25- var crypto = require ( 'crypto' ) ;
26-
2725var log2 = _log4 . default ;
2826var log10 = _log2 . default ;
2927var log2_10 = log2 ( 10 ) ;
3028
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+
3136var bits = function bits ( total , risk ) {
3237 if ( total == 0 ) {
3338 return 0 ;
@@ -69,32 +74,22 @@ var bitsWithPowers = function bitsWithPowers(tPower, rPower) {
6974 return N ;
7075} ;
7176
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 ;
8379
84- return randomStringWithBytes ( bits , charSet , bytes ) ;
80+ var bytes = crypto ? cryptoBytes ( entropy , charSet ) : randomBytes ( entropy , charSet ) ;
81+ return randomStringWithBytes ( entropy , charSet , bytes ) ;
8582} ;
8683
87- var randomStringWithBytes = function randomStringWithBytes ( bits , charSet , bytes ) {
84+ var randomStringWithBytes = function randomStringWithBytes ( entropy , charSet , bytes ) {
8885 if ( ! _charSet2 . default . isValid ( charSet ) ) {
89- console . log ( 'WTF?' ) ;
9086 throw new Error ( 'Invalid CharSet' ) ;
9187 }
92-
93- if ( bits <= 0 ) {
88+ if ( entropy <= 0 ) {
9489 return '' ;
9590 }
9691
97- var count = Math . ceil ( bits / charSet . entropyPerChar ) ;
92+ var count = Math . ceil ( entropy / charSet . entropyPerChar ) ;
9893 if ( count <= 0 ) {
9994 return '' ;
10095 }
@@ -152,10 +147,45 @@ var randomStringWithBytes = function randomStringWithBytes(bits, charSet, bytes)
152147 return string ;
153148} ;
154149
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+
156159 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+ }
159189} ;
160190
161191var ndx64 = function ndx64 ( chunk , slice , bytes ) {
@@ -209,6 +239,7 @@ exports.default = {
209239 bitsWithPowers : bitsWithPowers ,
210240 randomString : randomString ,
211241 randomStringWithBytes : randomStringWithBytes ,
242+ bytesNeeded : bytesNeeded ,
212243
213244 charSet64 : _charSet2 . default . charSet64 ,
214245 charSet32 : _charSet2 . default . charSet32 ,
0 commit comments