Python3 散列给出不同的结果

Python3 hashing gives different result

我尝试使用 Python3 代码计算加密文件 (file.gpg) 的 sha1。

我测试了两个函数

import hashlib
import gnupg

def sha1sum(filename):
    h  = hashlib.sha1()
    b  = bytearray(128*1024)
    mv = memoryview(b)
    with open(filename, 'rb', buffering=0) as f:
        for n in iter(lambda : f.readinto(mv), 0):
            h.update(mv[:n])
    return h.hexdigest()


def sha1_checksum(filename, block_size=65536):
    sha1 = hashlib.sha1()
    with open(filename, 'rb') as f:
        for block in iter(lambda: f.read(block_size), b''):
            sha1.update(block)
    return sha1.hexdigest()

original = open('file.bin', 'rb')

gpg = gnupg.GPG()
gpg.encoding = 'utf-8'
encrypt = gpg.encrypt_file(original, 
                           recipients=None, 
                           passphrase=password,
                           symmetric='AES256', 
                           output=file)

sum = sha1sum(file)
sum = sha1_checksum(file)

脚本第一次启动

697cee13eb4c91f41922472d8768fad076c72166
697cee13eb4c91f41922472d8768fad076c72166

脚本第二次启动

a95593f0d8ce274492862b58108a20700ecf9d2b
a95593f0d8ce274492862b58108a20700ecf9d2b

sha1sum() 或 sha1_checksum() 错了吗?

或者文件加密给出不同的 file.gpg ?

这不是 Python 的问题,甚至不是 gpg 的问题。

哈希变化的原因是gpg非对称加密是非确定性,或所谓的概率

引自wiki page Probabilistic encryption

Probabilistic encryption is the use of randomness in an encryption algorithm, so that when encrypting the same message several times it will, in general, yield different ciphertexts. The term "probabilistic encryption" is typically used in reference to public key encryption algorithms, however various symmetric key encryption algorithms achieve a similar property (e.g., block ciphers when used in a chaining mode such as CBC).