Skip to content

Commit e310537

Browse files
committed
Refactor
entropyPerChar -> bitsPerChar
1 parent ad0d723 commit e310537

File tree

3 files changed

+29
-29
lines changed

3 files changed

+29
-29
lines changed

lib/charSet.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ class CharSet {
44
constructor(chars, ndxFn) {
55
this.chars = chars
66
this.ndxFn = ndxFn
7-
this.entropyPerChar = Math.floor(Math.log2(chars.length))
8-
if (this.entropyPerChar != Math.log2(chars.length)) {
7+
this.bitsPerChar = Math.floor(Math.log2(chars.length))
8+
if (this.bitsPerChar != Math.log2(chars.length)) {
99
throw new Error('EntropyString only supports CharSets with a power of 2 characters')
1010
}
11-
this.charsPerChunk = lcm(this.entropyPerChar, 8) / this.entropyPerChar
11+
this.charsPerChunk = lcm(this.bitsPerChar, 8) / this.bitsPerChar
1212
}
1313

1414
use(chars) {
@@ -54,28 +54,28 @@ const _ndx2 = (chunk, slice, bytes) => {
5454
return _ndxDiv(chunk, slice, bytes, 1)
5555
}
5656

57-
const _ndxGen = (chunk, slice, bytes, entropyPerChar) => {
57+
const _ndxGen = (chunk, slice, bytes, bitsPerChar) => {
5858
let bitsPerByte = 8
59-
let slicesPerChunk = lcm(entropyPerChar, bitsPerByte) / bitsPerByte
59+
let slicesPerChunk = lcm(bitsPerChar, bitsPerByte) / bitsPerByte
6060
let bNum = chunk * slicesPerChunk
6161

62-
let rShift = bitsPerByte - entropyPerChar
63-
let lOffset = Math.floor((slice*entropyPerChar)/bitsPerByte)
64-
let lShift = (slice*entropyPerChar) % bitsPerByte
62+
let rShift = bitsPerByte - bitsPerChar
63+
let lOffset = Math.floor((slice*bitsPerChar)/bitsPerByte)
64+
let lShift = (slice*bitsPerChar) % bitsPerByte
6565

6666
let ndx = ((bytes[bNum+lOffset]<<lShift)&0xff)>>rShift
6767

68-
let rOffset = Math.ceil((slice*entropyPerChar)/bitsPerByte)
69-
let rShiftIt = ((rOffset+1)*bitsPerByte - (slice+1)*entropyPerChar) % bitsPerByte
68+
let rOffset = Math.ceil((slice*bitsPerChar)/bitsPerByte)
69+
let rShiftIt = ((rOffset+1)*bitsPerByte - (slice+1)*bitsPerChar) % bitsPerByte
7070
if (rShift < rShiftIt) {
7171
ndx += bytes[bNum+rOffset]>>rShiftIt
7272
}
7373
return ndx
7474
}
7575

76-
const _ndxDiv = (chunk, slice, bytes, entropyPerChar) => {
77-
let lShift = entropyPerChar
78-
let rShift = 8 - entropyPerChar
76+
const _ndxDiv = (chunk, slice, bytes, bitsPerChar) => {
77+
let lShift = bitsPerChar
78+
let rShift = 8 - bitsPerChar
7979
return ((bytes[chunk]<<(lShift*slice))&0xff)>>rShift
8080
}
8181

lib/entropy.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ const stringWithBytes = (entropy, charSet, bytes) => {
6767
}
6868
if (entropy <= 0) { return '' }
6969

70-
const count = Math.ceil(entropy / charSet.entropyPerChar)
70+
const count = Math.ceil(entropy / charSet.bitsPerChar)
7171
if (count <= 0) { return '' }
7272

73-
const needed = Math.ceil(count * (charSet.entropyPerChar / _bitsPerByte))
73+
const needed = Math.ceil(count * (charSet.bitsPerChar / _bitsPerByte))
7474
if (bytes.length < needed) {
7575
throw new Error('Insufficient bytes')
7676
}
@@ -94,10 +94,10 @@ const stringWithBytes = (entropy, charSet, bytes) => {
9494

9595
const bytesNeeded = (entropy, charSet) => {
9696
if (!CharSet.isValid(charSet)) { throw new Error('Invalid CharSet') }
97-
const count = Math.ceil(entropy / charSet.entropyPerChar)
97+
const count = Math.ceil(entropy / charSet.bitsPerChar)
9898
if (count <= 0) { return 0 }
9999

100-
const bytesPerSlice = charSet.entropyPerChar / _bitsPerByte
100+
const bytesPerSlice = charSet.bitsPerChar / _bitsPerByte
101101
return Math.ceil(count * bytesPerSlice)
102102
}
103103

test/charSet.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,48 @@ import CharSet from '../lib/charSet';
55
test('char set 64', t => {
66
const charSet = CharSet.charSet64
77
t.is(charSet.chars.length, 64)
8-
const entropyPerChar = Math.log2(charSet.chars.length)
9-
t.is(charSet.entropyPerChar, entropyPerChar)
8+
const bitsPerChar = Math.log2(charSet.chars.length)
9+
t.is(charSet.bitsPerChar, bitsPerChar)
1010
t.is(charSet.charsPerChunk, 4)
1111
})
1212

1313
test('char set 32', t => {
1414
const charSet = CharSet.charSet32
1515
t.is(charSet.chars.length, 32)
16-
const entropyPerChar = Math.log2(charSet.chars.length)
17-
t.is(charSet.entropyPerChar, entropyPerChar)
16+
const bitsPerChar = Math.log2(charSet.chars.length)
17+
t.is(charSet.bitsPerChar, bitsPerChar)
1818
t.is(charSet.charsPerChunk, 8)
1919
})
2020

2121
test('char set 16', t => {
2222
const charSet = CharSet.charSet16
2323
t.is(charSet.chars.length, 16)
24-
const entropyPerChar = Math.log2(charSet.chars.length)
25-
t.is(charSet.entropyPerChar, entropyPerChar)
24+
const bitsPerChar = Math.log2(charSet.chars.length)
25+
t.is(charSet.bitsPerChar, bitsPerChar)
2626
t.is(charSet.charsPerChunk, 2)
2727
})
2828

2929
test('char set 8', t => {
3030
const charSet = CharSet.charSet8
3131
t.is(charSet.chars.length, 8)
32-
const entropyPerChar = Math.log2(charSet.chars.length)
33-
t.is(charSet.entropyPerChar, entropyPerChar)
32+
const bitsPerChar = Math.log2(charSet.chars.length)
33+
t.is(charSet.bitsPerChar, bitsPerChar)
3434
t.is(charSet.charsPerChunk, 8)
3535
})
3636

3737
test('char set 4', t => {
3838
const charSet = CharSet.charSet4
3939
t.is(charSet.chars.length, 4)
40-
const entropyPerChar = Math.log2(charSet.chars.length)
41-
t.is(charSet.entropyPerChar, entropyPerChar)
40+
const bitsPerChar = Math.log2(charSet.chars.length)
41+
t.is(charSet.bitsPerChar, bitsPerChar)
4242
t.is(charSet.charsPerChunk, 4)
4343
})
4444

4545
test('char set 2', t => {
4646
const charSet = CharSet.charSet2
4747
t.is(charSet.chars.length, 2)
48-
const entropyPerChar = Math.log2(charSet.chars.length)
49-
t.is(charSet.entropyPerChar, entropyPerChar)
48+
const bitsPerChar = Math.log2(charSet.chars.length)
49+
t.is(charSet.bitsPerChar, bitsPerChar)
5050
t.is(charSet.charsPerChunk, 8)
5151
})
5252

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