在Python 3中使用hashlib计算文件的md5摘要 [英] Using hashlib to compute md5 digest of a file in Python 3

查看:449
本文介绍了在Python 3中使用hashlib计算文件的md5摘要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于python 2.7,以下代码计算文件内容的mD5十六进制摘要.

With python 2.7 the following code computes the mD5 hexdigest of the content of a file.

(恩,我并不是这么想的,我只是这么想).

( well, not really as answers have shown, I just thought so).

import hashlib

def md5sum(filename):
    f = open(filename, mode='rb')
    d = hashlib.md5()
    for buf in f.read(128):
        d.update(buf)
    return d.hexdigest()

现在,如果我使用python3运行该代码,则会引发TypeError异常:

Now if I run that code using python3 it raise a TypeError Exception:

    d.update(buf)
TypeError: object supporting the buffer API required

我发现我可以使python2和python3都将其更改为以下代码来运行该代码:

I figured out that I could make that code run with both python2 and python3 changing it to:

def md5sum(filename):
    f = open(filename, mode='r')
    d = hashlib.md5()
    for buf in f.read(128):
        d.update(buf.encode())
    return d.hexdigest()

现在,我仍然想知道为什么原始代码停止工作.似乎在使用二进制模式修饰符打开文件时,它返回整数,而不是编码为字节的字符串(我之所以这么说是因为type(buf)返回int).这种行为在某处得到了解释吗?

Now I still wonder why the original code stopped working. It seems that when opening a file using the binary mode modifier it returns integers instead of strings encoded as bytes (I say that because type(buf) returns int). Is this behavior explained somewhere ?

推荐答案

我认为您希望for循环连续调用f.read(128).可以使用 iter() functools.partial():

I think you wanted the for-loop to make successive calls to f.read(128). That can be done using iter() and functools.partial():

import hashlib
from functools import partial

def md5sum(filename):
    with open(filename, mode='rb') as f:
        d = hashlib.md5()
        for buf in iter(partial(f.read, 128), b''):
            d.update(buf)
    return d.hexdigest()

print(md5sum('utils.py'))

这篇关于在Python 3中使用hashlib计算文件的md5摘要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