Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit81625fb

Browse files
committed
Refactor
1 parent1354189 commit81625fb

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

‎lib/charset.js‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
constWeakMap=require('weak-map')
22

33
constpropMap=newWeakMap()
4-
54
constBITS_PER_BYTE=8
5+
const{
6+
abs, ceil, floor, log2
7+
}=Math
68

79
constgcd=(a,b)=>{
810
letla=a
911
letlb=b
1012
while(lb!==0){
1113
[la,lb]=[lb,la%lb]
1214
}
13-
returnMath.abs(la)
15+
returnabs(la)
1416
}
1517
constlcm=(a,b)=>(a/gcd(a,b))*b
1618

@@ -24,16 +26,16 @@ const genNdxFn = (bitsPerChar) => {
2426
return((bytes[chunk]<<(lShift*slice))&0xff)>>rShift
2527
}
2628
}
27-
// Otherwise, while slicing off bits per char, we will possibly straddle a couple
28-
// of bytes, so a bit more work is involved
2929

30+
// Otherwise, while slicing off bits per char, we can possibly straddle two
31+
// of bytes, so a more work is involved
3032
constslicesPerChunk=lcm(bitsPerChar,BITS_PER_BYTE)/BITS_PER_BYTE
3133
return(chunk,slice,bytes)=>{
3234
constbNum=chunk*slicesPerChunk
3335

3436
constoffset=(slice*bitsPerChar)/BITS_PER_BYTE
35-
constlOffset=Math.floor(offset)
36-
constrOffset=Math.ceil(offset)
37+
constlOffset=floor(offset)
38+
constrOffset=ceil(offset)
3739

3840
constrShift=BITS_PER_BYTE-bitsPerChar
3941
constlShift=(slice*bitsPerChar)%BITS_PER_BYTE
@@ -60,7 +62,7 @@ export default class CharSet {
6062
if(![2,4,8,16,32,64].includes(length)){
6163
thrownewError('Invalid char count: must be one of 2,4,8,16,32,64')
6264
}
63-
constbitsPerChar=Math.floor(Math.log2(length))
65+
constbitsPerChar=floor(log2(length))
6466
// Ensure no repeated characters
6567
for(leti=0;i<length;i+=1){
6668
constc=chars.charAt(i)
@@ -101,8 +103,8 @@ export default class CharSet {
101103
}
102104

103105
bytesNeeded(bitLen){
104-
constcount=Math.ceil(bitLen/this.bitsPerChar())
105-
returnMath.ceil((count*this.bitsPerChar())/BITS_PER_BYTE)
106+
constcount=ceil(bitLen/this.bitsPerChar())
107+
returnceil((count*this.bitsPerChar())/BITS_PER_BYTE)
106108
}
107109

108110
// Aliases

‎lib/entropy.js‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ export const charset4 = new CharSet('ATCG')
1111
exportconstcharset2=newCharSet('01')
1212

1313
constpropMap=newWeakMap()
14-
1514
constBITS_PER_BYTE=8
15+
const{
16+
ceil, floor, log2, random, round
17+
}=Math
1618

1719
constendianByteNum=(()=>{
1820
constbuf32=newUint32Array(1)
@@ -24,8 +26,6 @@ const endianByteNum = (() => {
2426
conststringWithBytes=(bytes,bitLen,charset)=>{
2527
if(bitLen<=0){return''}
2628

27-
const{ floor, ceil}=Math
28-
2929
constbitsPerChar=charset.getBitsPerChar()
3030
constcount=ceil(bitLen/bitsPerChar)
3131
if(count<=0){return''}
@@ -56,16 +56,16 @@ const stringWithBytes = (bytes, bitLen, charset) => {
5656
returnstring
5757
}
5858

59-
constcryptoBytes=count=>Buffer.from(Crypto.randomBytes(count))
59+
constcsprngBytes=count=>Buffer.from(Crypto.randomBytes(count))
6060

6161
constprngBytes=(count)=>{
6262
constBYTES_USED_PER_RANDOM_CALL=6
63-
constrandCount=Math.ceil(count/BYTES_USED_PER_RANDOM_CALL)
63+
constrandCount=ceil(count/BYTES_USED_PER_RANDOM_CALL)
6464

6565
constbuffer=Buffer.alloc(count)
6666
constdataView=newDataView(newArrayBuffer(BITS_PER_BYTE))
6767
for(letrNum=0;rNum<randCount;rNum+=1){
68-
dataView.setFloat64(0,Math.random())
68+
dataView.setFloat64(0,random())
6969
for(letn=0;n<BYTES_USED_PER_RANDOM_CALL;n+=1){
7070
constfByteNum=endianByteNum[n]
7171
constbByteNum=(rNum*BYTES_USED_PER_RANDOM_CALL)+n
@@ -79,7 +79,6 @@ const prngBytes = (count) => {
7979

8080
constentropyBits=(total,risk)=>{
8181
if(total===0){return0}
82-
const{ log2}=Math
8382
letN
8483
if(total<1000){
8584
N=log2(total)+log2(total-1)
@@ -147,7 +146,6 @@ export default class {
147146
}
148147

149148
letbitLen
150-
const{ round}=Math
151149
if(params.bits){
152150
bitLen=round(params.bits)
153151
}elseif(params.total&&params.risk){
@@ -195,7 +193,7 @@ export default class {
195193

196194
string(bitLen=propMap.get(this).bitLen,charset=propMap.get(this).charset){
197195
constbytesNeeded=charset.bytesNeeded(bitLen)
198-
constbytes=propMap.get(this).prng ?prngBytes(bytesNeeded) :cryptoBytes(bytesNeeded)
196+
constbytes=propMap.get(this).prng ?prngBytes(bytesNeeded) :csprngBytes(bytesNeeded)
199197
returnthis.stringWithBytes(bytes,bitLen,charset)
200198
}
201199

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp