@@ -5,10 +5,6 @@ Object.defineProperty(exports, "__esModule", {
5
5
} ) ;
6
6
exports . charset2 = exports . charset4 = exports . charset8 = exports . charset16 = exports . charset32 = exports . charset64 = undefined ;
7
7
8
- var _log = require ( 'babel-runtime/core-js/math/log2' ) ;
9
-
10
- var _log2 = _interopRequireDefault ( _log ) ;
11
-
12
8
var _classCallCheck2 = require ( 'babel-runtime/helpers/classCallCheck' ) ;
13
9
14
10
var _classCallCheck3 = _interopRequireDefault ( _classCallCheck2 ) ;
@@ -17,6 +13,10 @@ var _createClass2 = require('babel-runtime/helpers/createClass');
17
13
18
14
var _createClass3 = _interopRequireDefault ( _createClass2 ) ;
19
15
16
+ var _log = require ( 'babel-runtime/core-js/math/log2' ) ;
17
+
18
+ var _log2 = _interopRequireDefault ( _log ) ;
19
+
20
20
function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
21
21
22
22
var Crypto = require ( 'crypto' ) ;
@@ -103,22 +103,63 @@ var randomBytes = function randomBytes(count) {
103
103
return buffer ;
104
104
} ;
105
105
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
+
106
130
var _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 } ;
108
133
( 0 , _classCallCheck3 . default ) ( this , _class ) ;
109
134
110
135
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 ) ;
117
149
} 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' ) ;
119
157
}
158
+
120
159
var hideProps = {
121
- charset : charset
160
+ charset : charset ,
161
+ bitLen : bitLen ,
162
+ bytesNeeded : charset . bytesNeeded ( bitLen )
122
163
} ;
123
164
propMap . set ( this , hideProps ) ;
124
165
}
@@ -160,9 +201,18 @@ var _class = function () {
160
201
}
161
202
} , {
162
203
key : 'string' ,
163
- value : function string ( bitLen ) {
204
+ value : function string ( ) {
205
+ var bitLen = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : undefined ;
164
206
var charset = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : propMap . get ( this ) . charset ;
165
207
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
+ }
166
216
var bytesNeeded = charset . bytesNeeded ( bitLen ) ;
167
217
return this . stringWithBytes ( bitLen , cryptoBytes ( bytesNeeded ) , charset ) ;
168
218
}
@@ -193,6 +243,11 @@ var _class = function () {
193
243
value : function chars ( ) {
194
244
return propMap . get ( this ) . charset . chars ( ) ;
195
245
}
246
+ } , {
247
+ key : 'bits' ,
248
+ value : function bits ( ) {
249
+ return propMap . get ( this ) . bitLen ;
250
+ }
196
251
} , {
197
252
key : 'use' ,
198
253
value : function use ( charset ) {
@@ -212,20 +267,7 @@ var _class = function () {
212
267
} ] , [ {
213
268
key : 'bits' ,
214
269
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 ) ;
229
271
}
230
272
} ] ) ;
231
273
return _class ;
0 commit comments