Python摘要算法

来源:互联网 发布:做微商好还是做淘宝好 编辑:程序博客网 时间:2024/06/08 15:25

Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等

直接上代码:

#coding=utf-8import hashlibfile = open('./linux.zip')data = file.read()         #获取文件内容print type(data)#print data#打印出的字符串是乱码md5Hash = hashlib.md5()     #求md5的方法sha256Hash = hashlib.sha256()#求sha256的方法print type(md5Hash)print type(sha256Hash)md5Hash.update(data)   #把字符串放进去,常用于小文件sha256Hash.update(data)fileMD5 = md5Hash.hexdigest()   #显示出摘要fileSHA256 =sha256Hash.hexdigest()print 'file MD5:', fileMD5print 'file SHA256', fileSHA256

运行结果,如图:


其中hashlib的python库不需要额外安装,只需要import,应该Python自带的库。


原创粉丝点击