class Tlsh::TlshInstance

TlshInstance represents single TLSH instance.

Attributes

body[RW]
checksum[RW]
l_value[RW]
q1_ratio[RW]
q2_ratio[RW]
q_ratio[RW]

Public Class Methods

new(params = {}) click to toggle source

Creates new instance of TlshInstance from the named arguments.

# 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

Public Instance Methods

binary() click to toggle source

Returns the binary representation of the TLSH hash.

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

# File lib/tlsh/tlsh_instance.rb, line 30
def binary
  [swap_byte(checksum), swap_byte(l_value), q_ratio] + body
end
comparable?() click to toggle source
# File lib/tlsh/tlsh_instance.rb, line 43
def comparable?
  checksum && l_value && q1_ratio && q2_ratio && q_ratio && body
end
diff(other) click to toggle source

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.

# File lib/tlsh/tlsh_instance.rb, line 21
def diff(other)
  Distance.diff_total(self, other, true)
end
string() click to toggle source

Returns the string representation of the TLSH hash.

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

# File lib/tlsh/tlsh_instance.rb, line 39
def string
  binary.map { |i| i.to_i.to_s(16) }.join('')
end

Private Instance Methods

swap_byte(input) click to toggle source
# File lib/tlsh/tlsh_instance.rb, line 49
def swap_byte(input)
  out = ((input & 0xF0) >> 4) & 0x0F
  out | ((input & 0x0F) << 4) & 0xF0
end