From 8052b8714b8a7793db6330a9215bdfb618811b42 Mon Sep 17 00:00:00 2001 From: python333 <32724788+python333@users.noreply.github.com> Date: Sat, 3 Mar 2018 20:05:31 +0800 Subject: [PATCH 1/2] Parsing the hash of the string type When we have a hash, we can use this function to get the object to compare --- tlsh.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tlsh.go b/tlsh.go index 830a49f..aa9234e 100644 --- a/tlsh.go +++ b/tlsh.go @@ -65,6 +65,22 @@ func (t *Tlsh) String() string { return hex.EncodeToString(t.Binary()) } +// Parsing the hash of the string type +func ParseStringToTlsh(hashString string) (*Tlsh, error) { + var code [codeSize]byte + hashByte, err := hex.DecodeString(hashString) + if err != nil { + return &Tlsh{}, err + } + chechsum := swapByte(hashByte[0]) + lValue := swapByte(hashByte[1]) + qRatio := hashByte[2] + q1Ratio := (qRatio >> 4) & 0xF + q2Ratio := qRatio & 0xF + copy(code[:], hashByte[3:]) + return New(chechsum, lValue, q1Ratio, q2Ratio, qRatio, code), nil +} + func quartilePoints(buckets [numBuckets]uint) (q1, q2, q3 uint) { var spl, spr uint p1 := uint(effBuckets/4 - 1) From dd8a6814436cc5fc267ad34a9590758fb293e9eb Mon Sep 17 00:00:00 2001 From: Fedor Melekhin Date: Thu, 25 Jul 2019 19:56:05 +0500 Subject: [PATCH 2/2] Added test for ParseStringToTlsh function --- tlsh_test.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tlsh_test.go b/tlsh_test.go index 6442aa9..71ffc80 100644 --- a/tlsh_test.go +++ b/tlsh_test.go @@ -79,6 +79,19 @@ func TestDiff(t *testing.T) { } } +func TestParseStringToTlsh(t *testing.T) { + for _, tc := range hashTestCases { + if hash, err := ParseStringToTlsh(tc.hash); err != nil || hash.String() != tc.hash { + if err != nil { + t.Error(err) + } + if hash.String() != tc.hash { + t.Errorf("\noriginal and parsed tlsh have different hash %s vs. %s\n", tc.hash, hash.String()) + } + } + } +} + func BenchmarkPearson(b *testing.B) { var salt = byte(0) var keys = [3]byte{1, 3, 7}