@@ -48,19 +48,23 @@ var _stringWithBytes = function _stringWithBytes(bytes, bitLen, charset) {
4848 return '' ;
4949 }
5050
51+ var floor = Math . floor ,
52+ ceil = Math . ceil ;
53+
54+
5155 var bitsPerChar = charset . getBitsPerChar ( ) ;
52- var count = Math . ceil ( bitLen / bitsPerChar ) ;
56+ var count = ceil ( bitLen / bitsPerChar ) ;
5357 if ( count <= 0 ) {
5458 return '' ;
5559 }
5660
57- var need = Math . ceil ( count * ( bitsPerChar / BITS_PER_BYTE ) ) ;
61+ var need = ceil ( count * ( bitsPerChar / BITS_PER_BYTE ) ) ;
5862 if ( bytes . length < need ) {
5963 throw new Error ( 'Insufficient bytes: need ' + need + ' and got ' + bytes . length ) ;
6064 }
6165
6266 var charsPerChunk = charset . getCharsPerChunk ( ) ;
63- var chunks = Math . floor ( count / charsPerChunk ) ;
67+ var chunks = floor ( count / charsPerChunk ) ;
6468 var partials = count % charsPerChunk ;
6569
6670 var ndxFn = charset . getNdxFn ( ) ;
@@ -118,51 +122,85 @@ var entropyBits = function entropyBits(total, risk) {
118122 return N + log2 ( risk ) - 1 ;
119123} ;
120124
121- var createCharset = function createCharset ( arg ) {
122- if ( arg instanceof CharSet ) {
123- return arg ;
124- } else if ( typeof arg === 'string' || arg instanceof String ) {
125- return new CharSet ( arg ) ;
126- }
127- return charset32 ;
128- } ;
129-
130125var _class = function ( ) {
131126 function _class ( ) {
132- var arg = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : { bits : 128 , charset : charset32 } ;
127+ var params = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : { bits : 128 , charset : charset32 } ;
133128 ( 0 , _classCallCheck3 . default ) ( this , _class ) ;
134129
135- var charset = void 0 ;
136- var bitLen = 0 ;
137-
138- if ( arg instanceof CharSet || arg instanceof String || typeof arg === 'string' ) {
139- charset = createCharset ( arg ) ;
140- } else if ( arg instanceof Object ) {
141- var round = Math . round ;
142-
143- if ( typeof arg . bits === 'number' ) {
144- bitLen = round ( arg . bits ) ;
145- } else if ( typeof arg . total === 'number' && typeof arg . risk === 'number' ) {
146- bitLen = round ( entropyBits ( arg . total , arg . risk ) ) ;
147- } else {
148- bitLen = 128 ;
130+ if ( params !== undefined ) {
131+ if ( ! ( params instanceof Object ) ) {
132+ throw new Error ( 'Invalid argument for Entropy constructor: Expect params object' ) ;
133+ }
134+
135+ if ( params . bits === undefined && params . charset === undefined && params . total === undefined && params . risk === undefined ) {
136+ throw new Error ( 'Invalid Entropy params' ) ;
137+ }
138+
139+ if ( params . bits !== undefined ) {
140+ if ( typeof params . bits !== 'number' ) {
141+ throw new Error ( 'Invalid Entropy params: non-numeric bits' ) ;
142+ }
143+ if ( params . bits < 0 ) {
144+ throw new Error ( 'Invalid Entropy params: negative bits' ) ;
145+ }
146+ }
147+
148+ if ( params . total !== undefined ) {
149+ if ( typeof params . total !== 'number' ) {
150+ throw new Error ( 'Invalid Entropy params: non-numeric total' ) ;
151+ }
152+ if ( params . total < 1 ) {
153+ throw new Error ( 'Invalid Entropy params: non-positive total' ) ;
154+ }
155+ }
156+
157+ if ( params . risk !== undefined ) {
158+ if ( typeof params . risk !== 'number' ) {
159+ throw new Error ( 'Invalid Entropy params: non-numeric risk' ) ;
160+ }
161+ if ( params . risk < 1 ) {
162+ throw new Error ( 'Invalid Entropy params: non-positive risk' ) ;
163+ }
164+ }
165+
166+ if ( params . risk !== undefined && typeof params . risk !== 'number' ) {
167+ throw new Error ( 'Invalid Entropy params: non-numeric risk' ) ;
168+ }
169+
170+ if ( params . total !== undefined && params . risk === undefined || params . total === undefined && params . risk !== undefined ) {
171+ throw new Error ( 'Invalid Entropy params: total and risk must be paired' ) ;
172+ }
173+
174+ if ( params . bits !== undefined && ( params . total !== undefined || params . risk !== undefined ) ) {
175+ throw new Error ( 'Invalid Entropy params: bits with total and/or risk' ) ;
149176 }
150- charset = createCharset ( arg . charset ) ;
177+ }
178+
179+ var bitLen = void 0 ;
180+ var round = Math . round ;
181+
182+ if ( params . bits ) {
183+ bitLen = round ( params . bits ) ;
184+ } else if ( params . total && params . risk ) {
185+ bitLen = round ( entropyBits ( params . total , params . risk ) ) ;
151186 } else {
152- throw new Error ( 'Constructor arg must either be a valid CharSet, valid characters, or valid Entropy params' ) ;
187+ bitLen = 128 ;
153188 }
154189
155- if ( charset === undefined ) {
156- throw new Error ( 'Invalid constructor CharSet declaration' ) ;
157- } else if ( bitLen < 0 ) {
158- throw new Error ( 'Invalid constructor declaration of bits less than zero' ) ;
190+ var charset = void 0 ;
191+ if ( params . charset instanceof CharSet ) {
192+ var cs = params . charset ;
193+
194+ charset = cs ;
195+ } else if ( typeof params . charset === 'string' || params . charset instanceof String ) {
196+ charset = new CharSet ( params . charset ) ;
197+ } else {
198+ charset = charset32 ;
159199 }
160200
161- propMap . set ( this , {
162- charset : charset ,
163- bitLen : bitLen ,
164- bytesNeeded : charset . bytesNeeded ( bitLen )
165- } ) ;
201+ var prng = params . prng || false ;
202+
203+ propMap . set ( this , { charset : charset , bitLen : bitLen , prng : prng } ) ;
166204 }
167205
168206 ( 0 , _createClass3 . default ) ( _class , [ {
@@ -218,22 +256,10 @@ var _class = function () {
218256 var bytesNeeded = charset . bytesNeeded ( bitLen ) ;
219257 return this . stringWithBytes ( randomBytes ( bytesNeeded ) , bitLen , charset ) ;
220258 }
221-
222- /**
223- * @deprecated Since version 3.1. Will be deleted in version 4.0. Use stringPRNG instead.
224- */
225-
226- } , {
227- key : 'stringRandom' ,
228- value : function stringRandom ( ) {
229- var bitLen = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : propMap . get ( this ) . bitLen ;
230- var charset = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : propMap . get ( this ) . charset ;
231-
232- return this . stringPRNG ( bitLen , charset ) ;
233- }
234259 } , {
235260 key : 'stringWithBytes' ,
236- value : function stringWithBytes ( bytes , bitLen ) {
261+ value : function stringWithBytes ( bytes ) {
262+ var bitLen = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : propMap . get ( this ) . bitLen ;
237263 var charset = arguments . length > 2 && arguments [ 2 ] !== undefined ? arguments [ 2 ] : propMap . get ( this ) . charset ;
238264
239265 return _stringWithBytes ( bytes , bitLen , charset ) ;
0 commit comments