我想快速计算磁盘上大文件的哈希值,以便将它们相互比较。
我正在使用以下内容:
import hashlib
def sha256sum(filename):
with open(filename, 'rb', buffering=0) as f:
return hashlib.file_digest(f, 'sha256').hexdigest()
但我想使用
xxhash
,因为我听说它更快。这不起作用:
import hashlib
def xxhashsum(filename):
with open(filename, 'rb', buffering=0) as f:
return hashlib.file_digest(f, 'xxhash').hexdigest()
有版本可以吗?
尝试这样:
import xxhash
import hashlib
def xxhashsum(filename, algo="xxh128"):
if algo not in xxhash.alorithms_available:
raise NotImplementedError
digest = getattr(xxhash, algo)
with open(filename, 'rb', buffering=0) as f:
return hashlib.file_digest(f, digest).hexdigest()