Module: NilsimsaLite

Defined in:
lib/nilsimsa_lite.rb,
lib/nilsimsa_lite/version.rb,
ext/nilsimsa_lite/nilsimsa_lite.c

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.compare(a, b) ⇒ Object

Compares two given Nilsimsa hashes. Returns a score between -127 and 128 where -127 means completely uncorrelated data and 128 means same data.

Note that it doesn’t check whether the passed hashes are valid: if you pass shorter or longer strings, or of invalid format, you will get a score between -127 and 128, but it won’t mean anything.

Parameters:

  • a (String)

    Nilsimsa hash of a string

  • b (String)

    Nilsimsa hash of another string

Returns:

  • Integer Score between -127 and 128. -127 means uncorrelated data and 128 means same data.



35
36
37
38
39
40
41
42
43
# File 'ext/nilsimsa_lite/nilsimsa_lite.c', line 35

VALUE rb_nilsimsa_compare(VALUE self, VALUE a, VALUE b) {
  if (rb_type(a) != T_STRING || rb_type(b) != T_STRING) rb_raise(rb_eArgError, "can only compare digest strings");

  char *string_a = RSTRING_PTR(a);
  char *string_b = RSTRING_PTR(b);

  VALUE ruby_int = INT2NUM(nilsimsa_compare(string_a, string_b));
  return ruby_int;
}

.digest(input) ⇒ String

Computes the Nilsimsa hash of the given data. Returns a 64 character long string representing the digest.

Parameters:

  • input (String)

Returns:

  • (String)

    64 hexadecimal characters string.



11
12
13
14
15
16
17
18
19
20
21
# File 'ext/nilsimsa_lite/nilsimsa_lite.c', line 11

VALUE rb_nilsimsa_digest(VALUE self, VALUE input) {
  if (rb_type(input) != T_STRING) rb_raise(rb_eArgError, "can only digest strings");

  char *string = RSTRING_PTR(input);
  char hash[65] = { 0 };

  nilsimsa_compute(string, strlen(string), hash);

  VALUE ruby_str = rb_str_new2(hash);
  return ruby_str;
}