Module: Ssdeep

Defined in:
lib/ssdeep.rb,
lib/russdeep.rb,
ext/ssdeep_native/ssdeep_native.c

Defined Under Namespace

Modules: FuzzyComparable Classes: HashError

Constant Summary collapse

FUZZY_MAX_RESULT =
INT2NUM(FUZZY_MAX_RESULT)
SPAMSUM_LENGTH =
INT2NUM(SPAMSUM_LENGTH)

Class Method Summary collapse

Class Method Details

.compare(sig1, sig2) ⇒ Object

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.



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'ext/ssdeep_native/ssdeep_native.c', line 97

VALUE ssdeep_compare(VALUE klass, VALUE sig1, VALUE sig2) {
  int ret;

  Check_Type(sig1, T_STRING);
  Check_Type(sig2, T_STRING);

  if (strncmp(RSTRING_PTR(sig1), RSTRING_PTR(sig2), FUZZY_MAX_RESULT) == 0)
    return INT2NUM(100);
  else if ( (ret=fuzzy_compare(RSTRING_PTR(sig1), RSTRING_PTR(sig2))) < 0)
    rb_raise(error_HashError, "An error occurred comparing hashes");
  else
    return INT2NUM(ret);
}

.from_file(filename) ⇒ Object Also known as: hash_file

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.



42
43
44
45
46
47
48
49
50
51
52
# File 'ext/ssdeep_native/ssdeep_native.c', line 42

VALUE ssdeep_from_filename(VALUE klass, VALUE filename) {
  char hash[FUZZY_MAX_RESULT];
  int ret;
  Check_Type(filename, T_STRING);

  ret=fuzzy_hash_filename(RSTRING_PTR(filename), hash);
  if (ret == 0)
    return rb_str_new2(hash);
  else
    rb_raise(error_HashError, "An error occurred the file: %s -- ret=%i", RSTRING_PTR(filename), ret);
}

.from_fileno(fileno) ⇒ Object Also known as: hash_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.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'ext/ssdeep_native/ssdeep_native.c', line 65

VALUE ssdeep_from_fileno(VALUE klass, VALUE fileno) {
  char hash[FUZZY_MAX_RESULT];
  int ret, fd;
  FILE *file;

  Check_Type(fileno, T_FIXNUM);

  fd = FIX2INT(fileno);

  if (!(file=fdopen(fd, "r"))) {
    rb_sys_fail(0);
    return Qnil;
  }

  ret=fuzzy_hash_file(file, hash);
  if (ret == 0)
    return rb_str_new2(hash);
  else
    rb_raise(error_HashError, "An error occurred the fileno: %i -- ret=%i", fd, ret);
}

.from_string(buf) ⇒ Object Also known as: hash_string

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.



19
20
21
22
23
24
25
26
27
28
29
# File 'ext/ssdeep_native/ssdeep_native.c', line 19

VALUE ssdeep_from_string(VALUE klass, VALUE buf) {
  char hash[FUZZY_MAX_RESULT];
  int ret;
  Check_Type(buf, T_STRING);

  ret = fuzzy_hash_buf((unsigned char *) RSTRING_PTR(buf), RSTRING_LEN(buf), hash);
  if (ret == 0)
    return rb_str_new2(hash);
  else
    rb_raise(error_HashError, "An error occurred hashing a string: ret=%i", ret);
}