forked from glaslos/ssdeep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscore.go
80 lines (70 loc) · 2.09 KB
/
score.go
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
package ssdeep
import (
"math"
"strings"
"strconv"
"errors"
)
// Distance computes the match score between two fuzzy hash signatures.
// Returns a value from zero to 100 indicating the match score of the two signatures.
// A match score of zero indicates the signatures did not match.
// Returns an error when one of the inputs are not valid signatures.
func Distance(hash1, hash2 string) (score int, err error) {
hash1BlockSize, hash1String1, hash1String2, err := splitSsdeep(hash1)
if err != nil {
return
}
hash2BlockSize, hash2String1, hash2String2, err := splitSsdeep(hash2)
if err != nil {
return
}
if hash1BlockSize == hash2BlockSize && hash1String1 == hash2String1 {
return 100, nil
}
// We can only compare equal or *2 block sizes
if hash1BlockSize != hash2BlockSize && hash1BlockSize != hash2BlockSize*2 && hash2BlockSize != hash1BlockSize*2 {
return
}
if hash1BlockSize == hash2BlockSize {
d1 := scoreDistance(hash1String1, hash2String1, hash1BlockSize)
d2 := scoreDistance(hash1String2, hash2String2, hash1BlockSize*2)
score = int(math.Max(float64(d1), float64(d2)))
} else if hash1BlockSize == hash2BlockSize*2 {
score = scoreDistance(hash1String1, hash2String2, hash1BlockSize)
} else {
score = scoreDistance(hash1String2, hash2String1, hash2BlockSize)
}
return
}
func splitSsdeep(hash string) (blockSize int, hashString1, hashString2 string, err error) {
if hash == "" {
err = errors.New("empty string")
return
}
parts := strings.Split(hash, ":")
if len(parts) != 3 {
err = errors.New("invalid ssdeep format")
return
}
blockSize, err = strconv.Atoi(parts[0])
if err != nil {
err = errors.New("invalid ssdeep format")
return
}
hashString1 = parts[1]
hashString2 = parts[2]
return
}
func scoreDistance(h1, h2 string, blockSize int) int {
d := distance(h1, h2)
d = (d * spamSumLength) / (len(h1) + len(h2))
d = (100 * d) / spamSumLength
d = 100 - d
/* TODO: Figure out this black magic...
matchSize := float64(blockSize) / float64(blockMin) * math.Min(float64(len(h1)), float64(len(h2)))
if d > int(matchSize) {
d = int(matchSize)
}
*/
return d
}