forked from telehash/telehash-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdhash.js
More file actions
132 lines (118 loc) · 3.28 KB
/
Copy pathdhash.js
File metadata and controls
132 lines (118 loc) · 3.28 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
var crypto = require('crypto');
// just return true/false if it's at least the format of a sha1
exports.isSHA1 = function(str)
{
if(typeof str !== "string") return false;
if(str.length !== 40) return false;
if(str.replace(/[a-f0-9]+/i, "").length !== 0) return false;
return true;
}
// convenience, sha1 hash of arg, or random if none
exports.quick = function(str)
{
if(!str) return crypto.randomBytes(20).toString("hex");
return crypto.createHash("sha1").update(str).digest('hex');
}
/**
* Hash objects represent the sha1 of string content,
* with methods useful to DHT calculations.
* @constructor
*/
function Hash(value, hex) {
if(value == undefined) value = "";
if(hex) this.digest = hex2buf(hex);
// if failed still, just treat as a string
if (!this.digest) {
var hashAlgorithm = crypto.createHash("SHA1");
hashAlgorithm.update(value);
this.digest = new Buffer(hashAlgorithm.digest("base64"), "base64");
}
this.nibbles = [];
for (var i = 0; i < this.digest.length; i++) {
this.nibbles[this.nibbles.length] = this.digest[i] >> 4;
this.nibbles[this.nibbles.length] = this.digest[i] & 0xf;
}
}
function hex2buf(str)
{
var buf = new Buffer(20);
for (var i = 0; i < str.length / 2; i ++) {
var byte = parseInt(str.substr(i * 2, 2), 16);
if (isNaN(byte)) return null;
buf[i] = byte;
}
return buf;
}
/**
* Format a byte as a two digit hex string.
*/
function byte2hex(d) {
return d < 16 ? "0" + d.toString(16) : d.toString(16);
}
exports.Hash = Hash
/**
* Get the string hash as geometrically "far" as possible from this one.
* That would be the logical inverse, every bit flipped.
*/
Hash.prototype.far = function() {
var result = [];
for (var i = 0; i < this.digest.length; i++) {
result[i] = byte2hex(this.digest[i] ^ 0xff);
}
return result.join("");
}
/**
* Logical bitwise 'or' this hash with another.
*/
Hash.prototype.or = function(h) {
if (typeof h == 'string') { h = new Hash(h); }
var result = new Hash();
result.digest = new Buffer(this.digest.length);
for (var i = 0; i < this.digest.length; i++) {
result.digest[i] = this.digest[i] ^ h.digest[i];
}
return result;
}
/**
* Comparator for hash objects.
*/
Hash.prototype.cmp = function(h) {
for (var i = 0; i < this.digest.length; i++) {
var d = this.digest[i] - h.digest[i];
if (d != 0) {
return d;
}
}
return 0;
}
/**
* XOR distance between two sha1 hex hashes, 159 is furthest bit, 0 is closest bit, -1 is same hash
*/
Hash.prototype.distanceTo = function(h) {
var sbtab = [-1,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3];
var ret = 156;
for (var i = 0; i < this.nibbles.length; i++) {
var diff = this.nibbles[i] ^ h.nibbles[i];
if (diff) {
return ret + sbtab[diff];
}
ret -= 4;
}
return -1; // samehash ?!
}
/**
* Represent the hash as a hexadecimal string.
*/
Hash.prototype.toString = function() {
var result = [];
for (var i = this.digest.length - 1; i >= 0; i--) {
result[i] = byte2hex(this.digest[i]);
}
return result.join("");
}
/**
* Test if two hashes are equal.
*/
Hash.prototype.equals = function(h) {
return this.toString() == h.toString();
}