对于类赋值,我应该获取文件的内容,计算MD5散列并将其存储在单独的文件中。然后我应该能够通过比较MD5散列来检查完整性。我对Python和JSON还比较陌生,所以我想我应该尝试用这个任务来解决这些问题,而不是使用我已经知道的东西。在
不管怎样,我的程序从一个文件中读取,创建一个散列,并将该散列存储到一个JSON文件中。问题出在我的诚信检查上。当我返回文件的计算哈希的结果时,它与JSON文件中记录的结果不同,即使文件没有进行任何更改。下面是正在发生的事情的一个例子,我也粘贴了我的代码。提前谢谢你的帮助。在
例如:这些是我的JSON文件的内容
内容:b'我制作了一个文件来测试md5\n'
文摘:1e8f4e6598be2ea2516102de54e7e48e
当我尝试检查同一个文件的完整性时,返回的内容如下(未对其进行任何更改):
内容:b'我制作了一个文件来测试md5\n'
摘要:ef8b7bf2986f59f8a51aae6b496e8954import hashlib
import json
import os
import fnmatch
from codecs import open
#opens the file, reads/encodes it, and returns the contents (c)
def read_the_file(f_location):
with open(f_location, 'r', encoding="utf-8") as f:
c = f.read()
f.close()
return c
def scan_hash_json(directory_content):
for f in directory_content:
location = argument + "/" + f
content = read_the_file(location)
comp_hash = create_hash(content)
json_obj = {"Directory": argument, "Contents": {"filename": str(f),
"original string": str(content), "md5": str(comp_hash)}}
location = location.replace