Class: Tlsh::TlshInstance

Inherits:
Object
  • Object
show all
Defined in:
lib/tlsh/tlsh_instance.rb

Overview

TlshInstance represents single TLSH instance.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ TlshInstance

Creates new instance of TlshInstance from the named arguments.



9
10
11
12
13
14
# File 'lib/tlsh/tlsh_instance.rb', line 9

def initialize(params = {})
  params.each do |key, value|
    setter = "#{key}="
    send(setter, value) if respond_to?(setter.to_sym, false)
  end
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



4
5
6
# File 'lib/tlsh/tlsh_instance.rb', line 4

def body
  @body
end

#checksumObject

Returns the value of attribute checksum.



4
5
6
# File 'lib/tlsh/tlsh_instance.rb', line 4

def checksum
  @checksum
end

#l_valueObject

Returns the value of attribute l_value.



4
5
6
# File 'lib/tlsh/tlsh_instance.rb', line 4

def l_value
  @l_value
end

#q1_ratioObject

Returns the value of attribute q1_ratio.



4
5
6
# File 'lib/tlsh/tlsh_instance.rb', line 4

def q1_ratio
  @q1_ratio
end

#q2_ratioObject

Returns the value of attribute q2_ratio.



4
5
6
# File 'lib/tlsh/tlsh_instance.rb', line 4

def q2_ratio
  @q2_ratio
end

#q_ratioObject

Returns the value of attribute q_ratio.



4
5
6
# File 'lib/tlsh/tlsh_instance.rb', line 4

def q_ratio
  @q_ratio
end

Instance Method Details

#binaryObject

Returns the binary representation of the TLSH hash.

It’s constructed as a concatenation of hash metadata and body,



30
31
32
# File 'lib/tlsh/tlsh_instance.rb', line 30

def binary
  [swap_byte(checksum), swap_byte(l_value), q_ratio] + body
end

#comparable?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/tlsh/tlsh_instance.rb', line 43

def comparable?
  checksum && l_value && q1_ratio && q2_ratio && q_ratio && body
end

#diff(other) ⇒ Object

Returns diff (or similarity) against another TlshInstance.

The closer to 0, the smaller the diff. Both instances have to be comparable for comparison. If not, -1 is returned.



21
22
23
# File 'lib/tlsh/tlsh_instance.rb', line 21

def diff(other)
  Distance.diff_total(self, other, true)
end

#stringObject

Returns the string representation of the TLSH hash.

It’s constructed from the binary representation of the hash, converted to hex



39
40
41
# File 'lib/tlsh/tlsh_instance.rb', line 39

def string
  binary.map { |i| i.to_i.to_s(16) }.join('')
end