python里如何计算大文件的md5

在python3中,有了一个hashlib,可以用来计算md5,这里先给出一个简单的例子:

import hashlib

sstr="i love hanyu"
print(hashlib.md5(sstr).hexdigest())

很遗憾的,出错了,错误信息是:

C:\Python35\python.exe C:/pylearn/bottlelearn/3.py
Traceback (most recent call last):
  File "C:/pylearn/bottlelearn/3.py", line 4, in <module>
    print(hashlib.md5(sstr).hexdigest())
TypeError: Unicode-objects must be encoded before hashing

Process finished with exit code 1

这里主要是考虑到传入的编码不同,会导致md5出问题,所以,要求传入前进行统一的编码,修改如下:

 import hashlib
 hashlib.sha256(str(random.getrandbits(256)).encode('utf-8')).hexdigest()
import hashlib

with open(hash_file) as file:
    control_hash = file.readline().rstrip("\n")

wordlistfile = open(wordlist, "rb")
# ...
for line in wordlistfile:
    if hashlib.md5(line.rstrip(b'\n\r')).hexdigest() == control_hash:

 

下面,来看看如何计算大文件的md5,如果只是简单的把文件都入到内存中,大文件会导致出现大问题,编码如下:

import hashlib

def hash_bytestr_iter(bytesiter, hasher, ashexstr=False):
    for block in bytesiter:
        hasher.update(block)
    return (hasher.hexdigest() if ashexstr else hasher.digest())

def file_as_blockiter(afile, blocksize=65536):
    with afile:
        block = afile.read(blocksize)
        while len(block) > 0:
            yield block
            block = afile.read(blocksize)


[(fname, hash_bytestr_iter(file_as_blockiter(open(fname, 'rb')), hashlib.md5()))
    for fname in fnamelst]

 

转载于:https://www.cnblogs.com/aomi/p/7047214.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值