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

Commitccbbbaa

Browse files
committed
Restructure files
1 parent8da2c49 commitccbbbaa

21 files changed

+231
-594
lines changed

‎charSet.js‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
constBITS_PER_BYTE=8
2+
const{
3+
abs, ceil, floor, log2
4+
}=Math
5+
6+
constgcd=(a,b)=>{
7+
letla=a
8+
letlb=b
9+
while(lb!==0){
10+
[la,lb]=[lb,la%lb]
11+
}
12+
returnabs(la)
13+
}
14+
constlcm=(a,b)=>(a/gcd(a,b))*b
15+
16+
constgenNdxFn=(bitsPerChar)=>{
17+
// If BITS_PER_BYTEs is a multiple of bitsPerChar, we can slice off an integer number
18+
// of chars per byte.
19+
if(lcm(bitsPerChar,BITS_PER_BYTE)===BITS_PER_BYTE){
20+
return(chunk,slice,bytes)=>{
21+
constlShift=bitsPerChar
22+
constrShift=BITS_PER_BYTE-bitsPerChar
23+
return((bytes[chunk]<<(lShift*slice))&0xff)>>rShift
24+
}
25+
}
26+
27+
// Otherwise, while slicing off bits per char, we can possibly straddle two
28+
// of bytes, so a more work is involved
29+
constslicesPerChunk=lcm(bitsPerChar,BITS_PER_BYTE)/BITS_PER_BYTE
30+
return(chunk,slice,bytes)=>{
31+
constbNum=chunk*slicesPerChunk
32+
33+
constoffset=(slice*bitsPerChar)/BITS_PER_BYTE
34+
constlOffset=floor(offset)
35+
constrOffset=ceil(offset)
36+
37+
constrShift=BITS_PER_BYTE-bitsPerChar
38+
constlShift=(slice*bitsPerChar)%BITS_PER_BYTE
39+
40+
letndx=((bytes[bNum+lOffset]<<lShift)&0xff)>>rShift
41+
42+
constr1Bits=(rOffset+1)*BITS_PER_BYTE
43+
consts1Bits=(slice+1)*bitsPerChar
44+
45+
constrShiftIt=(r1Bits-s1Bits)%BITS_PER_BYTE
46+
if(rShift<rShiftIt){
47+
ndx+=bytes[bNum+rOffset]>>rShiftIt
48+
}
49+
returnndx
50+
}
51+
}
52+
53+
classCharSet{
54+
constructor(chars){
55+
if(!(typeofchars==='string'||charsinstanceofString)){
56+
thrownewError('Invalid chars: Must be string')
57+
}
58+
59+
const{ length}=chars
60+
if(![2,4,8,16,32,64].includes(length)){
61+
thrownewError('Invalid char count: must be one of 2,4,8,16,32,64')
62+
}
63+
64+
// Ensure no repeated characters
65+
for(leti=0;i<length;i+=1){
66+
constc=chars.charAt(i)
67+
for(letj=i+1;j<length;j+=1){
68+
if(c===chars.charAt(j)){
69+
thrownewError('Characters not unique')
70+
}
71+
}
72+
}
73+
74+
this.chars=chars
75+
this.bitsPerChar=floor(log2(length))
76+
this.length=length
77+
this.ndxFn=genNdxFn(this.bitsPerChar)
78+
this.charsPerChunk=lcm(this.bitsPerChar,BITS_PER_BYTE)/this.bitsPerChar
79+
}
80+
81+
bytesNeeded(bitLen){
82+
constcount=ceil(bitLen/this.bitsPerChar)
83+
returnceil((count*this.bitsPerChar)/BITS_PER_BYTE)
84+
}
85+
}
86+
87+
module.exports={
88+
CharSet
89+
}

‎csprng-bytes-browser.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
constcsprngBytes=(count)=>window.crypto.getRandomValues(newUint8Array(count))
2+
3+
module.exports={
4+
csprngBytes
5+
}

‎lib/csprng-bytes.js‎renamed to ‎csprng-bytes.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
constCrypto=require('crypto')
22

3-
constcsprngBytes=count=>Buffer.from(Crypto.randomBytes(count))
3+
constcsprngBytes=(count)=>Buffer.from(Crypto.randomBytes(count))
44

