Class: Ssdeep::FuzzyHash

Inherits:
FFI::MemoryPointer
  • Object
show all
Defined in:
lib/ssdeep/fuzzy_hash.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFuzzyHash

Returns a new instance of FuzzyHash.



9
10
11
# File 'lib/ssdeep/fuzzy_hash.rb', line 9

def initialize()
  super(FUZZY_MAX_RESULT)
end

Class Method Details

._load(raw) ⇒ Object

implements a _load method for Marshal



44
45
46
# File 'lib/ssdeep/fuzzy_hash.rb', line 44

def self._load(raw)
  from_hash(raw)
end

.from_file(filename) ⇒ Object

Creates a new fuzzy hash from the contents of ‘filename’



76
77
78
79
80
81
82
83
84
85
# File 'lib/ssdeep/fuzzy_hash.rb', line 76

def self.from_file(filename)
  fh = new()
  ret = Ssdeep.fuzzy_hash_filename(filename, fh)
  if ret == 0
    return fh
  else
    fh.free
    raise(StandardError, "An error occurred hashing file: #{filename.inspect}")
  end
end

.from_hash(raw) ⇒ Object

Restores a fuzzy hash that has already been generated and supplied as a string as a instance of a FuzzyHash object.



50
51
52
53
54
55
56
57
58
59
# File 'lib/ssdeep/fuzzy_hash.rb', line 50

def self.from_hash(raw)
  fh = new()
  fh.write_string( 
    if raw.size < FUZZY_MAX_RESULT 
      raw + "\x00" 
    else 
      raw[0,FUZZY_MAX_RESULT]
    end )
  return fh
end

.from_string(buf) ⇒ Object

Creates a new fuzzy hash from a string buffer.



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ssdeep/fuzzy_hash.rb', line 62

def self.from_string(buf)
  fh = new()
  p = FFI::MemoryPointer.new(buf.size)
  p.write_string(buf)
  ret = Ssdeep.fuzzy_hash_buf(p, buf.size, fh)
  if ret == 0
    return fh
  else
    fh.free
    raise(StandardError, "An error occurred hashing a string")
  end
end

Instance Method Details

#_dump(depth) ⇒ Object

implements a _dump method for Marshal



19
20
21
# File 'lib/ssdeep/fuzzy_hash.rb', line 19

def _dump(depth)
  self.to_s
end

#compare(other) ⇒ Integer, ...

Returns:

  • (Integer)

    Returns a value from zero to 100 indicating the match score of the two signatures. A match score of zero indicates the sigantures did not match.

  • (When an error occurs, such as if one of the inputs is NULL.)

    When an error occurs, such as if one of the inputs is NULL.



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ssdeep/fuzzy_hash.rb', line 30

def compare(other)
  unless other.is_a?(FuzzyHash)
    raise(TypeError, "a FuzzyHash can only be compared to another FuzzyHash")
  end
  if self.to_s == other.to_s
    return 100
  elsif (ret=Ssdeep.fuzzy_compare(self, other)) > -1
    return ret
  else
    raise(StandardError, "unknown fuzzy hash comparison error")
  end
end

#to_sObject

returns the computed fuzzy hash as a string



14
15
16
# File 'lib/ssdeep/fuzzy_hash.rb', line 14

def to_s
  self.read_string()
end