Skip to content

Commit 1a5945c

Browse files
committed
Use charSet bytesNeeded
1 parent 851f837 commit 1a5945c

File tree

1 file changed

+19
-26
lines changed

1 file changed

+19
-26
lines changed

lib/random.js

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import WeakMap from 'weak-map'
55

66
const propMap = new WeakMap()
77

8+
const BITS_PER_BYTE = 8
9+
810
export default class {
911
constructor(arg) {
1012
let charSet
@@ -31,19 +33,21 @@ export default class {
3133
}
3234

3335
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)
3538
}
3639

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)
3943
}
4044

4145
stringWithBytes(entropyBits, bytes, charSet = propMap.get(this).charSet) {
4246
return _stringWithBytes(entropyBits, bytes, charSet)
4347
}
4448

4549
bytesNeeded(entropyBits, charSet = propMap.get(this).charSet) {
46-
return _bytesNeeded(entropyBits, charSet)
50+
return charSet.bytesNeeded(entropyBits)
4751
}
4852

4953
chars() {
@@ -63,16 +67,14 @@ export default class {
6367
}
6468
}
6569

66-
const _bitsPerByte = 8
67-
6870
const _stringWithBytes = (entropyBits, bytes, charSet) => {
6971
if (entropyBits <= 0) { return '' }
7072

7173
const bitsPerChar = charSet.getBitsPerChar()
7274
const count = Math.ceil(entropyBits / bitsPerChar)
7375
if (count <= 0) { return '' }
7476

75-
const needed = Math.ceil(count * (bitsPerChar / _bitsPerByte))
77+
const needed = Math.ceil(count * (bitsPerChar / BITS_PER_BYTE))
7678
if (bytes.length < needed) {
7779
throw new Error('Insufficient bytes: need ' + needed + ' and got ' + bytes.length)
7880
}
@@ -98,32 +100,23 @@ const _stringWithBytes = (entropyBits, bytes, charSet) => {
98100
return string
99101
}
100102

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) => {
111104
const crypto = require('crypto')
112-
return Buffer.from(crypto.randomBytes(_bytesNeeded(entropyBits, charSet)))
105+
return Buffer.from(crypto.randomBytes(bytesNeeded))
113106
}
114107

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)
118111

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))
121114
for (let rNum = 0; rNum < randCount; rNum++) {
122115
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++) {
124117
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) {
127120
buffer[bByteNum] = dataView.getUint8(fByteNum)
128121
}
129122
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy