devicer.js 1.0.5 → 1.0.7
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 +6 -5
- package/src/libs/confidence.ts +8 -5
- package/tests/confidence.test.ts +11 -0
package/package.json
CHANGED
package/src/libs/confidence.js
CHANGED
|
@@ -2,14 +2,15 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.calculateConfidence = void 0;
|
|
4
4
|
const tlsh_1 = require("./tlsh");
|
|
5
|
-
function
|
|
5
|
+
function compareDatasets(data1, data2) {
|
|
6
6
|
let fields = 0;
|
|
7
7
|
let matches = 0;
|
|
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")
|
|
12
|
-
|
|
11
|
+
if ((typeof data1[key] == "object" && data1[key]) &&
|
|
12
|
+
(typeof data2[key] == "object" && data2[key])) {
|
|
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];
|
|
15
16
|
}
|
|
@@ -22,7 +23,7 @@ function compareDataSets(data1, data2) {
|
|
|
22
23
|
}
|
|
23
24
|
function calculateConfidence(data1, data2) {
|
|
24
25
|
// Compare how many fields are the same in both datasets
|
|
25
|
-
const [fields, matches] =
|
|
26
|
+
const [fields, matches] = compareDatasets(data1, data2);
|
|
26
27
|
if (fields === 0 || matches === 0) {
|
|
27
28
|
return 0;
|
|
28
29
|
}
|
|
@@ -32,7 +33,7 @@ function calculateConfidence(data1, data2) {
|
|
|
32
33
|
// Compare the hashes to get their difference
|
|
33
34
|
const differenceScore = (0, tlsh_1.compareHashes)(hash1, hash2);
|
|
34
35
|
const inverseMatchScore = 1 - (matches / fields);
|
|
35
|
-
const x =
|
|
36
|
+
const x = differenceScore * inverseMatchScore;
|
|
36
37
|
if (inverseMatchScore === 0 || differenceScore === 0) {
|
|
37
38
|
return 100;
|
|
38
39
|
}
|
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
|
}
|
|
@@ -22,7 +25,7 @@ function compareDataSets(data1: FPDataSet, data2: FPDataSet): [number, number] {
|
|
|
22
25
|
|
|
23
26
|
export function calculateConfidence(data1: FPDataSet, data2: FPDataSet): number {
|
|
24
27
|
// Compare how many fields are the same in both datasets
|
|
25
|
-
const [fields, matches] =
|
|
28
|
+
const [fields, matches] = compareDatasets(data1, data2);
|
|
26
29
|
|
|
27
30
|
if (fields === 0 || matches === 0) {
|
|
28
31
|
return 0;
|
|
@@ -36,7 +39,7 @@ export function calculateConfidence(data1: FPDataSet, data2: FPDataSet): number
|
|
|
36
39
|
const differenceScore = compareHashes(hash1, hash2);
|
|
37
40
|
|
|
38
41
|
const inverseMatchScore = 1 - (matches / fields);
|
|
39
|
-
const x =
|
|
42
|
+
const x = differenceScore * inverseMatchScore
|
|
40
43
|
if (inverseMatchScore === 0 || differenceScore === 0) {
|
|
41
44
|
return 100;
|
|
42
45
|
}
|
package/tests/confidence.test.ts
CHANGED
|
@@ -98,4 +98,15 @@ describe('Confidence Calculation', () => {
|
|
|
98
98
|
expect(confidence).toBeGreaterThan(10);
|
|
99
99
|
expect(confidence).toBeLessThan(95);
|
|
100
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
|
+
});
|
|
101
112
|
});
|