def compute_checksum(filename, hashtype):
file = encode(filename)
if not exists(file):
return None
buf = fsbsize(filename)
if hashtype in ("adler32", "crc32"):
hf = getattr(zlib, hashtype)
last = 0
with open(file, "rb") as f:
for chunk in iter(lambda: f.read(buf), ''):
last = hf(chunk, last)
return "%x" % last
elif hashtype in hashlib.algorithms_available:
h = hashlib.new(hashtype)
with open(file, "rb") as f:
for chunk in iter(lambda: f.read(buf * h.block_size), ''):
h.update(chunk)
return h.hexdigest()
else:
return None
评论列表
文章目录