@@ -5,6 +5,8 @@ import WeakMap from 'weak-map'
5
5
6
6
const propMap = new WeakMap ( )
7
7
8
+ const BITS_PER_BYTE = 8
9
+
8
10
export default class {
9
11
constructor ( arg ) {
10
12
let charSet
@@ -31,19 +33,21 @@ export default class {
31
33
}
32
34
33
35
string ( entropyBits , charSet = propMap . get ( this ) . charSet ) {
34
- return this . stringWithBytes ( entropyBits , _cryptoBytes ( entropyBits , charSet ) , charSet )
36
+ let bytesNeeded = charSet . bytesNeeded ( entropyBits )
37
+ return this . stringWithBytes ( entropyBits , _cryptoBytes ( bytesNeeded ) , charSet )
35
38
}
36
39
37
- stringRandom ( entropyBits , charSet = propMap . get ( this ) . charSet ) {
38
- return this . stringWithBytes ( entropyBits , _randomBytes ( entropyBits , charSet ) , charSet )
40
+ stringRandom ( entropyBits , charSet = propMap . get ( this ) . charSet ) {
41
+ let bytesNeeded = charSet . bytesNeeded ( entropyBits )
42
+ return this . stringWithBytes ( entropyBits , _randomBytes ( bytesNeeded ) , charSet )
39
43
}
40
44
41
45
stringWithBytes ( entropyBits , bytes , charSet = propMap . get ( this ) . charSet ) {
42
46
return _stringWithBytes ( entropyBits , bytes , charSet )
43
47
}
44
48
45
49
bytesNeeded ( entropyBits , charSet = propMap . get ( this ) . charSet ) {
46
- return _bytesNeeded ( entropyBits , charSet )
50
+ return charSet . bytesNeeded ( entropyBits )
47
51
}
48
52
49
53
chars ( ) {
@@ -63,16 +67,14 @@ export default class {
63
67
}
64
68
}
65
69
66
- const _bitsPerByte = 8
67
-
68
70
const _stringWithBytes = ( entropyBits , bytes , charSet ) => {
69
71
if ( entropyBits <= 0 ) { return '' }
70
72
71
73
const bitsPerChar = charSet . getBitsPerChar ( )
72
74
const count = Math . ceil ( entropyBits / bitsPerChar )
73
75
if ( count <= 0 ) { return '' }
74
76
75
- const needed = Math . ceil ( count * ( bitsPerChar / _bitsPerByte ) )
77
+ const needed = Math . ceil ( count * ( bitsPerChar / BITS_PER_BYTE ) )
76
78
if ( bytes . length < needed ) {
77
79
throw new Error ( 'Insufficient bytes: need ' + needed + ' and got ' + bytes . length )
78
80
}
@@ -98,32 +100,23 @@ const _stringWithBytes = (entropyBits, bytes, charSet) => {
98
100
return string
99
101
}
100
102
101
- const _bytesNeeded = ( entropyBits , charSet ) => {
102
- const bitsPerChar = charSet . getBitsPerChar ( )
103
- const count = Math . ceil ( entropyBits / bitsPerChar )
104
- if ( count <= 0 ) { return 0 }
105
-
106
- const bytesPerSlice = bitsPerChar / _bitsPerByte
107
- return Math . ceil ( count * bytesPerSlice )
108
- }
109
-
110
- const _cryptoBytes = ( entropyBits , charSet ) => {
103
+ const _cryptoBytes = ( bytesNeeded ) => {
111
104
const crypto = require ( 'crypto' )
112
- return Buffer . from ( crypto . randomBytes ( _bytesNeeded ( entropyBits , charSet ) ) )
105
+ return Buffer . from ( crypto . randomBytes ( bytesNeeded ) )
113
106
}
114
107
115
- const _randomBytes = ( entropyBits , charSet ) => {
116
- const byteCount = _bytesNeeded ( entropyBits , charSet )
117
- const randCount = Math . ceil ( byteCount / 6 )
108
+ const _randomBytes = ( bytesNeeded ) => {
109
+ let BYTES_USED_PER_RANDOM_CALL = 6
110
+ const randCount = Math . ceil ( bytesNeeded / BYTES_USED_PER_RANDOM_CALL )
118
111
119
- const buffer = new Buffer ( byteCount )
120
- var dataView = new DataView ( new ArrayBuffer ( _bitsPerByte ) )
112
+ const buffer = new Buffer ( bytesNeeded )
113
+ var dataView = new DataView ( new ArrayBuffer ( BITS_PER_BYTE ) )
121
114
for ( let rNum = 0 ; rNum < randCount ; rNum ++ ) {
122
115
dataView . setFloat64 ( 0 , Math . random ( ) )
123
- for ( let n = 0 ; n < 6 ; n ++ ) {
116
+ for ( let n = 0 ; n < BYTES_USED_PER_RANDOM_CALL ; n ++ ) {
124
117
let fByteNum = _endianByteNum [ n ]
125
- let bByteNum = 6 * rNum + n
126
- if ( bByteNum < byteCount ) {
118
+ let bByteNum = rNum * BYTES_USED_PER_RANDOM_CALL + n
119
+ if ( bByteNum < bytesNeeded ) {
127
120
buffer [ bByteNum ] = dataView . getUint8 ( fByteNum )
128
121
}
129
122
}
0 commit comments