1
1
import CharSet from './charSet'
2
2
import lcm from './lcm'
3
3
4
- const log2 = Math . log2
5
- const log10 = Math . log10
6
- const log2_10 = log2 ( 10 )
4
+ const _log2 = Math . log2
5
+ const _log10 = Math . log10
6
+ const _log2_10 = _log2 ( 10 )
7
7
8
- const endianByteNum = ( ( ) => {
8
+ const _endianByteNum = ( ( ) => {
9
9
const buf32 = new Uint32Array ( 1 )
10
10
const buf8 = new Uint8Array ( buf32 . buffer )
11
11
buf32 [ 0 ] = 0xff
@@ -17,11 +17,11 @@ const bits = (total, risk) => {
17
17
18
18
let N = 0
19
19
if ( total < 10001 ) {
20
- N = log2 ( total ) + log2 ( total - 1 ) + ( log2_10 * log10 ( risk ) ) - 1
20
+ N = _log2 ( total ) + _log2 ( total - 1 ) + ( _log2_10 * _log10 ( risk ) ) - 1
21
21
}
22
22
else {
23
- const n = 2 * log10 ( total ) + log10 ( risk )
24
- N = n * log2_10 - 1
23
+ const n = 2 * _log10 ( total ) + _log10 ( risk )
24
+ N = n * _log2_10 - 1
25
25
}
26
26
return N
27
27
}
@@ -31,11 +31,11 @@ const bitsWithRiskPower = (total, rPower) => {
31
31
32
32
let N = 0
33
33
if ( total < 10001 ) {
34
- N = log2 ( total ) + log2 ( total - 1 ) + ( log2_10 * rPower ) - 1
34
+ N = _log2 ( total ) + _log2 ( total - 1 ) + ( _log2_10 * rPower ) - 1
35
35
}
36
36
else {
37
- const n = 2 * log10 ( total ) + rPower
38
- N = n * log2_10 - 1
37
+ const n = 2 * _log10 ( total ) + rPower
38
+ N = n * _log2_10 - 1
39
39
}
40
40
return N
41
41
}
@@ -47,13 +47,13 @@ const bitsWithPowers = (tPower, rPower) => {
47
47
}
48
48
else {
49
49
const n = 2 * tPower + rPower
50
- N = n * log2_10 - 1
50
+ N = n * _log2_10 - 1
51
51
}
52
52
return N
53
53
}
54
54
55
55
const randomString = ( entropy , charSet , crypto = true ) => {
56
- const bytes = crypto ? cryptoBytes ( entropy , charSet ) : randomBytes ( entropy , charSet )
56
+ const bytes = crypto ? _cryptoBytes ( entropy , charSet ) : _randomBytes ( entropy , charSet )
57
57
return randomStringWithBytes ( entropy , charSet , bytes )
58
58
}
59
59
@@ -78,27 +78,27 @@ const randomStringWithBytes = (entropy, charSet, bytes) => {
78
78
switch ( charSet ) {
79
79
case CharSet . charSet64 :
80
80
chars = CharSet . charSet64 . chars
81
- ndxFn = ndx64
81
+ ndxFn = _ndx64
82
82
break
83
83
case CharSet . charSet32 :
84
84
chars = CharSet . charSet32 . chars
85
- ndxFn = ndx32
85
+ ndxFn = _ndx32
86
86
break
87
87
case CharSet . charSet16 :
88
88
chars = CharSet . charSet16 . chars
89
- ndxFn = ndx16
89
+ ndxFn = _ndx16
90
90
break
91
91
case CharSet . charSet8 :
92
92
chars = CharSet . charSet8 . chars
93
- ndxFn = ndx8
93
+ ndxFn = _ndx8
94
94
break
95
95
case CharSet . charSet4 :
96
96
chars = CharSet . charSet4 . chars
97
- ndxFn = ndx4
97
+ ndxFn = _ndx4
98
98
break
99
99
case CharSet . charSet2 :
100
100
chars = CharSet . charSet2 . chars
101
- ndxFn = ndx2
101
+ ndxFn = _ndx2
102
102
break
103
103
default :
104
104
break
@@ -127,12 +127,12 @@ const bytesNeeded = (entropy, charSet) => {
127
127
return Math . ceil ( count * bytesPerSlice )
128
128
}
129
129
130
- const cryptoBytes = ( entropy , charSet ) => {
130
+ const _cryptoBytes = ( entropy , charSet ) => {
131
131
const crypto = require ( 'crypto' )
132
132
return Buffer . from ( crypto . randomBytes ( bytesNeeded ( entropy , charSet ) ) )
133
133
}
134
134
135
- const randomBytes = ( entropy , charSet ) => {
135
+ const _randomBytes = ( entropy , charSet ) => {
136
136
const byteCount = bytesNeeded ( entropy , charSet )
137
137
const randCount = Math . ceil ( byteCount / 6 )
138
138
@@ -141,45 +141,45 @@ const randomBytes = (entropy, charSet) => {
141
141
for ( let rNum = 0 ; rNum < randCount ; rNum ++ ) {
142
142
dataView . setFloat64 ( 0 , Math . random ( ) )
143
143
for ( let n = 0 ; n < 6 ; n ++ ) {
144
- let fByteNum = endianByteNum [ n ]
144
+ let fByteNum = _endianByteNum [ n ]
145
145
let bByteNum = 6 * rNum + n
146
- bufferByte ( buffer , bByteNum , fByteNum , byteCount , dataView )
146
+ _bufferByte ( buffer , bByteNum , fByteNum , byteCount , dataView )
147
147
}
148
148
}
149
149
return buffer
150
150
}
151
151
152
- const bufferByte = ( buffer , bByte , nByte , byteCount , dataView ) => {
152
+ const _bufferByte = ( buffer , bByte , nByte , byteCount , dataView ) => {
153
153
if ( bByte < byteCount ) {
154
154
buffer [ bByte ] = dataView . getUint8 ( nByte )
155
155
}
156
156
}
157
157
158
- const ndx64 = ( chunk , slice , bytes ) => {
159
- return ndxGen ( chunk , slice , bytes , 6 )
158
+ const _ndx64 = ( chunk , slice , bytes ) => {
159
+ return _ndxGen ( chunk , slice , bytes , 6 )
160
160
}
161
161
162
- const ndx32 = ( chunk , slice , bytes ) => {
163
- return ndxGen ( chunk , slice , bytes , 5 )
162
+ const _ndx32 = ( chunk , slice , bytes ) => {
163
+ return _ndxGen ( chunk , slice , bytes , 5 )
164
164
}
165
165
166
- const ndx16 = ( chunk , slice , bytes ) => {
166
+ const _ndx16 = ( chunk , slice , bytes ) => {
167
167
return ( ( bytes [ chunk ] << ( 4 * slice ) ) & 0xff ) >> 4
168
168
}
169
169
170
- const ndx8 = ( chunk , slice , bytes ) => {
171
- return ndxGen ( chunk , slice , bytes , 3 )
170
+ const _ndx8 = ( chunk , slice , bytes ) => {
171
+ return _ndxGen ( chunk , slice , bytes , 3 )
172
172
}
173
173
174
- const ndx4 = ( chunk , slice , bytes ) => {
174
+ const _ndx4 = ( chunk , slice , bytes ) => {
175
175
return ( ( bytes [ chunk ] << ( 2 * slice ) ) & 0xff ) >> 6
176
176
}
177
177
178
- const ndx2 = ( chunk , slice , bytes ) => {
178
+ const _ndx2 = ( chunk , slice , bytes ) => {
179
179
return ( ( bytes [ chunk ] << slice ) & 0xff ) >> 7
180
180
}
181
181
182
- const ndxGen = ( chunk , slice , bytes , bitsPerSlice ) => {
182
+ const _ndxGen = ( chunk , slice , bytes , bitsPerSlice ) => {
183
183
let bitsPerByte = 8
184
184
let slicesPerChunk = lcm ( bitsPerSlice , bitsPerByte ) / bitsPerByte
185
185
0 commit comments