@@ -129,7 +129,7 @@ var createCharset = function createCharset(arg) {
129129
130130var _class = function ( ) {
131131 function _class ( ) {
132- var arg = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : { bits : 128 , charset : CharSet . charset32 } ;
132+ var arg = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : { bits : 128 , charset : charset32 } ;
133133 ( 0 , _classCallCheck3 . default ) ( this , _class ) ;
134134
135135 var charset = void 0 ;
@@ -138,12 +138,14 @@ var _class = function () {
138138 if ( arg instanceof CharSet || arg instanceof String || typeof arg === 'string' ) {
139139 charset = createCharset ( arg ) ;
140140 } else if ( arg instanceof Object ) {
141+ var round = Math . round ;
142+
141143 if ( typeof arg . bits === 'number' ) {
142- bitLen = arg . bits ;
144+ bitLen = round ( arg . bits ) ;
143145 } else if ( typeof arg . total === 'number' && typeof arg . risk === 'number' ) {
144- bitLen = entropyBits ( arg . total , arg . risk ) ;
146+ bitLen = round ( entropyBits ( arg . total , arg . risk ) ) ;
145147 } else {
146- throw new Error ( 'Entropy params must include either bits or both total and risk' ) ;
148+ bitLen = 128 ;
147149 }
148150 charset = createCharset ( arg . charset ) ;
149151 } else {
@@ -152,16 +154,15 @@ var _class = function () {
152154
153155 if ( charset === undefined ) {
154156 throw new Error ( 'Invalid constructor CharSet declaration' ) ;
155- } else if ( bits < 0 ) {
157+ } else if ( bitLen < 0 ) {
156158 throw new Error ( 'Invalid constructor declaration of bits less than zero' ) ;
157159 }
158160
159- var hideProps = {
161+ propMap . set ( this , {
160162 charset : charset ,
161163 bitLen : bitLen ,
162164 bytesNeeded : charset . bytesNeeded ( bitLen )
163- } ;
164- propMap . set ( this , hideProps ) ;
165+ } ) ;
165166 }
166167
167168 ( 0 , _createClass3 . default ) ( _class , [ {
@@ -202,28 +203,34 @@ var _class = function () {
202203 } , {
203204 key : 'string' ,
204205 value : function string ( ) {
205- var bitLen = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : undefined ;
206+ var bitLen = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : propMap . get ( this ) . bitLen ;
206207 var charset = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : propMap . get ( this ) . charset ;
207208
208- if ( bitLen === undefined ) {
209- var _propMap$get = propMap . get ( this ) ,
210- classCharset = _propMap$get . charset ,
211- classBitLen = _propMap$get . bitLen ,
212- _bytesNeeded = _propMap$get . bytesNeeded ;
213-
214- return this . stringWithBytes ( classBitLen , cryptoBytes ( _bytesNeeded ) , classCharset ) ;
215- }
216209 var bytesNeeded = charset . bytesNeeded ( bitLen ) ;
217210 return this . stringWithBytes ( bitLen , cryptoBytes ( bytesNeeded ) , charset ) ;
218211 }
219212 } , {
220- key : 'stringRandom' ,
221- value : function stringRandom ( bitLen ) {
213+ key : 'stringPRNG' ,
214+ value : function stringPRNG ( ) {
215+ var bitLen = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : propMap . get ( this ) . bitLen ;
222216 var charset = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : propMap . get ( this ) . charset ;
223217
224218 var bytesNeeded = charset . bytesNeeded ( bitLen ) ;
225219 return this . stringWithBytes ( bitLen , randomBytes ( bytesNeeded ) , charset ) ;
226220 }
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+ }
227234 } , {
228235 key : 'stringWithBytes' ,
229236 value : function stringWithBytes ( bitLen , bytes ) {
@@ -233,7 +240,8 @@ var _class = function () {
233240 }
234241 } , {
235242 key : 'bytesNeeded' ,
236- value : function bytesNeeded ( bitLen ) {
243+ value : function bytesNeeded ( ) {
244+ var bitLen = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : propMap . get ( this ) . bitLen ;
237245 var charset = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : propMap . get ( this ) . charset ;
238246
239247 return charset . bytesNeeded ( bitLen ) ;
0 commit comments