-
Notifications
You must be signed in to change notification settings - Fork 6
/
hash40.py
44 lines (36 loc) · 1.31 KB
/
hash40.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
import zlib
class HashLabel:
def __init__(self, hash40, label):
self.hash40 = hash40
self.label = label
HashLabels = []
file = open('ParamLabels.csv','r')
for s in file:
r = s.split(',')
HashLabels.append(HashLabel(r[0],r[1].strip()))
class Hash40:
def __init__(self, hash40, tag = None):
self.hash40 = hash40
self.length = int(hash40, 16) >> 32
self.hash = hex(int(hash40, 16) & 0xffffffff)
self.tag = tag
if(len(self.hash40.replace('0x', '')) < 10):
self.hash40 = '0x' + ('0' * (10 - len(self.hash40.replace('0x', '')))) + self.hash40.replace('0x', '')
def getLabel(self):
find = next((x for x in HashLabels if x.hash40 == self.hash40), None)
if find:
if find.label != '':
return 'hash40("{0}")'.format(find.label)
return self.hash40
@staticmethod
def Create(hash, length):
return Hash40(hex(length) + hash[2:])
@staticmethod
def CreateFromString(string):
return Hash40(hex(len(string)) + Hash40.doCRC(string).replace('0x',''))
@staticmethod
def doCRC(string):
h = hex(zlib.crc32(bytearray(string, 'utf-8')))
if len(h.replace("0x","")) < 8:
h = "0x" + ('0' * (8 - len(h.replace("0x","")))) + h.replace("0x","")
return h