-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhash_util.py
executable file
·135 lines (95 loc) · 3.24 KB
/
hash_util.py
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
### ripped add credits
"""
'stolen' with much thanks to:
Licence: GNU Lesser General Public License (LGPL) version 2.1 (http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html)
Author: Benjamin Evans
Email: benny.j.evans92_at_gmail.com (replace at with @)
https://github.com/BennyEvans/PyChordDHT
"""
import hashlib
import random
class Key():
def __init__(self, key):
self.key = key
def __eq__(self, other):
return hash_equal(self, other)
def __str__(self):
return str(self.key)
def __hash__(self):
return int(self.key,16)
#key size
KEY_SIZE = 160
#max chord index
MAX_INDEX = (0x01 << 160) - 1
#returns true if h1>h2
def hash_greater_than(h1, h2):
if int(h1.key, 16) > int(h2.key, 16):
return True
return False
#returns true if h1<h2
def hash_less_than(h1, h2):
if int(h1.key, 16) < int(h2.key, 16):
return True
return False
#returns true if h1==h2
def hash_equal(h1, h2):
if int(h1.key, 16) == int(h2.key, 16):
return True
return False
#returns True if h1 is between s1 and s2
def hash_between(h1, s1, s2):
#if s1 == s2 then h1 must be between them assuming a full loop
if(hash_equal(s1, s2)):
return True
#if h1 == s1 || h1 == s2 then return False
if(hash_equal(h1, s1) or hash_equal(h1, s2)):
return False
#Check if s2 < s1 - if so assume a loop
if hash_less_than(s2, s1):
#assume a loop around the circle in which case h1 must be h1 > s1 || h1 < s2
if hash_greater_than(h1, s1) or hash_less_than(h1, s2):
return True
else:
#normal s1 < h1 < s2
if hash_greater_than(h1, s1) and hash_less_than(h1, s2):
return True
return False
def hash_between_right_inclusive(h1, s1, s2):
#if s1 == s2 then h1 must be between them assuming a full loop
if(hash_equal(s1, s2)):
return True
#if h1 == s1 || h1 == s2 then return False
if(hash_equal(h1, s1)):
return False
if(hash_equal(h1, s2)):
return True
#Check if s2 < s1 - if so assume a loop
if hash_less_than(s2, s1):
#assume a loop around the circle in which case h1 must be h1 > s1 || h1 < s2
if hash_greater_than(h1, s1) or hash_less_than(h1, s2):
return True
else:
#normal s1 < h1 < s2
if hash_greater_than(h1, s1) and hash_less_than(h1, s2):
return True
return False
def add_keys(k1, k2):
x = (int(k1.key, 16) + int(k2.key, 16)) % MAX_INDEX
return Key(hex(x).replace("L", ""))
def subtract_keys(k1, k2):
x = (int(k1.key, 16) - int(k2.key, 16))
if x < 0:
x = MAX_INDEX + x
return Key(hex(x).replace("L", ""))
def hash_str(strToHash):
m = hashlib.sha1()
m.update(strToHash)
return Key("0x" + m.hexdigest())
def generate_key_with_index(index):
return Key(hex(0x01 << index).replace("L", ""))
def generate_random_key():
return Key(hex(random.getrandbits(160)).replace("L", ""))
def generate_lookup_key_with_index(thisIndex, indexOfKey):
return add_keys(thisIndex, generate_key_with_index(indexOfKey))
def generate_reverse_lookup_key_with_index(thisIndex, indexOfKey):
return subtract_keys(thisIndex, generate_key_with_index(indexOfKey))