This repository was archived by the owner on Jan 24, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmain.js
More file actions
85 lines (76 loc) · 2.22 KB
/
main.js
File metadata and controls
85 lines (76 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
var crypto = require('crypto');
var crc32 = require('buffer-crc32');
function Simhash(algorithm) {
this.algorithm = algorithm;
// crc32 is the default algorithm
if (!algorithm) {
this.algorithm = 'crc32';
}
var tmp = this._hashBuffer(new Buffer(''));
if (typeof(tmp) === 'number') {
this.hashLength = 32;
} else {
this.hashLength = tmp.length * 8;
}
this._hashed = [];
}
Simhash.prototype._accumulateNumber = function(input) {
}
Simhash.prototype._accumulate = function(input) {
var self = this;
var accumulated = new Array(this.hashLength);
var i = this.hashLength;
while(--i >= 0) {
accumulated[i] = 0;
}
if (typeof(input[0]) === 'number'){
i = input.length;
while(--i >= 0) {
var data = input[i];
var j = this.hashLength;
while(--j >= 0) {
accumulated[j] += (data & (1 << j)) ? 1 : -1;
}
}
} else {
i = input.length;
while(--i >= 0) {
var buf = input[i];
var j = 0;
var l = buf.length - 1;
while(j < self.hashLength) {
for(var k = 0; k < 8; ++k) {
accumulated[j] += (buf[l] & (1 << k)) ? 1 : -1;
j++;
}
l--;
}
};
}
return accumulated;
}
Simhash.prototype._hashBuffer = function(buffer) {
if (this.algorithm === 'crc32') {
return crc32.signed(buffer);
}
var hash = crypto.createHash(this.algorithm);
hash.update(buffer.toString('binary'), 'binary');
return new Buffer(hash.digest('binary'), 'binary');
}
Simhash.prototype.hash = function(tokens) {
this._hashed.length = tokens.length;
// var hashed = new Array(tokens.length);
for(var i = 0; i < tokens.length; ++i) {
this._hashed[i] = this._hashBuffer(tokens[i]);
}
var result = this._accumulate(this._hashed);
for(var i = 0; i < result.length; ++i) {
result[i] = result[i] > 0 ? 1 : 0;
}
return result;
}
module.exports = function (algorithm) {
var simhash = new Simhash(algorithm);
return simhash.hash.bind(simhash);
}
module.exports.Simhash = Simhash;