forked from EntropyString/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentropy.js
More file actions
219 lines (184 loc) · 5.49 KB
/
entropy.js
File metadata and controls
219 lines (184 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import CharSet from './charSet'
import lcm from './lcm'
const _log2 = Math.log2
const _log10 = Math.log10
const _log2_10 = _log2(10)
const _endianByteNum = (() => {
const buf32 = new Uint32Array(1)
const buf8 = new Uint8Array(buf32.buffer)
buf32[0] = 0xff
return (buf8[0] === 0xff) ? [2,3,4,5,6,7] : [0,1,2,3,6,7]
})()
const bits = (total, risk) => {
if (total == 0) { return 0 }
let N = 0
if (total < 10001) {
N = _log2(total) + _log2(total-1) + (_log2_10 * _log10(risk)) - 1
}
else {
const n = 2 * _log10(total) + _log10(risk)
N = n * _log2_10 - 1
}
return N
}
const bitsWithRiskPower = (total, rPower) => {
if (total == 0) { return 0 }
let N = 0
if (total < 10001) {
N = _log2(total) + _log2(total-1) + (_log2_10 * rPower) - 1
}
else {
const n = 2 * _log10(total) + rPower
N = n * _log2_10 - 1
}
return N
}
const bitsWithPowers = (tPower, rPower) => {
let N = 0
if (tPower < 5) {
return bitsWithRiskPower(Math.pow(10, tPower), rPower)
}
else {
const n = 2 * tPower + rPower
N = n * _log2_10 - 1
}
return N
}
const string = (entropy, charSet, opt = null) => {
if ( !( (opt === null) || (opt instanceof Uint8Array) || (typeof opt == 'boolean') ) ) {
throw new Error('Optional 3rd argument must be either an Uint8Array or a boolean')
}
let bytes = (opt instanceof Uint8Array) ? opt :
(opt === false) ? _randomBytes(entropy, charSet) : _cryptoBytes(entropy, charSet)
return stringWithBytes(entropy, charSet, bytes)
}
const stringWithBytes = (entropy, charSet, bytes) => {
if (!CharSet.isValid(charSet)) {
throw new Error('Invalid CharSet')
}
if (entropy <= 0) { return '' }
const count = Math.ceil(entropy / charSet.entropyPerChar)
if (count <= 0) { return '' }
const needed = Math.ceil(count * (charSet.entropyPerChar/8))
if (bytes.length < needed) {
throw new Error('Insufficient bytes')
}
const chunks = Math.floor(count / charSet.charsPerChunk)
const partials = count % charSet.charsPerChunk
let ndxFn
switch (charSet) {
case CharSet.charSet64:
ndxFn = _ndx64
break
case CharSet.charSet32:
ndxFn = _ndx32
break
case CharSet.charSet16:
ndxFn = _ndx16
break
case CharSet.charSet8:
ndxFn = _ndx8
break
case CharSet.charSet4:
ndxFn = _ndx4
break
case CharSet.charSet2:
ndxFn = _ndx2
break
default:
break
}
let chars = charSet.chars
let string = ''
for (let chunk = 0; chunk < chunks; chunk++) {
for (let slice = 0; slice < charSet.charsPerChunk; slice++) {
let ndx = ndxFn(chunk, slice, bytes)
string += chars[ndx]
}
}
for (let slice = 0; slice < partials; slice++) {
let ndx = ndxFn(chunks, slice, bytes)
string += chars[ndx]
}
return string
}
const bytesNeeded = (entropy, charSet) => {
if (!CharSet.isValid(charSet)) { throw new Error('Invalid CharSet') }
const count = Math.ceil(entropy / charSet.entropyPerChar)
if (count <= 0) { return 0 }
const bytesPerSlice = charSet.entropyPerChar / 8
return Math.ceil(count * bytesPerSlice)
}
const _cryptoBytes = (entropy, charSet) => {
const crypto = require('crypto')
return Buffer.from(crypto.randomBytes(bytesNeeded(entropy, charSet)))
}
const _randomBytes = (entropy, charSet) => {
const byteCount = bytesNeeded(entropy, charSet)
const randCount = Math.ceil(byteCount / 6)
const buffer = new Buffer(byteCount)
var dataView = new DataView(new ArrayBuffer(8))
for (let rNum = 0; rNum < randCount; rNum++) {
dataView.setFloat64(0, Math.random())
for (let n = 0; n < 6; n++) {
let fByteNum = _endianByteNum[n]
let bByteNum = 6*rNum + n
_bufferByte(buffer, bByteNum, fByteNum, byteCount, dataView)
}
}
return buffer
}
const _bufferByte = (buffer, bByte, nByte, byteCount, dataView) => {
if (bByte < byteCount) {
buffer[bByte] = dataView.getUint8(nByte)
}
}
const _ndx64 = (chunk, slice, bytes) => {
return _ndxGen(chunk, slice, bytes, 6)
}
const _ndx32 = (chunk, slice, bytes) => {
return _ndxGen(chunk, slice, bytes, 5)
}
const _ndx16 = (chunk, slice, bytes) => {
return ((bytes[chunk]<<(4*slice))&0xff)>>4
}
const _ndx8 = (chunk, slice, bytes) => {
return _ndxGen(chunk, slice, bytes, 3)
}
const _ndx4 = (chunk, slice, bytes) => {
return ((bytes[chunk]<<(2*slice))&0xff)>>6
}
const _ndx2 = (chunk, slice, bytes) => {
return ((bytes[chunk]<<slice)&0xff)>>7
}
const _ndxGen = (chunk, slice, bytes, bitsPerSlice) => {
let bitsPerByte = 8
let slicesPerChunk = lcm(bitsPerSlice, bitsPerByte) / bitsPerByte
let bNum = chunk * slicesPerChunk
let rShift = bitsPerByte - bitsPerSlice
let lOffset = Math.floor((slice*bitsPerSlice)/bitsPerByte)
let lShift = (slice*bitsPerSlice) % bitsPerByte
let ndx = ((bytes[bNum+lOffset]<<lShift)&0xff)>>rShift
let rOffset = Math.ceil((slice*bitsPerSlice)/bitsPerByte)
let rShiftIt = ((rOffset+1)*bitsPerByte - (slice+1)*bitsPerSlice) % bitsPerByte
if (rShift < rShiftIt) {
ndx += bytes[bNum+rOffset]>>rShiftIt
}
return ndx
}
export default {
bits : bits,
bitsWithRiskPower : bitsWithRiskPower,
bitsWithPowers : bitsWithPowers,
string : string,
stringWithBytes : stringWithBytes,
randomString : string,
randomStringWithBytes : stringWithBytes,
bytesNeeded : bytesNeeded,
charSet64: CharSet.charSet64,
charSet32: CharSet.charSet32,
charSet16: CharSet.charSet16,
charSet8: CharSet.charSet8,
charSet4: CharSet.charSet4,
charSet2: CharSet.charSet2
}