@cloudglides/veil 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/build-on-tag.yml +2 -0
- package/.github/workflows/deploy-pages.yml +73 -0
- package/README.md +190 -0
- package/example/index.html +16 -22
- package/example/veil_core.d.ts +71 -0
- package/example/veil_core.js +370 -0
- package/example/veil_core_bg.wasm +0 -0
- package/example/veil_core_bg.wasm.d.ts +20 -0
- package/package.json +1 -1
- package/src/entropy/approximate.ts +3 -2
- package/src/entropy/complexity.ts +3 -2
- package/src/entropy/distribution.ts +3 -2
- package/src/entropy/spectral.ts +3 -2
- package/src/index.ts +92 -42
- package/src/stability.ts +405 -0
- package/src/tamper.ts +207 -0
- package/src/types.ts +11 -2
- package/src/wasm-loader.ts +32 -2
- package/tsup.config.ts +20 -14
- package/veil-core/src/lib.rs +3 -0
- package/veil-core/src/similarity.rs +67 -0
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
let wasm;
|
|
2
|
+
|
|
3
|
+
let cachedFloat64ArrayMemory0 = null;
|
|
4
|
+
function getFloat64ArrayMemory0() {
|
|
5
|
+
if (cachedFloat64ArrayMemory0 === null || cachedFloat64ArrayMemory0.byteLength === 0) {
|
|
6
|
+
cachedFloat64ArrayMemory0 = new Float64Array(wasm.memory.buffer);
|
|
7
|
+
}
|
|
8
|
+
return cachedFloat64ArrayMemory0;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function getStringFromWasm0(ptr, len) {
|
|
12
|
+
ptr = ptr >>> 0;
|
|
13
|
+
return decodeText(ptr, len);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
let cachedUint8ArrayMemory0 = null;
|
|
17
|
+
function getUint8ArrayMemory0() {
|
|
18
|
+
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
|
|
19
|
+
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
|
|
20
|
+
}
|
|
21
|
+
return cachedUint8ArrayMemory0;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function passArray8ToWasm0(arg, malloc) {
|
|
25
|
+
const ptr = malloc(arg.length * 1, 1) >>> 0;
|
|
26
|
+
getUint8ArrayMemory0().set(arg, ptr / 1);
|
|
27
|
+
WASM_VECTOR_LEN = arg.length;
|
|
28
|
+
return ptr;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function passArrayF64ToWasm0(arg, malloc) {
|
|
32
|
+
const ptr = malloc(arg.length * 8, 8) >>> 0;
|
|
33
|
+
getFloat64ArrayMemory0().set(arg, ptr / 8);
|
|
34
|
+
WASM_VECTOR_LEN = arg.length;
|
|
35
|
+
return ptr;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function passStringToWasm0(arg, malloc, realloc) {
|
|
39
|
+
if (realloc === undefined) {
|
|
40
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
41
|
+
const ptr = malloc(buf.length, 1) >>> 0;
|
|
42
|
+
getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
|
|
43
|
+
WASM_VECTOR_LEN = buf.length;
|
|
44
|
+
return ptr;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
let len = arg.length;
|
|
48
|
+
let ptr = malloc(len, 1) >>> 0;
|
|
49
|
+
|
|
50
|
+
const mem = getUint8ArrayMemory0();
|
|
51
|
+
|
|
52
|
+
let offset = 0;
|
|
53
|
+
|
|
54
|
+
for (; offset < len; offset++) {
|
|
55
|
+
const code = arg.charCodeAt(offset);
|
|
56
|
+
if (code > 0x7F) break;
|
|
57
|
+
mem[ptr + offset] = code;
|
|
58
|
+
}
|
|
59
|
+
if (offset !== len) {
|
|
60
|
+
if (offset !== 0) {
|
|
61
|
+
arg = arg.slice(offset);
|
|
62
|
+
}
|
|
63
|
+
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
|
|
64
|
+
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
|
|
65
|
+
const ret = cachedTextEncoder.encodeInto(arg, view);
|
|
66
|
+
|
|
67
|
+
offset += ret.written;
|
|
68
|
+
ptr = realloc(ptr, len, offset, 1) >>> 0;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
WASM_VECTOR_LEN = offset;
|
|
72
|
+
return ptr;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
76
|
+
cachedTextDecoder.decode();
|
|
77
|
+
const MAX_SAFARI_DECODE_BYTES = 2146435072;
|
|
78
|
+
let numBytesDecoded = 0;
|
|
79
|
+
function decodeText(ptr, len) {
|
|
80
|
+
numBytesDecoded += len;
|
|
81
|
+
if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
|
|
82
|
+
cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
83
|
+
cachedTextDecoder.decode();
|
|
84
|
+
numBytesDecoded = len;
|
|
85
|
+
}
|
|
86
|
+
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const cachedTextEncoder = new TextEncoder();
|
|
90
|
+
|
|
91
|
+
if (!('encodeInto' in cachedTextEncoder)) {
|
|
92
|
+
cachedTextEncoder.encodeInto = function (arg, view) {
|
|
93
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
94
|
+
view.set(buf);
|
|
95
|
+
return {
|
|
96
|
+
read: arg.length,
|
|
97
|
+
written: buf.length
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
let WASM_VECTOR_LEN = 0;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* @param {Float64Array} samples
|
|
106
|
+
* @param {number} m
|
|
107
|
+
* @returns {number}
|
|
108
|
+
*/
|
|
109
|
+
export function approx_entropy(samples, m) {
|
|
110
|
+
const ptr0 = passArrayF64ToWasm0(samples, wasm.__wbindgen_malloc);
|
|
111
|
+
const len0 = WASM_VECTOR_LEN;
|
|
112
|
+
const ret = wasm.approx_entropy(ptr0, len0, m);
|
|
113
|
+
return ret;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* @param {Float64Array} v1
|
|
118
|
+
* @param {Float64Array} v2
|
|
119
|
+
* @returns {number}
|
|
120
|
+
*/
|
|
121
|
+
export function cosine_similarity(v1, v2) {
|
|
122
|
+
const ptr0 = passArrayF64ToWasm0(v1, wasm.__wbindgen_malloc);
|
|
123
|
+
const len0 = WASM_VECTOR_LEN;
|
|
124
|
+
const ptr1 = passArrayF64ToWasm0(v2, wasm.__wbindgen_malloc);
|
|
125
|
+
const len1 = WASM_VECTOR_LEN;
|
|
126
|
+
const ret = wasm.cosine_similarity(ptr0, len0, ptr1, len1);
|
|
127
|
+
return ret;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* @param {string} s
|
|
132
|
+
* @returns {string}
|
|
133
|
+
*/
|
|
134
|
+
export function fnv_hash(s) {
|
|
135
|
+
let deferred2_0;
|
|
136
|
+
let deferred2_1;
|
|
137
|
+
try {
|
|
138
|
+
const ptr0 = passStringToWasm0(s, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
139
|
+
const len0 = WASM_VECTOR_LEN;
|
|
140
|
+
const ret = wasm.fnv_hash(ptr0, len0);
|
|
141
|
+
deferred2_0 = ret[0];
|
|
142
|
+
deferred2_1 = ret[1];
|
|
143
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
144
|
+
} finally {
|
|
145
|
+
wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* @param {string} s
|
|
151
|
+
* @returns {number}
|
|
152
|
+
*/
|
|
153
|
+
export function kolmogorov_complexity(s) {
|
|
154
|
+
const ptr0 = passStringToWasm0(s, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
155
|
+
const len0 = WASM_VECTOR_LEN;
|
|
156
|
+
const ret = wasm.kolmogorov_complexity(ptr0, len0);
|
|
157
|
+
return ret;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* @param {Float64Array} values
|
|
162
|
+
* @returns {number}
|
|
163
|
+
*/
|
|
164
|
+
export function ks_test(values) {
|
|
165
|
+
const ptr0 = passArrayF64ToWasm0(values, wasm.__wbindgen_malloc);
|
|
166
|
+
const len0 = WASM_VECTOR_LEN;
|
|
167
|
+
const ret = wasm.ks_test(ptr0, len0);
|
|
168
|
+
return ret;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* @param {string} s1
|
|
173
|
+
* @param {string} s2
|
|
174
|
+
* @returns {number}
|
|
175
|
+
*/
|
|
176
|
+
export function levenshtein_distance(s1, s2) {
|
|
177
|
+
const ptr0 = passStringToWasm0(s1, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
178
|
+
const len0 = WASM_VECTOR_LEN;
|
|
179
|
+
const ptr1 = passStringToWasm0(s2, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
180
|
+
const len1 = WASM_VECTOR_LEN;
|
|
181
|
+
const ret = wasm.levenshtein_distance(ptr0, len0, ptr1, len1);
|
|
182
|
+
return ret >>> 0;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* @param {Uint8Array} data
|
|
187
|
+
* @returns {number}
|
|
188
|
+
*/
|
|
189
|
+
export function lz_complexity(data) {
|
|
190
|
+
const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc);
|
|
191
|
+
const len0 = WASM_VECTOR_LEN;
|
|
192
|
+
const ret = wasm.lz_complexity(ptr0, len0);
|
|
193
|
+
return ret >>> 0;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* @param {string} s
|
|
198
|
+
* @returns {string}
|
|
199
|
+
*/
|
|
200
|
+
export function murmur_hash(s) {
|
|
201
|
+
let deferred2_0;
|
|
202
|
+
let deferred2_1;
|
|
203
|
+
try {
|
|
204
|
+
const ptr0 = passStringToWasm0(s, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
205
|
+
const len0 = WASM_VECTOR_LEN;
|
|
206
|
+
const ret = wasm.murmur_hash(ptr0, len0);
|
|
207
|
+
deferred2_0 = ret[0];
|
|
208
|
+
deferred2_1 = ret[1];
|
|
209
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
210
|
+
} finally {
|
|
211
|
+
wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* @param {Float64Array} samples
|
|
217
|
+
* @param {number} m
|
|
218
|
+
* @param {number} r
|
|
219
|
+
* @returns {number}
|
|
220
|
+
*/
|
|
221
|
+
export function sample_ent(samples, m, r) {
|
|
222
|
+
const ptr0 = passArrayF64ToWasm0(samples, wasm.__wbindgen_malloc);
|
|
223
|
+
const len0 = WASM_VECTOR_LEN;
|
|
224
|
+
const ret = wasm.sample_ent(ptr0, len0, m, r);
|
|
225
|
+
return ret;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* @param {string} s
|
|
230
|
+
* @returns {number}
|
|
231
|
+
*/
|
|
232
|
+
export function shannon_entropy(s) {
|
|
233
|
+
const ptr0 = passStringToWasm0(s, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
234
|
+
const len0 = WASM_VECTOR_LEN;
|
|
235
|
+
const ret = wasm.shannon_entropy(ptr0, len0);
|
|
236
|
+
return ret;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* @param {string} s1
|
|
241
|
+
* @param {string} s2
|
|
242
|
+
* @returns {number}
|
|
243
|
+
*/
|
|
244
|
+
export function similarity_score(s1, s2) {
|
|
245
|
+
const ptr0 = passStringToWasm0(s1, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
246
|
+
const len0 = WASM_VECTOR_LEN;
|
|
247
|
+
const ptr1 = passStringToWasm0(s2, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
248
|
+
const len1 = WASM_VECTOR_LEN;
|
|
249
|
+
const ret = wasm.similarity_score(ptr0, len0, ptr1, len1);
|
|
250
|
+
return ret;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* @param {Float64Array} samples
|
|
255
|
+
* @returns {number}
|
|
256
|
+
*/
|
|
257
|
+
export function spectral(samples) {
|
|
258
|
+
const ptr0 = passArrayF64ToWasm0(samples, wasm.__wbindgen_malloc);
|
|
259
|
+
const len0 = WASM_VECTOR_LEN;
|
|
260
|
+
const ret = wasm.spectral(ptr0, len0);
|
|
261
|
+
return ret;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const EXPECTED_RESPONSE_TYPES = new Set(['basic', 'cors', 'default']);
|
|
265
|
+
|
|
266
|
+
async function __wbg_load(module, imports) {
|
|
267
|
+
if (typeof Response === 'function' && module instanceof Response) {
|
|
268
|
+
if (typeof WebAssembly.instantiateStreaming === 'function') {
|
|
269
|
+
try {
|
|
270
|
+
return await WebAssembly.instantiateStreaming(module, imports);
|
|
271
|
+
} catch (e) {
|
|
272
|
+
const validResponse = module.ok && EXPECTED_RESPONSE_TYPES.has(module.type);
|
|
273
|
+
|
|
274
|
+
if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
|
|
275
|
+
console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
|
|
276
|
+
|
|
277
|
+
} else {
|
|
278
|
+
throw e;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const bytes = await module.arrayBuffer();
|
|
284
|
+
return await WebAssembly.instantiate(bytes, imports);
|
|
285
|
+
} else {
|
|
286
|
+
const instance = await WebAssembly.instantiate(module, imports);
|
|
287
|
+
|
|
288
|
+
if (instance instanceof WebAssembly.Instance) {
|
|
289
|
+
return { instance, module };
|
|
290
|
+
} else {
|
|
291
|
+
return instance;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function __wbg_get_imports() {
|
|
297
|
+
const imports = {};
|
|
298
|
+
imports.wbg = {};
|
|
299
|
+
imports.wbg.__wbindgen_init_externref_table = function() {
|
|
300
|
+
const table = wasm.__wbindgen_externrefs;
|
|
301
|
+
const offset = table.grow(4);
|
|
302
|
+
table.set(0, undefined);
|
|
303
|
+
table.set(offset + 0, undefined);
|
|
304
|
+
table.set(offset + 1, null);
|
|
305
|
+
table.set(offset + 2, true);
|
|
306
|
+
table.set(offset + 3, false);
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
return imports;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function __wbg_finalize_init(instance, module) {
|
|
313
|
+
wasm = instance.exports;
|
|
314
|
+
__wbg_init.__wbindgen_wasm_module = module;
|
|
315
|
+
cachedFloat64ArrayMemory0 = null;
|
|
316
|
+
cachedUint8ArrayMemory0 = null;
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
wasm.__wbindgen_start();
|
|
320
|
+
return wasm;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
function initSync(module) {
|
|
324
|
+
if (wasm !== undefined) return wasm;
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
if (typeof module !== 'undefined') {
|
|
328
|
+
if (Object.getPrototypeOf(module) === Object.prototype) {
|
|
329
|
+
({module} = module)
|
|
330
|
+
} else {
|
|
331
|
+
console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const imports = __wbg_get_imports();
|
|
336
|
+
if (!(module instanceof WebAssembly.Module)) {
|
|
337
|
+
module = new WebAssembly.Module(module);
|
|
338
|
+
}
|
|
339
|
+
const instance = new WebAssembly.Instance(module, imports);
|
|
340
|
+
return __wbg_finalize_init(instance, module);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
async function __wbg_init(module_or_path) {
|
|
344
|
+
if (wasm !== undefined) return wasm;
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
if (typeof module_or_path !== 'undefined') {
|
|
348
|
+
if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
|
|
349
|
+
({module_or_path} = module_or_path)
|
|
350
|
+
} else {
|
|
351
|
+
console.warn('using deprecated parameters for the initialization function; pass a single object instead')
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
if (typeof module_or_path === 'undefined') {
|
|
356
|
+
module_or_path = new URL('veil_core_bg.wasm', import.meta.url).href;
|
|
357
|
+
}
|
|
358
|
+
const imports = __wbg_get_imports();
|
|
359
|
+
|
|
360
|
+
if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
|
|
361
|
+
module_or_path = fetch(module_or_path);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
const { instance, module } = await __wbg_load(await module_or_path, imports);
|
|
365
|
+
|
|
366
|
+
return __wbg_finalize_init(instance, module);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
export { initSync };
|
|
370
|
+
export default __wbg_init;
|
|
Binary file
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
export const memory: WebAssembly.Memory;
|
|
4
|
+
export const cosine_similarity: (a: number, b: number, c: number, d: number) => number;
|
|
5
|
+
export const levenshtein_distance: (a: number, b: number, c: number, d: number) => number;
|
|
6
|
+
export const similarity_score: (a: number, b: number, c: number, d: number) => number;
|
|
7
|
+
export const approx_entropy: (a: number, b: number, c: number) => number;
|
|
8
|
+
export const fnv_hash: (a: number, b: number) => [number, number];
|
|
9
|
+
export const kolmogorov_complexity: (a: number, b: number) => number;
|
|
10
|
+
export const ks_test: (a: number, b: number) => number;
|
|
11
|
+
export const lz_complexity: (a: number, b: number) => number;
|
|
12
|
+
export const sample_ent: (a: number, b: number, c: number, d: number) => number;
|
|
13
|
+
export const shannon_entropy: (a: number, b: number) => number;
|
|
14
|
+
export const spectral: (a: number, b: number) => number;
|
|
15
|
+
export const murmur_hash: (a: number, b: number) => [number, number];
|
|
16
|
+
export const __wbindgen_externrefs: WebAssembly.Table;
|
|
17
|
+
export const __wbindgen_malloc: (a: number, b: number) => number;
|
|
18
|
+
export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
19
|
+
export const __wbindgen_free: (a: number, b: number, c: number) => void;
|
|
20
|
+
export const __wbindgen_start: () => void;
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getWasmModule } from "../wasm-loader";
|
|
2
2
|
import { seededRng } from "../seeded-rng";
|
|
3
3
|
|
|
4
4
|
export async function getApproximateEntropy(seed: string): Promise<string> {
|
|
5
5
|
const samples = seededRng(seed, 256);
|
|
6
|
-
const
|
|
6
|
+
const wasm = await getWasmModule();
|
|
7
|
+
const apen = wasm.approx_entropy(new Float64Array(samples), 2);
|
|
7
8
|
return `apen:${apen.toFixed(6)}`;
|
|
8
9
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getWasmModule } from "../wasm-loader";
|
|
2
2
|
|
|
3
3
|
export async function getComplexityEntropy(): Promise<string> {
|
|
4
4
|
const ua = navigator.userAgent;
|
|
@@ -8,6 +8,7 @@ export async function getComplexityEntropy(): Promise<string> {
|
|
|
8
8
|
bytes[i] = ua.charCodeAt(i) % 256;
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
const
|
|
11
|
+
const wasm = await getWasmModule();
|
|
12
|
+
const complexity = wasm.lz_complexity(bytes);
|
|
12
13
|
return `lz:${complexity}`;
|
|
13
14
|
}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getWasmModule } from "../wasm-loader";
|
|
2
2
|
import { seededRng } from "../seeded-rng";
|
|
3
3
|
|
|
4
4
|
export async function getDistributionEntropy(seed: string): Promise<string> {
|
|
5
5
|
const samples = seededRng(seed, 1000);
|
|
6
|
-
const
|
|
6
|
+
const wasm = await getWasmModule();
|
|
7
|
+
const ksStatistic = wasm.ks_test(new Float64Array(samples));
|
|
7
8
|
return `ks:${ksStatistic.toFixed(6)}`;
|
|
8
9
|
}
|
package/src/entropy/spectral.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getWasmModule } from "../wasm-loader";
|
|
2
2
|
import { seededRng } from "../seeded-rng";
|
|
3
3
|
|
|
4
4
|
export async function getSpectralEntropy(seed: string): Promise<string> {
|
|
5
5
|
const samples = seededRng(seed, 128);
|
|
6
|
-
const
|
|
6
|
+
const wasm = await getWasmModule();
|
|
7
|
+
const entropy = wasm.spectral(new Float64Array(samples));
|
|
7
8
|
return `spectral:${entropy.toFixed(6)}`;
|
|
8
9
|
}
|
package/src/index.ts
CHANGED
|
@@ -23,18 +23,38 @@ import { getPreferencesEntropy } from "./entropy/preferences";
|
|
|
23
23
|
import { getPermissionsEntropy } from "./entropy/permissions";
|
|
24
24
|
import { getStatisticalEntropy } from "./entropy/statistical";
|
|
25
25
|
import { getProbabilisticEntropy } from "./entropy/probabilistic";
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
26
|
+
function levenshteinDistance(s1: string, s2: string): number {
|
|
27
|
+
const len1 = s1.length;
|
|
28
|
+
const len2 = s2.length;
|
|
29
|
+
const matrix: number[][] = Array(len1 + 1).fill(null).map(() => Array(len2 + 1).fill(0));
|
|
30
|
+
|
|
31
|
+
for (let i = 0; i <= len1; i++) matrix[i][0] = i;
|
|
32
|
+
for (let j = 0; j <= len2; j++) matrix[0][j] = j;
|
|
33
|
+
|
|
34
|
+
for (let i = 1; i <= len1; i++) {
|
|
35
|
+
for (let j = 1; j <= len2; j++) {
|
|
36
|
+
const cost = s1[i - 1] === s2[j - 1] ? 0 : 1;
|
|
37
|
+
matrix[i][j] = Math.min(
|
|
38
|
+
matrix[i - 1][j] + 1,
|
|
39
|
+
matrix[i][j - 1] + 1,
|
|
40
|
+
matrix[i - 1][j - 1] + cost,
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return matrix[len1][len2];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function similarityScore(s1: string, s2: string): number {
|
|
48
|
+
const distance = levenshteinDistance(s1, s2);
|
|
49
|
+
const maxLen = Math.max(s1.length, s2.length);
|
|
50
|
+
return maxLen === 0 ? 1 : 1 - distance / maxLen;
|
|
51
|
+
}
|
|
52
|
+
|
|
32
53
|
import * as normalize from "./normalize";
|
|
33
54
|
import { initializeWasm } from "./wasm-loader";
|
|
34
55
|
import { scoreFingerprint, calculateEntropy, type EntropySource } from "./scoring";
|
|
35
|
-
import {
|
|
36
|
-
import {
|
|
37
|
-
import { seededRng } from "./seeded-rng";
|
|
56
|
+
import { analyzeTamper } from "./tamper";
|
|
57
|
+
import { getSourceStability, assessFingerprint, generateDeviceIdentity } from "./stability";
|
|
38
58
|
|
|
39
59
|
export async function getFingerprint(
|
|
40
60
|
options?: FingerprintOptions,
|
|
@@ -107,46 +127,29 @@ export async function getFingerprint(
|
|
|
107
127
|
}
|
|
108
128
|
|
|
109
129
|
const score = scoreFingerprint(sources);
|
|
130
|
+
const tamper = analyzeTamper(sources);
|
|
131
|
+
const stability = getSourceStability(sources);
|
|
132
|
+
const assessment = assessFingerprint(sources);
|
|
110
133
|
|
|
111
|
-
const
|
|
134
|
+
const deviceIdentity = generateDeviceIdentity(sources);
|
|
112
135
|
|
|
113
|
-
const
|
|
114
|
-
const
|
|
115
|
-
const kolmogorov = kolmogorov_complexity(dataStr);
|
|
116
|
-
const murmur = murmur_hash(dataStr);
|
|
117
|
-
const fnv = fnv_hash(dataStr);
|
|
136
|
+
const data = sources.map(s => s.value);
|
|
137
|
+
const combined = data.join("|");
|
|
118
138
|
|
|
119
|
-
const
|
|
120
|
-
const
|
|
139
|
+
const coreWeight = "core:" + deviceIdentity.core_hash;
|
|
140
|
+
const prefsWeight = "prefs:" + deviceIdentity.preferences_hash;
|
|
141
|
+
const weightedInput = `${coreWeight}|${prefsWeight}|${combined}`;
|
|
121
142
|
|
|
122
143
|
const algorithm = opts.hash === "sha512" ? "SHA-512" : "SHA-256";
|
|
123
144
|
const hash = await crypto.subtle.digest(
|
|
124
145
|
algorithm,
|
|
125
|
-
new TextEncoder().encode(
|
|
146
|
+
new TextEncoder().encode(weightedInput),
|
|
126
147
|
);
|
|
127
148
|
|
|
128
149
|
let fingerprint = Array.from(new Uint8Array(hash))
|
|
129
150
|
.map((b) => b.toString(16).padStart(2, "0"))
|
|
130
151
|
.join("");
|
|
131
152
|
|
|
132
|
-
if (opts.gpuBenchmark) {
|
|
133
|
-
const gpuHash = await getGPUHash();
|
|
134
|
-
fingerprint = Array.from(new Uint8Array(
|
|
135
|
-
await crypto.subtle.digest("SHA-256", new TextEncoder().encode(fingerprint + gpuHash))
|
|
136
|
-
))
|
|
137
|
-
.map((b) => b.toString(16).padStart(2, "0"))
|
|
138
|
-
.join("");
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
if (opts.cpuBenchmark) {
|
|
142
|
-
const cpuHash = await getCPUHash();
|
|
143
|
-
fingerprint = Array.from(new Uint8Array(
|
|
144
|
-
await crypto.subtle.digest("SHA-256", new TextEncoder().encode(fingerprint + cpuHash))
|
|
145
|
-
))
|
|
146
|
-
.map((b) => b.toString(16).padStart(2, "0"))
|
|
147
|
-
.join("");
|
|
148
|
-
}
|
|
149
|
-
|
|
150
153
|
if (opts.detailed) {
|
|
151
154
|
const os = await getOSVersionEntropy();
|
|
152
155
|
const lang = await getLanguageEntropy();
|
|
@@ -156,17 +159,32 @@ export async function getFingerprint(
|
|
|
156
159
|
const ua = await getUserAgentEntropy();
|
|
157
160
|
const browser = await getBrowserEntropy();
|
|
158
161
|
|
|
159
|
-
const sourceMetrics: SourceMetric[] = sources.map(s =>
|
|
160
|
-
source
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
162
|
+
const sourceMetrics: SourceMetric[] = sources.map(s => {
|
|
163
|
+
const stab = stability.find(st => st.source === s.name);
|
|
164
|
+
return {
|
|
165
|
+
source: s.name,
|
|
166
|
+
value: s.value,
|
|
167
|
+
entropy: s.entropy,
|
|
168
|
+
confidence: score.likelihood > 0 ? Math.min(s.entropy / score.likelihood, 1) : 0,
|
|
169
|
+
stability: stab?.stability,
|
|
170
|
+
};
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
const avgStability = stability.length > 0
|
|
174
|
+
? stability.reduce((a, b) => a + b.stability, 0) / stability.length
|
|
175
|
+
: 0;
|
|
165
176
|
|
|
166
177
|
const response: FingerprintResponse = {
|
|
167
178
|
hash: fingerprint,
|
|
168
179
|
uniqueness: score.uniqueness,
|
|
169
180
|
confidence: score.confidence,
|
|
181
|
+
stability_score: avgStability,
|
|
182
|
+
usable: assessment.usable,
|
|
183
|
+
warnings: assessment.warnings.length > 0 ? assessment.warnings : undefined,
|
|
184
|
+
entropy_warnings: assessment.entropy_warnings.length > 0 ? assessment.entropy_warnings : undefined,
|
|
185
|
+
tampering_risk: tamper.tampering_risk,
|
|
186
|
+
anomalies: tamper.anomalies,
|
|
187
|
+
device_identity: deviceIdentity,
|
|
170
188
|
sources: sourceMetrics,
|
|
171
189
|
system: {
|
|
172
190
|
os,
|
|
@@ -195,4 +213,36 @@ export async function getFingerprint(
|
|
|
195
213
|
return fingerprint;
|
|
196
214
|
}
|
|
197
215
|
|
|
216
|
+
export async function compareFingerpints(
|
|
217
|
+
fp1: string | FingerprintResponse,
|
|
218
|
+
fp2: string | FingerprintResponse,
|
|
219
|
+
): Promise<{ similarity: number; match: boolean }> {
|
|
220
|
+
const hash1 = typeof fp1 === "string" ? fp1 : fp1.hash;
|
|
221
|
+
const hash2 = typeof fp2 === "string" ? fp2 : fp2.hash;
|
|
222
|
+
|
|
223
|
+
const sim = similarityScore(hash1, hash2);
|
|
224
|
+
const match = sim > 0.95;
|
|
225
|
+
|
|
226
|
+
return { similarity: sim, match };
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export async function matchProbability(
|
|
230
|
+
storedFingerprints: (string | FingerprintResponse)[],
|
|
231
|
+
currentFingerprint: string | FingerprintResponse,
|
|
232
|
+
): Promise<{ bestMatch: number; confidence: number }> {
|
|
233
|
+
const currentHash = typeof currentFingerprint === "string" ? currentFingerprint : currentFingerprint.hash;
|
|
234
|
+
|
|
235
|
+
let bestMatch = 0;
|
|
236
|
+
for (const stored of storedFingerprints) {
|
|
237
|
+
const storedHash = typeof stored === "string" ? stored : stored.hash;
|
|
238
|
+
const sim = similarityScore(currentHash, storedHash);
|
|
239
|
+
if (sim > bestMatch) {
|
|
240
|
+
bestMatch = sim;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const confidence = Math.min(bestMatch * 1.2, 1.0);
|
|
245
|
+
return { bestMatch, confidence };
|
|
246
|
+
}
|
|
247
|
+
|
|
198
248
|
export type { FingerprintOptions, FingerprintResponse } from "./types";
|