55
module.exports={
66
csprngBytes

‎dist/charSet.js‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
"use strict";
2+
3+
var_interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");
4+
5+
var_classCallCheck2=_interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
6+
7+
var_createClass2=_interopRequireDefault(require("@babel/runtime/helpers/createClass"));
8+
9+
varBITS_PER_BYTE=8;
10+
varabs=Math.abs,
11+
ceil=Math.ceil,
12+
floor=Math.floor,
13+
log2=Math.log2;
14+
15+
vargcd=functiongcd(a,b){
16+
varla=a;
17+
varlb=b;
18+
19+
while(lb!==0){
20+
var_ref=[lb,la%lb];
21+
la=_ref[0];
22+
lb=_ref[1];
23+
}
24+
25+
returnabs(la);
26+
};
27+
28+
varlcm=functionlcm(a,b){
29+
returna/gcd(a,b)*b;
30+
};
31+
32+
vargenNdxFn=functiongenNdxFn(bitsPerChar){
33+
// If BITS_PER_BYTEs is a multiple of bitsPerChar, we can slice off an integer number
34+
// of chars per byte.
35+
if(lcm(bitsPerChar,BITS_PER_BYTE)===BITS_PER_BYTE){
36+
returnfunction(chunk,slice,bytes){
37+
varlShift=bitsPerChar;
38+
varrShift=BITS_PER_BYTE-bitsPerChar;
39+
return(bytes[chunk]<<lShift*slice&0xff)>>rShift;
40+
};
41+
}// Otherwise, while slicing off bits per char, we can possibly straddle two
42+
// of bytes, so a more work is involved
43+
44+
45+
varslicesPerChunk=lcm(bitsPerChar,BITS_PER_BYTE)/BITS_PER_BYTE;
46+
returnfunction(chunk,slice,bytes){
47+
varbNum=chunk*slicesPerChunk;
48+
varoffset=slice*bitsPerChar/BITS_PER_BYTE;
49+
varlOffset=floor(offset);
50+
varrOffset=ceil(offset);
51+
varrShift=BITS_PER_BYTE-bitsPerChar;
52+
varlShift=slice*bitsPerChar%BITS_PER_BYTE;
53+
varndx=(bytes[bNum+lOffset]<<lShift&0xff)>>rShift;
54+
varr1Bits=(rOffset+1)*BITS_PER_BYTE;
55+
vars1Bits=(slice+1)*bitsPerChar;
56+
varrShiftIt=(r1Bits-s1Bits)%BITS_PER_BYTE;
57+
58+
if(rShift<rShiftIt){
59+
ndx+=bytes[bNum+rOffset]>>rShiftIt;
60+
}
61+
62+
returnndx;
63+
};
64+
};
65+
66+
varCharSet=/*#__PURE__*/function(){
67+
functionCharSet(chars){
68+
(0,_classCallCheck2["default"])(this,CharSet);
69+
70+
if(!(typeofchars==='string'||charsinstanceofString)){
71+
thrownewError('Invalid chars: Must be string');
72+
}
73+
74+
varlength=chars.length;
75+
76+
if(![2,4,8,16,32,64].includes(length)){
77+
thrownewError('Invalid char count: must be one of 2,4,8,16,32,64');
78+
}// Ensure no repeated characters
79+
80+
81+
for(vari=0;i<length;i+=1){
82+
varc=chars.charAt(i);
83+
84+
for(varj=i+1;j<length;j+=1){
85+
if(c===chars.charAt(j)){
86+
thrownewError('Characters not unique');
87+
}
88+
}
89+
}
90+
91+
this.chars=chars;
92+
this.bitsPerChar=floor(log2(length));
93+
this.length=length;
94+
this.ndxFn=genNdxFn(this.bitsPerChar);
95+
this.charsPerChunk=lcm(this.bitsPerChar,BITS_PER_BYTE)/this.bitsPerChar;
96+
}
97+
98+
(0,_createClass2["default"])(CharSet,[{
99+
key:"bytesNeeded",
100+
value:functionbytesNeeded(bitLen){
101+
varcount=ceil(bitLen/this.bitsPerChar);
102+
returnceil(count*this.bitsPerChar/BITS_PER_BYTE);
103+
}
104+
}]);
105+
returnCharSet;
106+
}();
107+
108+
module.exports={
109+
CharSet:CharSet
110+
};

‎dist/entropy-string.js‎

Lines changed: 7 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -6,114 +6,20 @@ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/cl
66

77
var_createClass2=_interopRequireDefault(require("@babel/runtime/helpers/createClass"));
88

9-
var_require=require('./lib/csprng-bytes'),
9+
var_require=require('./csprng-bytes'),
1010
csprngBytes=_require.csprngBytes;
1111

12-
var_require2=require('./lib/prng-bytes'),
12+
var_require2=require('./prng-bytes'),
1313
prngBytes=_require2.prngBytes;
1414

15+
var_require3=require('./charSet'),
16+
CharSet=_require3.CharSet;
17+
1518
varBITS_PER_BYTE=8;
16-
varabs=Math.abs,
17-
ceil=Math.ceil,
19+
varceil=Math.ceil,
1820
floor=Math.floor,
1921
log2=Math.log2,
2022
round=Math.round;
21-
22-
vargcd=functiongcd(a,b){
23-
varla=a;
24-
varlb=b;
25-
26-
while(lb!==0){
27-
var_ref=[lb,la%lb];
28-
la=_ref[0];
29-
lb=_ref[1];
30-
}
31-
32-
returnabs(la);
33-
};
34-
35-
varlcm=functionlcm(a,b){
36-
returna/gcd(a,b)*b;
37-
};
38-
39-
vargenNdxFn=functiongenNdxFn(bitsPerChar){
40-
// If BITS_PER_BYTEs is a multiple of bitsPerChar, we can slice off an integer number
41-
// of chars per byte.
42-
if(lcm(bitsPerChar,BITS_PER_BYTE)===BITS_PER_BYTE){
43-
returnfunction(chunk,slice,bytes){
44-
varlShift=bitsPerChar;
45-
varrShift=BITS_PER_BYTE-bitsPerChar;
46-
return(bytes[chunk]<<lShift*slice&0xff)>>rShift;
47-
};
48-
}// Otherwise, while slicing off bits per char, we can possibly straddle two
49-
// of bytes, so a more work is involved
50-
51-
52-
varslicesPerChunk=lcm(bitsPerChar,BITS_PER_BYTE)/BITS_PER_BYTE;
53-
returnfunction(chunk,slice,bytes){
54-
varbNum=chunk*slicesPerChunk;
55-
varoffset=slice*bitsPerChar/BITS_PER_BYTE;
56-
varlOffset=floor(offset);
57-
varrOffset=ceil(offset);
58-
varrShift=BITS_PER_BYTE-bitsPerChar;
59-
varlShift=slice*bitsPerChar%BITS_PER_BYTE;
60-
varndx=(bytes[bNum+lOffset]<<lShift&0xff)>>rShift;
61-
varr1Bits=(rOffset+1)*BITS_PER_BYTE;
62-
vars1Bits=(slice+1)*bitsPerChar;
63-
varrShiftIt=(r1Bits-s1Bits)%BITS_PER_BYTE;
64-
65-
if(rShift<rShiftIt){
66-
ndx+=bytes[bNum+rOffset]>>rShiftIt;
67-
}
68-
69-
returnndx;
70-
};
71-
};
72-
73-
varCharSet=
74-
/*#__PURE__*/
75-
function(){
76-
functionCharSet(chars){
77-
(0,_classCallCheck2["default"])(this,CharSet);
78-
79-
if(!(typeofchars==='string'||charsinstanceofString)){
80-
thrownewError('Invalid chars: Must be string');
81-
}
82-
83-
varlength=chars.length;
84-
85-
if(![2,4,8,16,32,64].includes(length)){
86-
thrownewError('Invalid char count: must be one of 2,4,8,16,32,64');
87-
}// Ensure no repeated characters
88-
89-
90-
for(vari=0;i<length;i+=1){
91-
varc=chars.charAt(i);
92-
93-
for(varj=i+1;j<length;j+=1){
94-
if(c===chars.charAt(j)){
95-
thrownewError('Characters not unique');
96-
}
97-
}
98-
}
99-
100-
this.chars=chars;
101-
this.bitsPerChar=floor(log2(length));
102-
this.length=length;
103-
this.ndxFn=genNdxFn(this.bitsPerChar);
104-
this.charsPerChunk=lcm(this.bitsPerChar,BITS_PER_BYTE)/this.bitsPerChar;
105-
}
106-
107-
(0,_createClass2["default"])(CharSet,[{
108-
key:"bytesNeeded",
109-
value:functionbytesNeeded(bitLen){
110-
varcount=ceil(bitLen/this.bitsPerChar);
111-
returnceil(count*this.bitsPerChar/BITS_PER_BYTE);
112-
}
113-
}]);
114-
returnCharSet;
115-
}();
116-
11723
varcharset64=newCharSet('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_');
11824
varcharset32=newCharSet('2346789bdfghjmnpqrtBDFGHJLMNPQRT');
11925
varcharset16=newCharSet('0123456789abcdef');
@@ -178,9 +84,7 @@ var entropyBits = function entropyBits(total, risk) {
17884
returnN+log2(risk)-1;
17985
};
18086

181-
varEntropy=
182-
/*#__PURE__*/
183-
function(){
87+
varEntropy=/*#__PURE__*/function(){
18488
functionEntropy(){
18589
varparams=arguments.length>0&&arguments[0]!==undefined ?arguments[0] :{
18690
bits:128,

‎dist/lib/csprng-bytes-browser.js‎

Lines changed: 0 additions & 9 deletions
This file was deleted.

‎dist/lib/csprng-bytes.js‎

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp