1919// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121// SOFTWARE.
22- import crypto from 'crypto'
23-
2422import encoder from './encoder'
2523import { entropyBitsPerChar } from './entropy'
2624
@@ -140,22 +138,7 @@ const fillEntropy = (entropyBits: EntropyBits, entropyFunction: EntropyFunction)
140138 }
141139}
142140
143- const selectEntropyFunction = ( puidConfig : PuidConfig ) : EntropyFunction => {
144- if ( puidConfig . entropyValues ) {
145- return [ true , puidConfig . entropyValues as EntropyByValues ]
146- } else if ( puidConfig . entropyBytes ) {
147- return [ false , puidConfig . entropyBytes as EntropyByBytes ]
148- } else {
149- return [ false , crypto . randomBytes ]
150- // [true, crypto.getRandomValues]
151- }
152- }
153-
154- export default ( puidLen : number , puidChars : string , puidConfig : PuidConfig ) : PuidBitsMuncherResult => {
155- if ( puidConfig . entropyBytes && puidConfig . entropyValues ) {
156- return { error : new Error ( 'Cannot specify both entropyBytes and entropyValues functions' ) }
157- }
158-
141+ export default ( puidLen : number , puidChars : string , entropyFunction : EntropyFunction ) : PuidBitsMuncherResult => {
159142 const nBitsPerChar = bitsPerChar ( puidChars )
160143 const nBitsPerPuid = nBitsPerChar * puidLen
161144 const nBytesPerPuid = ceil ( nBitsPerPuid / 8 )
@@ -164,12 +147,13 @@ export default (puidLen: number, puidChars: string, puidConfig: PuidConfig): Pui
164147 const entropyBuffer = new ArrayBuffer ( bufferLen )
165148 const entropyBits : EntropyBits = [ 8 * bufferLen , entropyBuffer ]
166149 const entropyBytes = new Uint8Array ( entropyBuffer )
167- const entropyFunction = selectEntropyFunction ( puidConfig )
168150
169151 const charsEncoder = encoder ( puidChars )
170152 const puidEncoded = ( value : number ) => String . fromCharCode ( charsEncoder ( value ) )
171153
172- if ( isPow2 ( puidChars . length ) ) {
154+ const nChars = puidChars . length
155+
156+ if ( isPow2 ( nChars ) ) {
173157 // When chars count is a power of 2, sliced bits always yield a valid char
174158 const mapper = new Array ( puidLen ) . fill ( 0 ) . map ( ( zero , ndx ) => zero + ndx )
175159
@@ -187,7 +171,7 @@ export default (puidLen: number, puidChars: string, puidConfig: PuidConfig): Pui
187171 return { success : bitsMuncher }
188172 }
189173
190- const nChars = puidChars . length
174+
191175 const nEntropyBits = 8 * entropyBytes . length
192176 const puidShifts = bitShifts ( puidChars )
193177
0 commit comments