@@ -5,10 +5,6 @@ Object.defineProperty(exports, "__esModule", {
55} ) ;
66exports . charset2 = exports . charset4 = exports . charset8 = exports . charset16 = exports . charset32 = exports . charset64 = undefined ;
77
8- var _log = require ( 'babel-runtime/core-js/math/log2' ) ;
9-
10- var _log2 = _interopRequireDefault ( _log ) ;
11-
128var _classCallCheck2 = require ( 'babel-runtime/helpers/classCallCheck' ) ;
139
1410var _classCallCheck3 = _interopRequireDefault ( _classCallCheck2 ) ;
@@ -17,6 +13,10 @@ var _createClass2 = require('babel-runtime/helpers/createClass');
1713
1814var _createClass3 = _interopRequireDefault ( _createClass2 ) ;
1915
16+ var _log = require ( 'babel-runtime/core-js/math/log2' ) ;
17+
18+ var _log2 = _interopRequireDefault ( _log ) ;
19+
2020function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
2121
2222var Crypto = require ( 'crypto' ) ;
@@ -103,22 +103,63 @@ var randomBytes = function randomBytes(count) {
103103 return buffer ;
104104} ;
105105
106+ var entropyBits = function entropyBits ( total , risk ) {
107+ if ( total === 0 ) {
108+ return 0 ;
109+ }
110+ var log2 = _log2 . default ;
111+
112+ var N = void 0 ;
113+ if ( total < 1000 ) {
114+ N = log2 ( total ) + log2 ( total - 1 ) ;
115+ } else {
116+ N = 2 * log2 ( total ) ;
117+ }
118+ return N + log2 ( risk ) - 1 ;
119+ } ;
120+
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+
106130var _class = function ( ) {
107- function _class ( arg ) {
131+ function _class ( ) {
132+ var arg = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : { bits : 128 , charset : CharSet . charset32 } ;
108133 ( 0 , _classCallCheck3 . default ) ( this , _class ) ;
109134
110135 var charset = void 0 ;
111- if ( arg === undefined ) {
112- charset = charset32 ;
113- } else if ( arg instanceof CharSet ) {
114- charset = arg ;
115- } else if ( typeof arg === 'string' || arg instanceof String ) {
116- charset = new CharSet ( arg ) ;
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+ if ( typeof arg . bits === 'number' ) {
142+ bitLen = arg . bits ;
143+ } else if ( typeof arg . total === 'number' && typeof arg . risk === 'number' ) {
144+ bitLen = entropyBits ( arg . total , arg . risk ) ;
145+ } else {
146+ throw new Error ( 'Entropy params must include either bits or both total and risk' ) ;
147+ }
148+ charset = createCharset ( arg . charset ) ;
117149 } else {
118- throw new Error ( 'Invalid arg: must be either valid CharSet or valid chars' ) ;
150+ throw new Error ( 'Constructor arg must either be a valid CharSet, valid characters, or valid Entropy params' ) ;
151+ }
152+
153+ if ( charset === undefined ) {
154+ throw new Error ( 'Invalid constructor CharSet declaration' ) ;
155+ } else if ( bits < 0 ) {
156+ throw new Error ( 'Invalid constructor declaration of bits less than zero' ) ;
119157 }
158+
120159 var hideProps = {
121- charset : charset
160+ charset : charset ,
161+ bitLen : bitLen ,
162+ bytesNeeded : charset . bytesNeeded ( bitLen )
122163 } ;
123164 propMap . set ( this , hideProps ) ;
124165 }
@@ -160,9 +201,18 @@ var _class = function () {
160201 }
161202 } , {
162203 key : 'string' ,
163- value : function string ( bitLen ) {
204+ value : function string ( ) {
205+ var bitLen = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : undefined ;
164206 var charset = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : propMap . get ( this ) . charset ;
165207
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+ }
166216 var bytesNeeded = charset . bytesNeeded ( bitLen ) ;
167217 return this . stringWithBytes ( bitLen , cryptoBytes ( bytesNeeded ) , charset ) ;
168218 }
@@ -193,6 +243,11 @@ var _class = function () {
193243 value : function chars ( ) {
194244 return propMap . get ( this ) . charset . chars ( ) ;
195245 }
246+ } , {
247+ key : 'bits' ,
248+ value : function bits ( ) {
249+ return propMap . get ( this ) . bitLen ;
250+ }
196251 } , {
197252 key : 'use' ,
198253 value : function use ( charset ) {
@@ -212,20 +267,7 @@ var _class = function () {
212267 } ] , [ {
213268 key : 'bits' ,
214269 value : function bits ( total , risk ) {
215- if ( total === 0 ) {
216- return 0 ;
217- }
218-
219- var log2 = _log2 . default ;
220-
221-
222- var N = void 0 ;
223- if ( total < 1000 ) {
224- N = log2 ( total ) + log2 ( total - 1 ) ;
225- } else {
226- N = 2 * log2 ( total ) ;
227- }
228- return N + log2 ( risk ) - 1 ;
270+ return entropyBits ( total , risk ) ;
229271 }
230272 } ] ) ;
231273 return _class ;
0 commit comments