前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Get MD5 Hash of Big Files

Get MD5 Hash of Big Files

作者头像
技术小黑屋
发布2018-09-04 16:25:24
6140
发布2018-09-04 16:25:24
举报

Recently I have been dealing with files and I need to get md5 hash of all kinds of files;Some are small and some are big. For the small files I use this method to get md5 hash value.

1 2 3 4 5 6 7

def getFileMd5(filename): file = open(filename, 'rb') m = md5() m.update(file.read()) file.close() result = m.hexdigest() return result

However for calculating md5 hash value of big files,the above method will be very Less Efficient.For Big files I use the following method(It’s acquired from stackoverflow)

1 2 3 4 5 6 7 8 9 10

def getBigFileMd5(filename, block_size=2**20): f = open(filename, 'rb') m = md5() while True: data = f.read(block_size) if not data: break m.update(data) f.close() return m.hexdigest()

And I did a test.The cost of Getting md5 hash of a Big file(size:10.7 GiB; 11,455,512,109 bytes) is 213.447s.And I think it’s OK.

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档