nilsimsa_lite 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c7838c1a839f2679bab3d90f107ac0226151adb1e4d000c077d9e1a99ccb84d7
4
+ data.tar.gz: 53ea9849b1b013a11136f9e12c35ba45072cfd69a36aedd4801450013e8b908d
5
+ SHA512:
6
+ metadata.gz: 5051618cb4d5abe3cff58cdf1675394b0228aa51019ff252cd3f3c512cf492fa7020ae9a3a7f68999486513a7e57e3afa778b52ec18f0e0fee4a4f5fa47e5152
7
+ data.tar.gz: 71d6298a7c00b21e9357e5f3e32bd579c05fe6b0be8141764df969ecf3b1418c12fab35214d6f04e718c75417c9091c0b15ec6d46c072693d50de73ad03021f2
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2026-06-11
4
+
5
+ - Initial release
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Sergio Gil
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # NilsimsaLite
2
+
3
+ Ruby wrapper for [nilsimsa-lite](https://github.com/3p3r/nilsimsa-lite).
4
+
5
+ > In its most simplistic explanation, Nilsimsa produces the same hash if the data it represents is
6
+ > "almost" identical. Compare this to cryptographic hash functions (such as MD5) where hashes are
7
+ > identical if the data it represents are identical.
8
+
9
+ This makes it useful for things like spam detection, etc.
10
+
11
+ ## Installation
12
+
13
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
14
+
15
+ Install the gem and add to the application's Gemfile by executing:
16
+
17
+ ```bash
18
+ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
19
+ ```
20
+
21
+ If bundler is not being used to manage dependencies, install the gem by executing:
22
+
23
+ ```bash
24
+ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ ```ruby
30
+ require 'nilsimsa_lite'
31
+
32
+ d1 = NilsimsaLite.digest("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")
33
+ #=> "5D9E4CB0061ECD485062E26FA8273991A7A9C9C36A57E33C2E18AA13F7E4F0EE"
34
+
35
+ d2 = NilsimsaLite.digest("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.".gsub('dolor', 'delectatio'))
36
+ #=> "55DC5CB10716C8587022E275BC203991A781C9C36A56672C2E18AA03D3F4A467"
37
+
38
+ NilsimsaLite.compare(d1, d2)
39
+ => 92
40
+ ```
41
+
42
+ According to the [original
43
+ implementation](https://web.archive.org/web/20150308093244/http://ixazon.dynip.com/~cmeclax/nilsimsa.html),
44
+ _"any nilsimsa over 24 (which is 3 sigma) indicates that the two messages are probably not
45
+ independently generated."_
46
+
47
+ ## Contributing
48
+
49
+ Bug reports and pull requests are welcome on Codeberg at https://codeberg.org/sgilperez/nilsimsa_lite. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://codeberg.org/sgilperez/nilsimsa_lite/blob/main/CODE_OF_CONDUCT.md).
50
+
51
+ ## Code of Conduct
52
+
53
+ Everyone interacting in the NilsimsaLite project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://codeberg.org/sgilperez/nilsimsa_lite/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "standard/rake"
9
+
10
+ require "rake/extensiontask"
11
+
12
+ task build: :compile
13
+
14
+ GEMSPEC = Gem::Specification.load("nilsimsa_lite.gemspec")
15
+
16
+ Rake::ExtensionTask.new("nilsimsa_lite", GEMSPEC) do |ext|
17
+ ext.lib_dir = "lib/nilsimsa_lite"
18
+ end
19
+
20
+ task default: %i[clobber compile spec standard]
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Sepehr Laal
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,51 @@
1
+ # nilsimsa-lite
2
+ A minimal C implementation of the Nilsimsa algorithm
3
+
4
+ ## What is Nilsimsa?
5
+ In its most simplistic explanation, Nilsimsa produces the same hash if the data it represents is "almost" identical. Compare this to cryptographic hash functions (such as MD5) where hashes are identical iff the data it represents are identical.
6
+
7
+ ## Build
8
+ There is no dependency in the source. Unit tests require `<assert.h>` and `<string.h>`. To build:
9
+
10
+ * Linux
11
+ ```Bash
12
+ $> cd <root of the repo>
13
+ $> mkdir build && cd build
14
+ $> cmake ..
15
+ $> make
16
+ ```
17
+
18
+ * Windows
19
+ ```Batch
20
+ $> cd <root of the repo>
21
+ $> mkdir build && cd build
22
+ $> cmake .. -G "Visual Studio 14 Win64"
23
+ ```
24
+ Open up the solution and build.
25
+
26
+ ## Example Usage
27
+ ```C
28
+ // input text to be hashed
29
+ const char data[] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam cursus lacinia condimentum. Integer feugiat tortor sed massa lobortis eleifend vel eu diam. Nullam libero ligula, accumsan eu sollicitudin et, rhoncus id lorem. Etiam lacinia erat id feugiat pulvinar. Nulla scelerisque, libero vel commodo malesuada, lorem mi ultricies arcu, eget rutrum lorem velit sed ipsum. Cras ac tellus in mauris eleifend elementum. Curabitur tempor sed massa sed dignissim.";
30
+
31
+ // -1 to compensate for trailing null character
32
+ int data_size = (sizeof(data) / data(data[0])) - 1;
33
+
34
+ // pre-allocate hash's string
35
+ char hash[65] = { 0 };
36
+ nilsimsa_compute(data, data_size, hash);
37
+
38
+ // hash is now:
39
+ 5DF842A0C27C234DF170AB95440675C10E82294B181438D87D09CE4CB7C4F47E
40
+ ```
41
+
42
+ ## API Documentation
43
+ - `nilsimsa_compute`
44
+ > computes the Nilsimsa hash of the given data. Returns a 64 character long string representing the digest.
45
+
46
+ - `nilsimsa_compare`
47
+ > compares two given Nilsimsa hashes. Returns a score between -127 and 128 where -127 means completely uncorrelated data and 128 means same data.
48
+
49
+ ## Notes
50
+ This is a minimal port of [rholder/nilsimsa](https://github.com/rholder/nilsimsa) to C.
51
+ The reference C implementation can be found [here](http://ixazon.dynip.com/~cmeclax/nilsimsa.html).
@@ -0,0 +1,218 @@
1
+ /*!
2
+ * The MIT License (MIT)
3
+ * =====================
4
+ *
5
+ * Copyright 2017 Sepehr Laal
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in
15
+ * all copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ */
25
+
26
+ #include "nilsimsa.h"
27
+
28
+ #ifdef __cplusplus
29
+ extern "C" {
30
+ #endif
31
+
32
+ static int __score(int num);
33
+ static int __tran3(int a, int b, int c, int n);
34
+ static int __hexchar_to_int(char digit);
35
+ static const char* __int_to_hexchar(int num);
36
+
37
+ #define ACCUM_LENGTH 256
38
+ #define DIGEST_LENGTH 32
39
+ #define WINDOW_LENGTH 4
40
+
41
+ void nilsimsa_compute(const char* data, int n, char* out)
42
+ {
43
+ int accum[ACCUM_LENGTH] = { 0 };
44
+ int digest[DIGEST_LENGTH] = { 0 };
45
+ int window[WINDOW_LENGTH] = { -1, -1, -1, -1 };
46
+
47
+ for (int i = 0; i < n; ++i)
48
+ {
49
+ int ch = data[i] & 0xFF;
50
+
51
+ if (window[1] > -1)
52
+ {
53
+ accum[__tran3(ch, window[0], window[1], 0)] += 1;
54
+ }
55
+
56
+ if (window[2] > -1)
57
+ {
58
+ accum[__tran3(ch, window[0], window[2], 1)] += 1;
59
+ accum[__tran3(ch, window[1], window[2], 2)] += 1;
60
+ }
61
+
62
+ if (window[3] > -1)
63
+ {
64
+ accum[__tran3(ch, window[0], window[3], 3)] += 1;
65
+ accum[__tran3(ch, window[1], window[3], 4)] += 1;
66
+ accum[__tran3(ch, window[2], window[3], 5)] += 1;
67
+ accum[__tran3(window[3], window[0], ch, 6)] += 1;
68
+ accum[__tran3(window[3], window[2], ch, 7)] += 1;
69
+ }
70
+
71
+ window[3] = window[2];
72
+ window[2] = window[1];
73
+ window[1] = window[0];
74
+ window[0] = ch;
75
+ }
76
+
77
+ int total = 0;
78
+
79
+ if (n == 3)
80
+ total = 1;
81
+ else if (n == 4)
82
+ total = 4;
83
+ else if (n > 4)
84
+ total = 8 * n - 28;
85
+
86
+ int threshold = total / ACCUM_LENGTH;
87
+
88
+ for (int i = 0; i < ACCUM_LENGTH; i++)
89
+ if (accum[i] > threshold)
90
+ digest[i >> 3] += 1 << (i & 7);
91
+
92
+ int rev_index = 0;
93
+
94
+ for (int i = 0; i < DIGEST_LENGTH; ++i)
95
+ {
96
+ rev_index = DIGEST_LENGTH - i - 1;
97
+ out[2*i ] = __int_to_hexchar(digest[rev_index])[0];
98
+ out[2*i+1] = __int_to_hexchar(digest[rev_index])[1];
99
+ }
100
+
101
+ out[2 * DIGEST_LENGTH] = '\0';
102
+ }
103
+
104
+ int nilsimsa_compare(const char* hash1, const char* hash2)
105
+ {
106
+ int digest1[DIGEST_LENGTH] = { 0 };
107
+ int digest2[DIGEST_LENGTH] = { 0 };
108
+
109
+ for (int i = 0; i < DIGEST_LENGTH; ++i)
110
+ {
111
+ digest1[i] = (__hexchar_to_int(hash1[2 * i]) << 4) + __hexchar_to_int(hash1[2 * i + 1]);
112
+ digest2[i] = (__hexchar_to_int(hash2[2 * i]) << 4) + __hexchar_to_int(hash2[2 * i + 1]);
113
+ }
114
+
115
+ int bits = 0;
116
+ for (int i = 0; i < DIGEST_LENGTH; i++)
117
+ bits += __score(255 & (digest1[i] ^ digest2[i]));
118
+ return 128 - bits;
119
+ }
120
+
121
+ //////////////////////////////////////////////////////////////////////////
122
+ // implementation detail
123
+
124
+ // This is a precomputed constant for the standard Nilsimsa "53"-based transition table.
125
+ static const int __TRAN53[] =
126
+ {
127
+ 0x02, 0xD6, 0x9E, 0x6F, 0xF9, 0x1D, 0x04, 0xAB, 0xD0, 0x22, 0x16, 0x1F, 0xD8, 0x73, 0xA1, 0xAC,
128
+ 0x3B, 0x70, 0x62, 0x96, 0x1E, 0x6E, 0x8F, 0x39, 0x9D, 0x05, 0x14, 0x4A, 0xA6, 0xBE, 0xAE, 0x0E,
129
+ 0xCF, 0xB9, 0x9C, 0x9A, 0xC7, 0x68, 0x13, 0xE1, 0x2D, 0xA4, 0xEB, 0x51, 0x8D, 0x64, 0x6B, 0x50,
130
+ 0x23, 0x80, 0x03, 0x41, 0xEC, 0xBB, 0x71, 0xCC, 0x7A, 0x86, 0x7F, 0x98, 0xF2, 0x36, 0x5E, 0xEE,
131
+ 0x8E, 0xCE, 0x4F, 0xB8, 0x32, 0xB6, 0x5F, 0x59, 0xDC, 0x1B, 0x31, 0x4C, 0x7B, 0xF0, 0x63, 0x01,
132
+ 0x6C, 0xBA, 0x07, 0xE8, 0x12, 0x77, 0x49, 0x3C, 0xDA, 0x46, 0xFE, 0x2F, 0x79, 0x1C, 0x9B, 0x30,
133
+ 0xE3, 0x00, 0x06, 0x7E, 0x2E, 0x0F, 0x38, 0x33, 0x21, 0xAD, 0xA5, 0x54, 0xCA, 0xA7, 0x29, 0xFC,
134
+ 0x5A, 0x47, 0x69, 0x7D, 0xC5, 0x95, 0xB5, 0xF4, 0x0B, 0x90, 0xA3, 0x81, 0x6D, 0x25, 0x55, 0x35,
135
+ 0xF5, 0x75, 0x74, 0x0A, 0x26, 0xBF, 0x19, 0x5C, 0x1A, 0xC6, 0xFF, 0x99, 0x5D, 0x84, 0xAA, 0x66,
136
+ 0x3E, 0xAF, 0x78, 0xB3, 0x20, 0x43, 0xC1, 0xED, 0x24, 0xEA, 0xE6, 0x3F, 0x18, 0xF3, 0xA0, 0x42,
137
+ 0x57, 0x08, 0x53, 0x60, 0xC3, 0xC0, 0x83, 0x40, 0x82, 0xD7, 0x09, 0xBD, 0x44, 0x2A, 0x67, 0xA8,
138
+ 0x93, 0xE0, 0xC2, 0x56, 0x9F, 0xD9, 0xDD, 0x85, 0x15, 0xB4, 0x8A, 0x27, 0x28, 0x92, 0x76, 0xDE,
139
+ 0xEF, 0xF8, 0xB2, 0xB7, 0xC9, 0x3D, 0x45, 0x94, 0x4B, 0x11, 0x0D, 0x65, 0xD5, 0x34, 0x8B, 0x91,
140
+ 0x0C, 0xFA, 0x87, 0xE9, 0x7C, 0x5B, 0xB1, 0x4D, 0xE5, 0xD4, 0xCB, 0x10, 0xA2, 0x17, 0x89, 0xBC,
141
+ 0xDB, 0xB0, 0xE2, 0x97, 0x88, 0x52, 0xF7, 0x48, 0xD3, 0x61, 0x2C, 0x3A, 0x2B, 0xD1, 0x8C, 0xFB,
142
+ 0xF1, 0xCD, 0xE4, 0x6A, 0xE7, 0xA9, 0xFD, 0xC4, 0x37, 0xC8, 0xD2, 0xF6, 0xDF, 0x58, 0x72, 0x4E
143
+ };
144
+
145
+ /*
146
+ * This is an optimization table for doing bitwise vector comparisons. The
147
+ * population count of x, POPC[x], is the number of 1's in the binary
148
+ * representation of x. The bitwise XOR(a, b) applied within this table,
149
+ * POPC[a ^ b], is the Hamming distance between a and b. For more
150
+ * information, see http://en.wikipedia.org/wiki/Hamming_weight.
151
+ */
152
+ static const int __POPC[] =
153
+ {
154
+ 0x00, 0x01, 0x01, 0x02, 0x01, 0x02, 0x02, 0x03, 0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04,
155
+ 0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04, 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
156
+ 0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04, 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
157
+ 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
158
+ 0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04, 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
159
+ 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
160
+ 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
161
+ 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07,
162
+ 0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04, 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
163
+ 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
164
+ 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
165
+ 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07,
166
+ 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
167
+ 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07,
168
+ 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07,
169
+ 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07, 0x05, 0x06, 0x06, 0x07, 0x06, 0x07, 0x07, 0x08
170
+ };
171
+
172
+ /*
173
+ * Used to convert integers to 1-byte hex strings
174
+ * Integers in between the range of 0 and 256.
175
+ */
176
+ static const char* __HEX_BYTE_DIGITS[] =
177
+ {
178
+ "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0A", "0B", "0C", "0D", "0E", "0F",
179
+ "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "1A", "1B", "1C", "1D", "1E", "1F",
180
+ "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "2A", "2B", "2C", "2D", "2E", "2F",
181
+ "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "3A", "3B", "3C", "3D", "3E", "3F",
182
+ "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "4A", "4B", "4C", "4D", "4E", "4F",
183
+ "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "5A", "5B", "5C", "5D", "5E", "5F",
184
+ "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "6A", "6B", "6C", "6D", "6E", "6F",
185
+ "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "7A", "7B", "7C", "7D", "7E", "7F",
186
+ "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "8A", "8B", "8C", "8D", "8E", "8F",
187
+ "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "9A", "9B", "9C", "9D", "9E", "9F",
188
+ "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "AA", "AB", "AC", "AD", "AE", "AF",
189
+ "B0", "B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8", "B9", "BA", "BB", "BC", "BD", "BE", "BF",
190
+ "C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CA", "CB", "CC", "CD", "CE", "CF",
191
+ "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "DA", "DB", "DC", "DD", "DE", "DF",
192
+ "E0", "E1", "E2", "E3", "E4", "E5", "E6", "E7", "E8", "E9", "EA", "EB", "EC", "ED", "EE", "EF",
193
+ "F0", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "FA", "FB", "FC", "FD", "FE", "FF"
194
+ };
195
+
196
+ static int __tran3(int a, int b, int c, int n)
197
+ {
198
+ return (((__TRAN53[(a + n) & 255] ^ __TRAN53[b] * (n + n + 1)) + __TRAN53[c ^ __TRAN53[n]]) & 255);
199
+ }
200
+
201
+ static int __hexchar_to_int(char digit)
202
+ {
203
+ return ((digit | 432) * 239217992 & 0xffffffff) >> 28;
204
+ }
205
+
206
+ static const char* __int_to_hexchar(int num)
207
+ {
208
+ return __HEX_BYTE_DIGITS[num];
209
+ }
210
+
211
+ static int __score(int num)
212
+ {
213
+ return __POPC[num];
214
+ }
215
+
216
+ #ifdef __cplusplus
217
+ } //!extern "C"
218
+ #endif
@@ -0,0 +1,58 @@
1
+ /*!
2
+ * The MIT License (MIT)
3
+ * =====================
4
+ *
5
+ * Copyright 2017 Sepehr Laal
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in
15
+ * all copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ */
25
+
26
+ #ifndef _NILSIMSA_LITE_H_
27
+ #define _NILSIMSA_LITE_H_
28
+
29
+ #ifdef __cplusplus
30
+ extern "C" {
31
+ #endif
32
+
33
+ /*!
34
+ * @fn nilsimsa_compute
35
+ * @brief computes the Nilsimsa hash of the given data.
36
+ * @arg data [IN] input data byte (char) array
37
+ * @arg size [IN] input data size
38
+ * @arg out [OUT] output hash string (must be allocated
39
+ * to hold exactly 64 characters + 1 null character. So
40
+ * total of 65 characters e.g. char hash[65])
41
+ */
42
+ void nilsimsa_compute(const char* data, int size, char* out);
43
+
44
+ /*!
45
+ * @fn nilsimsa_compare
46
+ * @brief compares two given Nilsimsa hashes
47
+ * @returns A score between -127 and 128 where -127 means
48
+ * completely uncorrelated data and 128 means same data.
49
+ * @arg lhs [IN] left hand side hash
50
+ * @arg rhs [IN] right hand side hash
51
+ */
52
+ int nilsimsa_compare(const char* lhs, const char* rhs);
53
+
54
+ #ifdef __cplusplus
55
+ } //!extern "C"
56
+ #endif
57
+
58
+ #endif //!_NILSIMSA_LITE_H_
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "mkmf"
4
+
5
+ # Makes all symbols private by default to avoid unintended conflict
6
+ # with other gems. To explicitly export symbols you can use RUBY_FUNC_EXPORTED
7
+ # selectively, or entirely remove this flag.
8
+ append_cflags("-fvisibility=hidden")
9
+
10
+ create_makefile("nilsimsa_lite/nilsimsa_lite")
@@ -0,0 +1,51 @@
1
+ #include "ruby.h"
2
+ #include "../nilsimsa-lite/nilsimsa.c"
3
+
4
+ /*
5
+ Computes the Nilsimsa hash of the given data. Returns a 64 character long string representing the
6
+ digest.
7
+
8
+ @param input [String]
9
+ @return [String] 64 hexadecimal characters string.
10
+ */
11
+ VALUE rb_nilsimsa_digest(VALUE self, VALUE input) {
12
+ if (rb_type(input) != T_STRING) rb_raise(rb_eArgError, "can only digest strings");
13
+
14
+ char *string = RSTRING_PTR(input);
15
+ char hash[65] = { 0 };
16
+
17
+ nilsimsa_compute(string, strlen(string), hash);
18
+
19
+ VALUE ruby_str = rb_str_new2(hash);
20
+ return ruby_str;
21
+ }
22
+
23
+ /*
24
+ Compares two given Nilsimsa hashes. Returns a score between -127 and 128 where -127 means
25
+ completely uncorrelated data and 128 means same data.
26
+
27
+ @param a [String] Nilsimsa hash of a string
28
+ @param b [String] Nilsimsa hash of another string
29
+ @return Integer Score between -127 and 128. -127 means uncorrelated data and 128 means same data.
30
+
31
+ Note that it doesn't check whether the passed hashes are valid: if you pass shorter or longer
32
+ strings, or of invalid format, you will get a score between -127 and 128, but it won't mean
33
+ anything.
34
+ */
35
+ VALUE rb_nilsimsa_compare(VALUE self, VALUE a, VALUE b) {
36
+ if (rb_type(a) != T_STRING || rb_type(b) != T_STRING) rb_raise(rb_eArgError, "can only compare digest strings");
37
+
38
+ char *string_a = RSTRING_PTR(a);
39
+ char *string_b = RSTRING_PTR(b);
40
+
41
+ VALUE ruby_int = INT2NUM(nilsimsa_compare(string_a, string_b));
42
+ return ruby_int;
43
+ }
44
+
45
+ RUBY_FUNC_EXPORTED void
46
+ Init_nilsimsa_lite(void)
47
+ {
48
+ VALUE rb_mNilsimsaLite = rb_define_module("NilsimsaLite");
49
+ rb_define_module_function(rb_mNilsimsaLite, "digest", rb_nilsimsa_digest, 1);
50
+ rb_define_module_function(rb_mNilsimsaLite, "compare", rb_nilsimsa_compare, 2);
51
+ }
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NilsimsaLite
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "nilsimsa_lite/version"
4
+ require_relative "nilsimsa_lite/nilsimsa_lite"
5
+
6
+ module NilsimsaLite
7
+ end
@@ -0,0 +1,4 @@
1
+ module NilsimsaLite
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nilsimsa_lite
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sergio Gil
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: Nilsimsa is a locality-sensitive hashing algorithm. Similar strings have
13
+ similar hashes. This library provides a way to generate this hashes, and compare
14
+ them
15
+ email:
16
+ - sgilperez@gmail.com
17
+ executables: []
18
+ extensions:
19
+ - ext/nilsimsa_lite/extconf.rb
20
+ extra_rdoc_files: []
21
+ files:
22
+ - CHANGELOG.md
23
+ - CODE_OF_CONDUCT.md
24
+ - LICENSE
25
+ - README.md
26
+ - Rakefile
27
+ - ext/nilsimsa-lite/LICENSE
28
+ - ext/nilsimsa-lite/README.md
29
+ - ext/nilsimsa-lite/nilsimsa.c
30
+ - ext/nilsimsa-lite/nilsimsa.h
31
+ - ext/nilsimsa_lite/extconf.rb
32
+ - ext/nilsimsa_lite/nilsimsa_lite.c
33
+ - lib/nilsimsa_lite.rb
34
+ - lib/nilsimsa_lite/version.rb
35
+ - sig/nilsimsa_lite.rbs
36
+ homepage: https://codeberg.org/sgilperez/nilsimsa_lite
37
+ licenses: []
38
+ metadata:
39
+ homepage_uri: https://codeberg.org/sgilperez/nilsimsa_lite
40
+ source_code_uri: https://codeberg.org/sgilperez/nilsimsa_lite
41
+ changelog_uri: https://codeberg.org/sgilperez/nilsimsa_lite/CHANGELOG.md
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 3.1.0
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubygems_version: 3.6.9
57
+ specification_version: 4
58
+ summary: Ruby wrapper for nilsimsa-lite C library
59
+ test_files: []