Module: FFI::Ssdeep

Extended by:
Library
Defined in:
lib/ffi/ssdeep.rb

Defined Under Namespace

Classes: HashError

Constant Summary

SPAMSUM_LENGTH =
64
FUZZY_MAX_RESULT =
(SPAMSUM_LENGTH + (SPAMSUM_LENGTH/2 + 20))

Class Method Summary (collapse)

Class Method Details

+ (Object) compare(sig1, sig2)

Compare two hashes

Parameters:

  • String

    sig1 A fuzzy hash which will be compared to sig2

  • String

    sig2 A fuzzy hash which will be compared to sig1

Returns:

  • Integer A value between 0 and 100 indicating the percentage of similarity.



100
101
102
103
104
105
# File 'lib/ffi/ssdeep.rb', line 100

def self.compare(sig1, sig2)
  psig1 = FFI::MemoryPointer.from_string(sig1)
  psig2 = FFI::MemoryPointer.from_string(sig2)

  return fuzzy_compare(psig1, psig2)
end

+ (Object) from_file(filename)

Create a fuzzy hash from a file

Parameters:

  • String

    fielname The file to read and hash

Returns:

  • String The fuzzy hash of the file input

Raises:

  • HashError An exception is raised if the libfuzzy library encounters an error.



81
82
83
84
85
86
87
88
89
90
# File 'lib/ffi/ssdeep.rb', line 81

def self.from_file(filename)
  File.stat(filename)
  out = FFI::MemoryPointer.new(FUZZY_MAX_RESULT)

  if (ret=fuzzy_hash_filename(filename, out)) == 0
    return out.read_string
  else
    raise(HashError, "An error occurred hashing the file: #{filename} -- ret=#{ret}");
  end
end

+ (Object) from_fileno(fileno)

Create a fuzzy hash from a file descriptor fileno

Parameters:

  • Integer

    fileno The file descriptor to read and hash

Returns:

  • String The fuzzy hash of the file descriptor input

Raises:

  • HashError An exception is raised if the libfuzzy library encounters an error.



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ffi/ssdeep.rb', line 59

def self.from_fileno(fileno)
  if (not fileno.kind_of?(Integer)) or (file=fdopen(fileno, "r")).null?
    raise(HashError, "Unable to open file descriptor: #{fileno}")
  end

  out = FFI::MemoryPointer.new(FUZZY_MAX_RESULT)

  if (ret=fuzzy_hash_file(file, out)) == 0
    return out.read_string
  else
    raise(HashError, "An error occurred hashing the file descriptor: #{fileno} -- ret=#{ret}")
  end
end

+ (Object) from_string(buf)

Create a fuzzy hash from a ruby string

Parameters:

  • String

    buf The string to hash

Returns:

  • String The fuzzy hash of the string

Raises:

  • HashError An exception is raised if the libfuzzy library encounters an error.



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ffi/ssdeep.rb', line 38

def self.from_string(buf)
  bufp = FFI::MemoryPointer.new(buf.size)
  bufp.write_string(buf)

  out = FFI::MemoryPointer.new(FUZZY_MAX_RESULT)

  if (ret=fuzzy_hash_buf(bufp, bufp.size, out)) == 0
    return out.read_string
  else
    raise(HashError, "An error occurred hashing a string: ret=#{ret}")
  end
end