Skip to content

Commit

Permalink
Merge dd8a681 into 0057c01
Browse files Browse the repository at this point in the history
  • Loading branch information
melehin committed Jul 25, 2019
2 parents 0057c01 + dd8a681 commit 9bb6f39
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
16 changes: 16 additions & 0 deletions tlsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions tlsh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit 9bb6f39

Please sign in to comment.