我不能一次计算和比较每个文件的哈希值

2024-04-20 07:15:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个hashcomparator.pypython脚本,它在脚本中打开一个文件夹,然后应该计算每个文件的哈希值,打开第二个文件夹,计算文件的哈希值,然后比较哈希值

import hashlib
import glob

#Open the input folder
path = "C:\\Users\\luis.henrique\\Documents\\filesin\\"
#scrolls through all .txt files
filesin = [f for f in glob.glob(path + "**/*.txt", recursive=True)]
#calculate hash of .txt files
hash_object = hashlib.sha256(b'filesin')
hex_dig = hash_object.hexdigest()

#Open the output folder
path = "C:\\Users\\luis.henrique\\Documents\\filesout\\"
#scrolls through all .txt files
filesout = [f for f in glob.glob(path + "**/*.txt", recursive=True)]
#calculate hash of .txt files
hash_object = hashlib.sha256(b'filesout')
hex_dig = hash_object.hexdigest()

if filesin == filesout: 
    print ("The MD5 Hashes of the files are the same.")
    print  (filesin)
    print(hex_dig)
    print  ("\n")
    print  (filesout)
    print(hex_dig)
else:
    print ("The MD5 Hashes are not the same.")
    print(hex_dig)

但我无法计算和比较每个文件的哈希值


Tags: 文件ofthepathtxtobjecthashfiles
1条回答
网友
1楼 · 发布于 2024-04-20 07:15:52

您正在用glob构建文件列表,但是您不会将这些文件发送到hashlib函数。而是传递字符串“filesin”。您必须为每个文件提供数据,注意不要超过缓冲区大小。这应该让你开始:

for file in filesin:
    sha256_hash = hashlib.sha256()
    with open(file,'rb') as f:
        # Read and update hash string value in blocks of 4K
        for byte_block in iter(lambda: f.read(4096),b""):
            sha256_hash.update(byte_block)
        print(sha256_hash.hexdigest())

相关问题 更多 >