devicer.js 1.0.4 → 1.0.6
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/package.json +1 -1
- package/src/libs/confidence.js +9 -4
- package/src/libs/confidence.ts +15 -7
- package/tests/confidence.test.ts +24 -15
- package/tests/data.test.ts +6 -6
package/package.json
CHANGED
package/src/libs/confidence.js
CHANGED
|
@@ -8,7 +8,8 @@ function compareDataSets(data1, data2) {
|
|
|
8
8
|
for (const key in data1) {
|
|
9
9
|
if (data1[key] !== undefined && data2[key] !== undefined) {
|
|
10
10
|
fields++;
|
|
11
|
-
if (typeof data1[key] == "object")
|
|
11
|
+
if ((typeof data1[key] == "object" && data1[key]) &&
|
|
12
|
+
(typeof data2[key] == "object" && data2[key])) {
|
|
12
13
|
const subData = compareDataSets(data1[key], data2[key]);
|
|
13
14
|
fields += subData[0] - 1; // Subtract 1 for the key itself
|
|
14
15
|
matches += subData[1];
|
|
@@ -21,18 +22,22 @@ function compareDataSets(data1, data2) {
|
|
|
21
22
|
return [fields, matches];
|
|
22
23
|
}
|
|
23
24
|
function calculateConfidence(data1, data2) {
|
|
25
|
+
// Compare how many fields are the same in both datasets
|
|
26
|
+
const [fields, matches] = compareDataSets(data1, data2);
|
|
27
|
+
if (fields === 0 || matches === 0) {
|
|
28
|
+
return 0;
|
|
29
|
+
}
|
|
24
30
|
// Calculate the hash for each user data
|
|
25
31
|
const hash1 = (0, tlsh_1.getHash)(JSON.stringify(data1));
|
|
26
32
|
const hash2 = (0, tlsh_1.getHash)(JSON.stringify(data2));
|
|
27
33
|
// Compare the hashes to get their difference
|
|
28
34
|
const differenceScore = (0, tlsh_1.compareHashes)(hash1, hash2);
|
|
29
|
-
// Compare how many fields are the same in both datasets
|
|
30
|
-
const [fields, matches] = compareDataSets(data1, data2);
|
|
31
35
|
const inverseMatchScore = 1 - (matches / fields);
|
|
32
36
|
const x = (differenceScore / 1.5) * inverseMatchScore;
|
|
33
37
|
if (inverseMatchScore === 0 || differenceScore === 0) {
|
|
34
38
|
return 100;
|
|
35
39
|
}
|
|
36
|
-
|
|
40
|
+
const confidenceScore = 100 / (1 + Math.E ** (-4.5 + (0.25 * x)));
|
|
41
|
+
return confidenceScore;
|
|
37
42
|
}
|
|
38
43
|
exports.calculateConfidence = calculateConfidence;
|
package/src/libs/confidence.ts
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
import { getHash, compareHashes } from "./tlsh";
|
|
2
2
|
import { FPDataSet } from "../types/data";
|
|
3
3
|
|
|
4
|
-
function
|
|
4
|
+
function compareDatasets(data1: FPDataSet, data2: FPDataSet): [number, number] {
|
|
5
5
|
let fields = 0;
|
|
6
6
|
let matches = 0;
|
|
7
7
|
for (const key in data1) {
|
|
8
8
|
if (data1[key] !== undefined && data2[key] !== undefined) {
|
|
9
9
|
fields++;
|
|
10
|
-
if (
|
|
11
|
-
|
|
10
|
+
if (
|
|
11
|
+
(typeof data1[key] == "object" && data1[key]) &&
|
|
12
|
+
(typeof data2[key] == "object" && data2[key])
|
|
13
|
+
) {
|
|
14
|
+
const subData = compareDatasets(data1[key] as FPDataSet, data2[key] as FPDataSet);
|
|
12
15
|
fields += subData[0] - 1; // Subtract 1 for the key itself
|
|
13
16
|
matches += subData[1];
|
|
14
17
|
}
|
|
@@ -21,6 +24,13 @@ function compareDataSets(data1: FPDataSet, data2: FPDataSet): [number, number] {
|
|
|
21
24
|
}
|
|
22
25
|
|
|
23
26
|
export function calculateConfidence(data1: FPDataSet, data2: FPDataSet): number {
|
|
27
|
+
// Compare how many fields are the same in both datasets
|
|
28
|
+
const [fields, matches] = compareDatasets(data1, data2);
|
|
29
|
+
|
|
30
|
+
if (fields === 0 || matches === 0) {
|
|
31
|
+
return 0;
|
|
32
|
+
}
|
|
33
|
+
|
|
24
34
|
// Calculate the hash for each user data
|
|
25
35
|
const hash1 = getHash(JSON.stringify(data1));
|
|
26
36
|
const hash2 = getHash(JSON.stringify(data2));
|
|
@@ -28,13 +38,11 @@ export function calculateConfidence(data1: FPDataSet, data2: FPDataSet): number
|
|
|
28
38
|
// Compare the hashes to get their difference
|
|
29
39
|
const differenceScore = compareHashes(hash1, hash2);
|
|
30
40
|
|
|
31
|
-
// Compare how many fields are the same in both datasets
|
|
32
|
-
const [fields, matches] = compareDataSets(data1, data2);
|
|
33
|
-
|
|
34
41
|
const inverseMatchScore = 1 - (matches / fields);
|
|
35
42
|
const x = (differenceScore / 1.5) * inverseMatchScore
|
|
36
43
|
if (inverseMatchScore === 0 || differenceScore === 0) {
|
|
37
44
|
return 100;
|
|
38
45
|
}
|
|
39
|
-
|
|
46
|
+
const confidenceScore = 100 / (1 + Math.E ** (-4.5 + (0.25 * x)));
|
|
47
|
+
return confidenceScore;
|
|
40
48
|
}
|
package/tests/confidence.test.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { it, describe, expect } from 'vitest';
|
|
2
2
|
import { FPUserDataSet } from '../src/types/data';
|
|
3
|
-
import { getHash } from '../src/libs/tlsh';
|
|
4
3
|
import { calculateConfidence } from '../src/libs/confidence';
|
|
5
4
|
import { randomString } from './tlsh.test';
|
|
6
5
|
|
|
@@ -21,9 +20,9 @@ const sampleData1: FPUserDataSet = {
|
|
|
21
20
|
ip: '157.185.170.244',
|
|
22
21
|
languages: ['en-US', 'en'],
|
|
23
22
|
plugins: ['Chrome PDF Viewer', 'Shockwave Flash'],
|
|
24
|
-
canvasHash:
|
|
25
|
-
audioHash:
|
|
26
|
-
webglHash:
|
|
23
|
+
canvasHash: randomString(524),
|
|
24
|
+
audioHash: randomString(524),
|
|
25
|
+
webglHash: randomString(524)
|
|
27
26
|
};
|
|
28
27
|
|
|
29
28
|
const sampleData2: FPUserDataSet = {
|
|
@@ -43,9 +42,9 @@ const sampleData2: FPUserDataSet = {
|
|
|
43
42
|
ip: '178.238.11.6',
|
|
44
43
|
languages: ['en-GB', 'en'],
|
|
45
44
|
plugins: ['Chrome PDF Viewer', 'Shockwave Flash'],
|
|
46
|
-
canvasHash:
|
|
47
|
-
audioHash:
|
|
48
|
-
webglHash:
|
|
45
|
+
canvasHash: randomString(524),
|
|
46
|
+
audioHash: randomString(524),
|
|
47
|
+
webglHash: randomString(524)
|
|
49
48
|
};
|
|
50
49
|
|
|
51
50
|
describe('Confidence Calculation', () => {
|
|
@@ -85,19 +84,29 @@ describe('Confidence Calculation', () => {
|
|
|
85
84
|
const partialData: FPUserDataSet = {
|
|
86
85
|
...sampleData1,
|
|
87
86
|
hardware: {
|
|
88
|
-
|
|
89
|
-
gpu: 'Intel HD Graphics'
|
|
90
|
-
|
|
91
|
-
screen: {
|
|
92
|
-
...sampleData1.screen,
|
|
93
|
-
width: 1280, // Different screen width
|
|
94
|
-
height: 720 // Different screen height
|
|
87
|
+
cpu: 'Pentium 4',
|
|
88
|
+
gpu: 'Intel HD Graphics',
|
|
89
|
+
ram: 4096
|
|
95
90
|
},
|
|
96
|
-
timezone: 'Europe/London',
|
|
91
|
+
timezone: 'Europe/London',
|
|
92
|
+
ip: '178.238.11.6',
|
|
93
|
+
languages: ['en-GB', 'en'],
|
|
94
|
+
userAgent: 'Mozilla/5.0 (compatible; Konqueror/2.2.2-3; Linux)'
|
|
97
95
|
};
|
|
98
96
|
const confidence = calculateConfidence(sampleData1, partialData);
|
|
99
97
|
console.log('Confidence for partially similar data:', confidence);
|
|
100
98
|
expect(confidence).toBeGreaterThan(10);
|
|
101
99
|
expect(confidence).toBeLessThan(95);
|
|
102
100
|
});
|
|
101
|
+
|
|
102
|
+
it('should handle empty datasets and nonetypes gracefully', () => {
|
|
103
|
+
const incompleteData = {
|
|
104
|
+
...sampleData1,
|
|
105
|
+
hardware: {},
|
|
106
|
+
screen: null
|
|
107
|
+
};
|
|
108
|
+
const confidence = calculateConfidence(sampleData1, incompleteData);
|
|
109
|
+
expect(confidence).toBeGreaterThan(0); // Expecting some confidence even with missing data
|
|
110
|
+
expect(confidence).toBeLessThan(100); // Not identical, so confidence should not be 100
|
|
111
|
+
});
|
|
103
112
|
});
|
package/tests/data.test.ts
CHANGED
|
@@ -20,9 +20,9 @@ const sampleData1: FPUserDataSet = {
|
|
|
20
20
|
ip: '157.185.170.244',
|
|
21
21
|
languages: ['en-US', 'en'],
|
|
22
22
|
plugins: ['Chrome PDF Viewer', 'Shockwave Flash'],
|
|
23
|
-
canvasHash:
|
|
24
|
-
audioHash:
|
|
25
|
-
webglHash:
|
|
23
|
+
canvasHash: randomString(524),
|
|
24
|
+
audioHash: randomString(524),
|
|
25
|
+
webglHash: randomString(524)
|
|
26
26
|
};
|
|
27
27
|
|
|
28
28
|
const sampleData2: FPUserDataSet = {
|
|
@@ -42,9 +42,9 @@ const sampleData2: FPUserDataSet = {
|
|
|
42
42
|
ip: '178.238.11.6',
|
|
43
43
|
languages: ['en-GB', 'en'],
|
|
44
44
|
plugins: ['Chrome PDF Viewer', 'Shockwave Flash'],
|
|
45
|
-
canvasHash:
|
|
46
|
-
audioHash:
|
|
47
|
-
webglHash:
|
|
45
|
+
canvasHash: randomString(524),
|
|
46
|
+
audioHash: randomString(524),
|
|
47
|
+
webglHash: randomString(524)
|
|
48
48
|
};
|
|
49
49
|
|
|
50
50
|
describe('User Data Fingerprinting', () => {
|