python 中的 md5 输出与命令行不同(即使在二进制模式下)
我正在编写一个脚本,需要检查 OSX 和 Windows 上文件的 md5 总和,作为健全性检查,我将结果与命令行 md5
工具的结果进行了比较,但我得到了不同的结果结果。 代码
def MD5File(self, f, block_size=2**20):
md5 = hashlib.md5()
while True:
data = f.read(block_size)
if not data:
break
md5.update(data)
return md5.hexdigest()
with open(path, 'rb') as f:
print MD5File(path)
这是我做了以二进制模式打开文件的明显事情的 ,但它仍然给出了不同的结果。我尝试了不同的缓冲数据的方法,包括一次性读取所有数据,并且 python 脚本始终返回相同的内容,但这与 md5 命令不同。
那么,是否还有其他明显的我做错的事情,或者运行 md5 filename
的情况是否实际上没有达到您的预期?当我直接读取文件的二进制文件时,不应该有任何换行问题。如果我运行 cat filename | md5 然后我再次得到不同的结果。
I'm writing a script that needs to check the md5 sum of a file on OSX and Windows, and as a sanity check I compared the results with that of the command line md5
tool, but I get different results. Here's the code
def MD5File(self, f, block_size=2**20):
md5 = hashlib.md5()
while True:
data = f.read(block_size)
if not data:
break
md5.update(data)
return md5.hexdigest()
with open(path, 'rb') as f:
print MD5File(path)
I did the obvious thing of opening the file in binary mode, but it still gives different results. I've tried different ways of buffering the data, including just reading it all in one go, and the python script consistently returns the same thing, but that's different to the md5 command.
So is there something else really obvious I'm doing wrong, or is it a case that running md5 filename
doesn't actually do what you expect? As I'm reading the binary of the file directly there shouldn't be any newline issues. If I run cat filename | md5
then I get a different result again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下内容对我来说正确:
请尝试我的代码,看看它是否适合您。如果没有,您能否上传您的 python 脚本的 gist 和示例文件供我尝试?
The following works correctly for me:
Please try my code and see if it works for you. If it doesn't, will you upload a gist of your python script and a sample file for me to try?
糟糕,这是用户错误的情况。我重写了 shell 中的
md5
命令,只返回字符串而不是文件的哈希值。Oops, this was a case of user error. I had overriden the
md5
command in the shell to just return the hash of a string rather than a file.