forked from iambus/xunlei-lixian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lixian_hash.py
executable file
·90 lines (77 loc) · 2.13 KB
/
lixian_hash.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
#!/usr/bin/env python
import hashlib
import lixian_hash_ed2k
import lixian_hash_bt
import os
def lib_hash_file(h, path):
with open(path, 'rb') as stream:
while True:
bytes = stream.read(1024*1024)
if not bytes:
break
h.update(bytes)
return h.hexdigest()
def sha1_hash_file(path):
return lib_hash_file(hashlib.sha1(), path)
def verify_sha1(path, sha1):
return sha1_hash_file(path).lower() == sha1.lower()
def md5_hash_file(path):
return lib_hash_file(hashlib.md5(), path)
def verify_md5(path, md5):
return md5_hash_file(path).lower() == md5.lower()
def md4_hash_file(path):
return lib_hash_file(hashlib.new('md4'), path)
def verify_md4(path, md4):
return md4_hash_file(path).lower() == md4.lower()
def dcid_hash_file(path):
h = hashlib.sha1()
size = os.path.getsize(path)
with open(path, 'rb') as stream:
if size < 0xF000:
h.update(stream.read())
else:
h.update(stream.read(0x5000))
stream.seek(size/3)
h.update(stream.read(0x5000))
stream.seek(size-0x5000)
h.update(stream.read(0x5000))
return h.hexdigest()
def verify_dcid(path, dcid):
return dcid_hash_file(path).lower() == dcid.lower()
def main(args):
option = args.pop(0)
def verify_bt(f, t):
from lixian_progress import SimpleProgressBar
bar = SimpleProgressBar()
result = lixian_hash_bt.verify_bt_file(t, f, progress_callback=bar.update)
bar.done()
return result
if option.startswith('--verify'):
hash_fun = {'--verify-sha1':verify_sha1,
'--verify-md5':verify_md5,
'--verify-md4':verify_md4,
'--verify-dcid':verify_dcid,
'--verify-ed2k':lixian_hash_ed2k.verify_ed2k_link,
'--verify-bt': verify_bt,
}[option]
assert len(args) == 2
hash, path = args
if hash_fun(path, hash):
print 'looks good...'
else:
print 'failed...'
else:
hash_fun = {'--sha1':sha1_hash_file,
'--md5':md5_hash_file,
'--md4':md4_hash_file,
'--dcid':dcid_hash_file,
'--ed2k':lixian_hash_ed2k.generate_ed2k_link,
'--info-hash':lixian_hash_bt.info_hash,
}[option]
for f in args:
h = hash_fun(f)
print '%s *%s' % (h, f)
if __name__ == '__main__':
import sys
args = sys.argv[1:]
main(args)