11'use strict' ;
22
3- Object . defineProperty ( exports , "__esModule" , {
4- value : true
5- } ) ;
6- exports . charset2 = exports . charset4 = exports . charset8 = exports . charset16 = exports . charset32 = exports . charset64 = undefined ;
7-
83var _classCallCheck2 = require ( 'babel-runtime/helpers/classCallCheck' ) ;
94
105var _classCallCheck3 = _interopRequireDefault ( _classCallCheck2 ) ;
@@ -22,25 +17,160 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
2217var Crypto = require ( 'crypto' ) ;
2318var WeakMap = require ( 'weak-map' ) ;
2419
25- var _require = require ( './charset' ) ,
26- CharSet = _require . default ;
27-
28- var charset64 = exports . charset64 = new CharSet ( 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_' ) ;
29- var charset32 = exports . charset32 = new CharSet ( '2346789bdfghjmnpqrtBDFGHJLMNPQRT' ) ;
30- var charset16 = exports . charset16 = new CharSet ( '0123456789abcdef' ) ;
31- var charset8 = exports . charset8 = new CharSet ( '01234567' ) ;
32- var charset4 = exports . charset4 = new CharSet ( 'ATCG' ) ;
33- var charset2 = exports . charset2 = new CharSet ( '01' ) ;
34-
35- var propMap = new WeakMap ( ) ;
3620var BITS_PER_BYTE = 8 ;
37- var ceil = Math . ceil ,
21+ var abs = Math . abs ,
22+ ceil = Math . ceil ,
3823 floor = Math . floor ,
3924 log2 = _log2 . default ,
4025 random = Math . random ,
4126 round = Math . round ;
4227
4328
29+ var gcd = function gcd ( a , b ) {
30+ var la = a ;
31+ var lb = b ;
32+ while ( lb !== 0 ) {
33+ var _ref = [ lb , la % lb ] ;
34+ la = _ref [ 0 ] ;
35+ lb = _ref [ 1 ] ;
36+ }
37+ return abs ( la ) ;
38+ } ;
39+ var lcm = function lcm ( a , b ) {
40+ return a / gcd ( a , b ) * b ;
41+ } ;
42+
43+ var genNdxFn = function genNdxFn ( bitsPerChar ) {
44+ // If BITS_PER_BYTEs is a multiple of bitsPerChar, we can slice off an integer number
45+ // of chars per byte.
46+ if ( lcm ( bitsPerChar , BITS_PER_BYTE ) === BITS_PER_BYTE ) {
47+ return function ( chunk , slice , bytes ) {
48+ var lShift = bitsPerChar ;
49+ var rShift = BITS_PER_BYTE - bitsPerChar ;
50+ return ( bytes [ chunk ] << lShift * slice & 0xff ) >> rShift ;
51+ } ;
52+ }
53+
54+ // Otherwise, while slicing off bits per char, we can possibly straddle two
55+ // of bytes, so a more work is involved
56+ var slicesPerChunk = lcm ( bitsPerChar , BITS_PER_BYTE ) / BITS_PER_BYTE ;
57+ return function ( chunk , slice , bytes ) {
58+ var bNum = chunk * slicesPerChunk ;
59+
60+ var offset = slice * bitsPerChar / BITS_PER_BYTE ;
61+ var lOffset = floor ( offset ) ;
62+ var rOffset = ceil ( offset ) ;
63+
64+ var rShift = BITS_PER_BYTE - bitsPerChar ;
65+ var lShift = slice * bitsPerChar % BITS_PER_BYTE ;
66+
67+ var ndx = ( bytes [ bNum + lOffset ] << lShift & 0xff ) >> rShift ;
68+
69+ var r1Bits = ( rOffset + 1 ) * BITS_PER_BYTE ;
70+ var s1Bits = ( slice + 1 ) * bitsPerChar ;
71+
72+ var rShiftIt = ( r1Bits - s1Bits ) % BITS_PER_BYTE ;
73+ if ( rShift < rShiftIt ) {
74+ ndx += bytes [ bNum + rOffset ] >> rShiftIt ;
75+ }
76+ return ndx ;
77+ } ;
78+ } ;
79+
80+ var charsetProps = new WeakMap ( ) ;
81+
82+ var CharSet = function ( ) {
83+ function CharSet ( chars ) {
84+ ( 0 , _classCallCheck3 . default ) ( this , CharSet ) ;
85+
86+ if ( ! ( typeof chars === 'string' || chars instanceof String ) ) {
87+ throw new Error ( 'Invalid chars: Must be string' ) ;
88+ }
89+ var length = chars . length ;
90+
91+ if ( ! [ 2 , 4 , 8 , 16 , 32 , 64 ] . includes ( length ) ) {
92+ throw new Error ( 'Invalid char count: must be one of 2,4,8,16,32,64' ) ;
93+ }
94+ var bitsPerChar = floor ( log2 ( length ) ) ;
95+ // Ensure no repeated characters
96+ for ( var i = 0 ; i < length ; i += 1 ) {
97+ var c = chars . charAt ( i ) ;
98+ for ( var j = i + 1 ; j < length ; j += 1 ) {
99+ if ( c === chars . charAt ( j ) ) {
100+ throw new Error ( 'Characters not unique' ) ;
101+ }
102+ }
103+ }
104+ var privProps = {
105+ chars : chars ,
106+ bitsPerChar : bitsPerChar ,
107+ length : length ,
108+ ndxFn : genNdxFn ( bitsPerChar ) ,
109+ charsPerChunk : lcm ( bitsPerChar , BITS_PER_BYTE ) / bitsPerChar
110+ } ;
111+ charsetProps . set ( this , privProps ) ;
112+ }
113+
114+ ( 0 , _createClass3 . default ) ( CharSet , [ {
115+ key : 'getChars' ,
116+ value : function getChars ( ) {
117+ return charsetProps . get ( this ) . chars ;
118+ }
119+ } , {
120+ key : 'getBitsPerChar' ,
121+ value : function getBitsPerChar ( ) {
122+ return charsetProps . get ( this ) . bitsPerChar ;
123+ }
124+ } , {
125+ key : 'getNdxFn' ,
126+ value : function getNdxFn ( ) {
127+ return charsetProps . get ( this ) . ndxFn ;
128+ }
129+ } , {
130+ key : 'getCharsPerChunk' ,
131+ value : function getCharsPerChunk ( ) {
132+ return charsetProps . get ( this ) . charsPerChunk ;
133+ }
134+ } , {
135+ key : 'length' ,
136+ value : function length ( ) {
137+ return charsetProps . get ( this ) . length ;
138+ }
139+ } , {
140+ key : 'bytesNeeded' ,
141+ value : function bytesNeeded ( bitLen ) {
142+ var count = ceil ( bitLen / this . bitsPerChar ( ) ) ;
143+ return ceil ( count * this . bitsPerChar ( ) / BITS_PER_BYTE ) ;
144+ }
145+
146+ // Aliases
147+
148+ } , {
149+ key : 'chars' ,
150+ value : function chars ( ) {
151+ return this . getChars ( ) ;
152+ }
153+ } , {
154+ key : 'ndxFn' ,
155+ value : function ndxFn ( ) {
156+ return this . getNdxFn ( ) ;
157+ }
158+ } , {
159+ key : 'bitsPerChar' ,
160+ value : function bitsPerChar ( ) {
161+ return this . getBitsPerChar ( ) ;
162+ }
163+ } ] ) ;
164+ return CharSet ;
165+ } ( ) ;
166+
167+ var charset64 = new CharSet ( 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_' ) ;
168+ var charset32 = new CharSet ( '2346789bdfghjmnpqrtBDFGHJLMNPQRT' ) ;
169+ var charset16 = new CharSet ( '0123456789abcdef' ) ;
170+ var charset8 = new CharSet ( '01234567' ) ;
171+ var charset4 = new CharSet ( 'ATCG' ) ;
172+ var charset2 = new CharSet ( '01' ) ;
173+
44174var endianByteNum = function ( ) {
45175 var buf32 = new Uint32Array ( 1 ) ;
46176 var buf8 = new Uint8Array ( buf32 . buffer ) ;
@@ -89,6 +219,12 @@ var csprngBytes = function csprngBytes(count) {
89219 return Buffer . from ( Crypto . randomBytes ( count ) ) ;
90220} ;
91221
222+ // const csprngBytes = count => (
223+ // process.browser ?
224+ // window.crypto.getRandomValues(new Uint8Array(count)) :
225+ // Buffer.from(Crypto.randomBytes(count))
226+ // )
227+
92228var prngBytes = function prngBytes ( count ) {
93229 var BYTES_USED_PER_RANDOM_CALL = 6 ;
94230 var randCount = ceil ( count / BYTES_USED_PER_RANDOM_CALL ) ;
@@ -121,10 +257,12 @@ var entropyBits = function entropyBits(total, risk) {
121257 return N + log2 ( risk ) - 1 ;
122258} ;
123259
124- var _class = function ( ) {
125- function _class ( ) {
260+ var entropyProps = new WeakMap ( ) ;
261+
262+ var Entropy = function ( ) {
263+ function Entropy ( ) {
126264 var params = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : { bits : 128 , charset : charset32 } ;
127- ( 0 , _classCallCheck3 . default ) ( this , _class ) ;
265+ ( 0 , _classCallCheck3 . default ) ( this , Entropy ) ;
128266
129267 if ( params !== undefined ) {
130268 if ( ! ( params instanceof Object ) ) {
@@ -197,87 +335,87 @@ var _class = function () {
197335
198336 var prng = params . prng || false ;
199337
200- propMap . set ( this , { charset : charset , bitLen : bitLen , prng : prng } ) ;
338+ entropyProps . set ( this , { charset : charset , bitLen : bitLen , prng : prng } ) ;
201339 }
202340
203- ( 0 , _createClass3 . default ) ( _class , [ {
341+ ( 0 , _createClass3 . default ) ( Entropy , [ {
204342 key : 'smallID' ,
205343 value : function smallID ( ) {
206- var charset = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : propMap . get ( this ) . charset ;
344+ var charset = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : entropyProps . get ( this ) . charset ;
207345
208346 return this . string ( 29 , charset ) ;
209347 }
210348 } , {
211349 key : 'mediumID' ,
212350 value : function mediumID ( ) {
213- var charset = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : propMap . get ( this ) . charset ;
351+ var charset = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : entropyProps . get ( this ) . charset ;
214352
215353 return this . string ( 69 , charset ) ;
216354 }
217355 } , {
218356 key : 'largeID' ,
219357 value : function largeID ( ) {
220- var charset = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : propMap . get ( this ) . charset ;
358+ var charset = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : entropyProps . get ( this ) . charset ;
221359
222360 return this . string ( 99 , charset ) ;
223361 }
224362 } , {
225363 key : 'sessionID' ,
226364 value : function sessionID ( ) {
227- var charset = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : propMap . get ( this ) . charset ;
365+ var charset = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : entropyProps . get ( this ) . charset ;
228366
229367 return this . string ( 128 , charset ) ;
230368 }
231369 } , {
232370 key : 'token' ,
233371 value : function token ( ) {
234- var charset = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : propMap . get ( this ) . charset ;
372+ var charset = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : entropyProps . get ( this ) . charset ;
235373
236374 return this . string ( 256 , charset ) ;
237375 }
238376 } , {
239377 key : 'string' ,
240378 value : function string ( ) {
241- var bitLen = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : propMap . get ( this ) . bitLen ;
242- var charset = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : propMap . get ( this ) . charset ;
379+ var bitLen = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : entropyProps . get ( this ) . bitLen ;
380+ var charset = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : entropyProps . get ( this ) . charset ;
243381
244382 var bytesNeeded = charset . bytesNeeded ( bitLen ) ;
245- var bytes = propMap . get ( this ) . prng ? prngBytes ( bytesNeeded ) : csprngBytes ( bytesNeeded ) ;
383+ var bytes = entropyProps . get ( this ) . prng ? prngBytes ( bytesNeeded ) : csprngBytes ( bytesNeeded ) ;
246384 return this . stringWithBytes ( bytes , bitLen , charset ) ;
247385 }
248386 } , {
249387 key : 'stringWithBytes' ,
250388 value : function stringWithBytes ( bytes ) {
251- var bitLen = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : propMap . get ( this ) . bitLen ;
252- var charset = arguments . length > 2 && arguments [ 2 ] !== undefined ? arguments [ 2 ] : propMap . get ( this ) . charset ;
389+ var bitLen = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : entropyProps . get ( this ) . bitLen ;
390+ var charset = arguments . length > 2 && arguments [ 2 ] !== undefined ? arguments [ 2 ] : entropyProps . get ( this ) . charset ;
253391
254392 return _stringWithBytes ( bytes , bitLen , charset ) ;
255393 }
256394 } , {
257395 key : 'bytesNeeded' ,
258396 value : function bytesNeeded ( ) {
259- var bitLen = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : propMap . get ( this ) . bitLen ;
260- var charset = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : propMap . get ( this ) . charset ;
397+ var bitLen = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : entropyProps . get ( this ) . bitLen ;
398+ var charset = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : entropyProps . get ( this ) . charset ;
261399
262400 return charset . bytesNeeded ( bitLen ) ;
263401 }
264402 } , {
265403 key : 'chars' ,
266404 value : function chars ( ) {
267- return propMap . get ( this ) . charset . chars ( ) ;
405+ return entropyProps . get ( this ) . charset . chars ( ) ;
268406 }
269407 } , {
270408 key : 'bits' ,
271409 value : function bits ( ) {
272- return propMap . get ( this ) . bitLen ;
410+ return entropyProps . get ( this ) . bitLen ;
273411 }
274412 } , {
275413 key : 'use' ,
276414 value : function use ( charset ) {
277415 if ( ! ( charset instanceof CharSet ) ) {
278416 throw new Error ( 'Invalid CharSet' ) ;
279417 }
280- propMap . get ( this ) . charset = charset ;
418+ entropyProps . get ( this ) . charset = charset ;
281419 }
282420 } , {
283421 key : 'useChars' ,
@@ -293,7 +431,16 @@ var _class = function () {
293431 return entropyBits ( total , risk ) ;
294432 }
295433 } ] ) ;
296- return _class ;
434+ return Entropy ;
297435} ( ) ;
298436
299- exports . default = _class ;
437+ module . exports = {
438+ CharSet : CharSet ,
439+ Entropy : Entropy ,
440+ charset2 : charset2 ,
441+ charset4 : charset4 ,
442+ charset8 : charset8 ,
443+ charset16 : charset16 ,
444+ charset32 : charset32 ,
445+ charset64 : charset64
446+ } ;
0 commit comments