Lines 22-27
Link Here
|
22 |
#dict of all available hash functions |
22 |
#dict of all available hash functions |
23 |
hashfunc_map = {} |
23 |
hashfunc_map = {} |
24 |
|
24 |
|
|
|
25 |
# This is only available in Python 2.5 |
26 |
try: |
27 |
import hashlib |
28 |
except ImportError: |
29 |
hashlib = None |
30 |
|
25 |
# We _try_ to load this module. If it fails we do the slightly slower fallback. |
31 |
# We _try_ to load this module. If it fails we do the slightly slower fallback. |
26 |
try: |
32 |
try: |
27 |
import fchksum |
33 |
import fchksum |
Lines 32-42
Link Here
|
32 |
except ImportError: |
38 |
except ImportError: |
33 |
import md5 |
39 |
import md5 |
34 |
def md5hash(filename): |
40 |
def md5hash(filename): |
35 |
return pyhash(filename, md5) |
41 |
return pyhash(filename, md5.new) |
36 |
hashfunc_map["MD5"] = md5hash |
42 |
hashfunc_map["MD5"] = md5hash |
37 |
|
43 |
|
38 |
def sha1hash(filename): |
44 |
def sha1hash(filename): |
39 |
return pyhash(filename, sha) |
45 |
return pyhash(filename, sha.new) |
40 |
hashfunc_map["SHA1"] = sha1hash |
46 |
hashfunc_map["SHA1"] = sha1hash |
41 |
|
47 |
|
42 |
# Keep pycrypto optional for now, there are no internal fallbacks for these |
48 |
# Keep pycrypto optional for now, there are no internal fallbacks for these |
Lines 44-62
Link Here
|
44 |
import Crypto.Hash.SHA256 |
50 |
import Crypto.Hash.SHA256 |
45 |
|
51 |
|
46 |
def sha256hash(filename): |
52 |
def sha256hash(filename): |
47 |
return pyhash(filename, Crypto.Hash.SHA256) |
53 |
return pyhash(filename, Crypto.Hash.SHA256.new) |
48 |
hashfunc_map["SHA256"] = sha256hash |
54 |
hashfunc_map["SHA256"] = sha256hash |
49 |
except ImportError: |
55 |
except ImportError: |
50 |
pass |
56 |
if hashlib != None: |
|
|
57 |
def sha256hash(filename): |
58 |
return pyhash(filename, hashlib.sha256) |
59 |
hashfunc_map["SHA256"] = sha256hash |
51 |
|
60 |
|
52 |
try: |
61 |
try: |
53 |
import Crypto.Hash.RIPEMD |
62 |
import Crypto.Hash.RIPEMD |
54 |
|
63 |
|
55 |
def rmd160hash(filename): |
64 |
def rmd160hash(filename): |
56 |
return pyhash(filename, Crypto.Hash.RIPEMD) |
65 |
return pyhash(filename, Crypto.Hash.RIPEMD.new) |
57 |
hashfunc_map["RMD160"] = rmd160hash |
66 |
hashfunc_map["RMD160"] = rmd160hash |
58 |
except ImportError: |
67 |
except ImportError: |
59 |
pass |
68 |
if hashlib != None: |
|
|
69 |
# Even if hashlib is available, it's not guaranteed |
70 |
# to support ripemd160, so check first |
71 |
try: |
72 |
hashlib.new('ripemd160') |
73 |
except ValueError: |
74 |
pass |
75 |
else: |
76 |
def rmd160hash(filename): |
77 |
return pyhash(filename, lambda:hashlib.new('ripemd160')) |
78 |
hashfunc_map["RMD160"] = rmd160hash |
79 |
|
60 |
|
80 |
|
61 |
def getsize(filename): |
81 |
def getsize(filename): |
62 |
size = os.stat(filename).st_size |
82 |
size = os.stat(filename).st_size |
Lines 111-122
Link Here
|
111 |
break |
131 |
break |
112 |
return file_is_ok,reason |
132 |
return file_is_ok,reason |
113 |
|
133 |
|
114 |
def pyhash(filename, hashobject): |
134 |
def pyhash(filename, hashfactory): |
115 |
f = open(filename, 'rb') |
135 |
f = open(filename, 'rb') |
116 |
blocksize = HASHING_BLOCKSIZE |
136 |
blocksize = HASHING_BLOCKSIZE |
117 |
data = f.read(blocksize) |
137 |
data = f.read(blocksize) |
118 |
size = 0L |
138 |
size = 0L |
119 |
sum = hashobject.new() |
139 |
sum = hashfactory() |
120 |
while data: |
140 |
while data: |
121 |
sum.update(data) |
141 |
sum.update(data) |
122 |
size = size + len(data) |
142 |
size = size + len(data) |