Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it consistent with trendmicro latest implementation #26

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ func (t *TLSH) Size() int {

func (t *TLSH) Sum(b []byte) []byte {
q1, q2, q3 := quartilePoints(t.state.buckets)
if q3 == 0 || t.state.fileSize < 50 {
*t = TLSH{
state: t.state,
}
return t.Binary()
}
q1Ratio := byte(float32(q1)*100/float32(q3)) % 16
q2Ratio := byte(float32(q2)*100/float32(q3)) % 16
qRatio := ((q1Ratio & 0xF) << 4) | (q2Ratio & 0xF)
Expand Down
28 changes: 26 additions & 2 deletions hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func TestHashWrite(t *testing.T) {

// hash from read
h2, err := HashBytes([]byte("1234111111111"))
if err != nil {
t.Error(err)
if err == nil {
t.Error("Missing error of less than 50 bytes")
}
t.Logf("checksum h2: %d, %x", h2.state.checksum, h2.checksum)
t.Log(fmt.Sprintf("h2: %x", h2.Binary()))
Expand All @@ -50,4 +50,28 @@ func TestHashWrite(t *testing.T) {
if diff != 0 {
t.Errorf("hashes differ by: %d", diff)
}

h1.Write([]byte("1234567890"))
h1.Write([]byte("1234567890"))
h1.Write([]byte("1234567890"))
h1.Write([]byte("1234567890"))
t.Log(fmt.Sprintf("h1: %x", h1.Sum(nil)))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use t.Logf instead

t.Logf("checksum h1: %d, %x", h1.state.checksum, h1.checksum)
s := "1234111111111" + "1234567890" + "1234567890" + "1234567890" + "1234567890"
h3, err := HashBytes([]byte(s))
if err != nil {
t.Error(err)
}
t.Logf("checksum h3: %d, %x", h3.state.checksum, h3.checksum)
t.Log(fmt.Sprintf("h3: %x", h3.Binary()))
if h1.state.fileSize != h3.state.fileSize {
t.Errorf("file size mismatch: %d != %d", h1.state.fileSize, h3.state.fileSize)
}
if h1.checksum != h3.checksum {
t.Errorf("checksum mismatch: %x != %x", h1.checksum, h3.checksum)
}
diff = h1.Diff(h3)
if diff != 0 {
t.Errorf("hashes differ by: %d", diff)
}
}
1 change: 1 addition & 0 deletions tests/test_file_49bytes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MIT License is so cool license that I can't imag
1 change: 1 addition & 0 deletions tests/test_file_q3zero
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1234560000000000000000000000000000000000000000000
23 changes: 21 additions & 2 deletions tlsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io"
"math"
"os"
"errors"
)

const (
Expand Down Expand Up @@ -334,10 +335,28 @@ func fillBuckets(r FuzzyReader) ([numBuckets]uint, byte, int, error) {
func hashCalculate(r FuzzyReader) (*TLSH, error) {
buckets, checksum, fileSize, err := fillBuckets(r)
if err != nil {
return &TLSH{}, err
return &TLSH{}, err
}
if fileSize < 50 {
return &TLSH{
state: chunkState{
buckets: buckets,
fileSize: fileSize,
checksum: checksum,
},
}, errors.New("less than 50 bytes")
}

q1, q2, q3 := quartilePoints(buckets)
if q3 == 0 {
return &TLSH{
state: chunkState{
buckets: buckets,
fileSize: fileSize,
checksum: checksum,
},
}, errors.New("q3 is zero")
}
q1Ratio := byte(float32(q1)*100/float32(q3)) % 16
q2Ratio := byte(float32(q2)*100/float32(q3)) % 16
qRatio := ((q1Ratio & 0xF) << 4) | (q2Ratio & 0xF)
Expand All @@ -364,7 +383,7 @@ type FuzzyReader interface {
func HashReader(r FuzzyReader) (*TLSH, error) {
t, err := hashCalculate(r)
if err != nil {
return &TLSH{}, err
return &TLSH{ state : t.state }, err
}
return t, err
}
Expand Down
2 changes: 2 additions & 0 deletions tlsh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ var (
{"tests/test_file_9_tinyssl.exe", "67a3ad97f601c873e11a0af49d83d2d6bc7f7f709e522c9b74990b0e8d796822d1d48a"},
{"tests/NON_EXISTENT", "0000000000000000000000000000000000000000000000000000000000000000000000"},
{"tests/test_file_empty", "0000000000000000000000000000000000000000000000000000000000000000000000"},
{"tests/test_file_q3zero", "0000000000000000000000000000000000000000000000000000000000000000000000"},
{"tests/test_file_49bytes", "0000000000000000000000000000000000000000000000000000000000000000000000"},
}

diffTestCases = []struct {
Expand Down