我设法想出了一个计算文件校验和的代码。代码如下:
from Crypto.Hash import *
def get_file_checksum(filename):
h = SHA384.new()
chunk_size = 8192
with open(filename, 'rb') as f:
while True:
chunk = f.read(chunk_size)
if len(chunk) == 0:
break
h.update(chunk)
return h.hexdigest()
现在我想到了另一个函数来计算任何给定字符串的校验和。这是密码
^{pr2}$我面临的问题是字符串校验和的输出与文件内容的输出不匹配。在
对于系统中的特定文件,我得到这些值
get_file_checksum(filename)
将得到:
d57eec87df9c5ec33baab34be4ac7695a59d80779d10e8920640fac62f4bfa2b0da0cef0b0d7d6d1e16cae5ca560cd95
get_string_checksum(string)
将得到:
18044418f40f12e9601d0f15a7da46c114779ac337fd8fe513a122e2aa95a9b5e05d6b00d46d0682c0054c5f2b3355c0
我使用参数的字符串与文件的内容相同。在
我正在使用此网站验证校验和:https://defuse.ca/checksums.htm#checksums
get_string_checksum(string)
有什么问题吗?在
去掉文件读取函数中的空白,如:
相关问题 更多 >
编程相关推荐