如何在hashlib.file_digest中使用xxhash?

问题描述 投票:0回答:1

我想快速计算磁盘上大文件的哈希值,以便将它们相互比较。

我正在使用以下内容:

 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()

有版本可以吗?

python hash sha hashlib
1个回答
0
投票

尝试这样:

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()
© www.soinside.com 2019 - 2024. All rights reserved.