python hashlib模块
来源:互联网 发布:linux 创建文件 编辑:程序博客网 时间:2024/05/18 00:24
hashlib用来替换md5和sha模块,并使他们的API一致。它由OpenSSL支持,支持如下算法:
md5,sha1, sha224, sha256, sha384, sha512
示例一:
import hashlib m = hashlib.md5() #创建hash对象,md5:(message-Digest Algorithm 5)消息摘要算法,得出一个128位的密文 print m #<md5 HASH object @ 02B93250> m.update('python') #更新哈希对象,以字符串为参数 print m.digest() #返回摘要,作为二进制数据字符串值 print m.hexdigest() #返回摘要,作为十六进制数据字符串值 23eeeb4347bdd26bfc6b7ee9a3b755dd print m.digest_size #16,结果hash的大小,产生的散列的字节大小 print m.block_size #64,hash内部块的大小,The internal block size of the hash algorithm in bytes print hashlib.md5('python').hexdigest()#简略写法 23eeeb4347bdd26bfc6b7ee9a3b755dd m1=m.copy()#复制 print m #<md5 HASH object @ 02B93250>print m1 #<md5 HASH object @ 02B93200>print m.hexdigest()==m1.hexdigest() #True示例二:使用new()创建指定加密模式的hash对象
import hashlib h = hashlib.new('md5') print h #<md5 HASH object @ 029F3200> h.update('python') print h.hexdigest() #23eeeb4347bdd26bfc6b7ee9a3b755dd print h.block_size,h.digest_sizeprint hashlib.new('md5','python').hexdigest() #简略写法 23eeeb4347bdd26bfc6b7ee9a3b755dd #等效 h1 = hashlib.md5() h1.update('python') print h1.hexdigest() #23eeeb4347bdd26bfc6b7ee9a3b755dd print h1.block_size,h1.digest_size print hashlib.md5('python').hexdigest() #简略写法 23eeeb4347bdd26bfc6b7ee9a3b755dd #列出所有加密算法 print hashlib.algorithms#('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512')示例三:更新哈希对象以字符串为参数,如果同一个hash对象重复调用该方法,则 m.update(a);m.update(b) is equivalent to m.update(a+b)
data.py文件:
s = '''Loremipsum dolor sit amet, consectetur adipisicing elit,sed doeiusmod tempor incididunt ut labore et dolore magna aliqua. Utenim ad minimveniam, quis nostrud exercitation ullamco laboris nisiut aliquip exea commodo consequat. Duis aute irure dolor inreprehenderitin voluptate velit esse cillum dolore eu fugiat nullapariatur.Excepteur sint occaecat cupidatat non proident, sunt inculpa quiofficia deserunt mollit anim id est laborum.'''
当前要执行的test.py文件:
import hashlib from data import s h =hashlib.md5() h.update(s) all_at_once=h.hexdigest() #增量更新,文件太大的时候,可以分多次读入 def chunkmd5(size,text): '''''Return parts of the text in size-based increments.''' start=0 while start<len(text): chunk=text[start:start+size] yield chunk start+=size h1=hashlib.md5() for chunk in chunkmd5(64,s): h1.update(chunk) all_at_many_time= h1.hexdigest() print 'All at once :',all_at_once print 'All at many time:',all_at_many_time print 'the two is same?:', (all_at_once ==all_at_many_time)结果:
All at once : fda376c90e12c5f0ec0c8dc86b056aa3
All at many time: fda376c90e12c5f0ec0c8dc86b056aa3
the two is same?: True
1 0
- Python hashlib模块
- python模块之hashlib
- Python hashlib模块
- python模块之hashlib
- python hashlib模块
- python 模块学习 hashlib
- Python hashlib模块
- Python hashlib、hmac模块
- python 模块学习 hashlib
- python 模块学习 hashlib
- python hashlib 模块
- python hashlib模块
- Python的hashlib模块
- python 模块学习 hashlib
- Python中hashlib模块
- python中hashlib模块
- Python的hashlib模块
- Python模块学习之hashlib
- android切换输入法工具类
- 前端数组去重
- php微信支付(仅Jsapi支付)详细步骤.----仅适合第一次做微信开发的程序员
- 商品以省市方式显示百分比数量
- 用pathinfo()+$_FILES获取文件上传时的一些信息
- python hashlib模块
- 如何使用View自定义属性画圆
- 杭电1241 Oil Deposits
- HDU 4280 Island Transport 网络流sap
- A*算法
- ios有关UITableView单选多选框的实现
- 大数乘法
- Android编码规范
- java中关于范型使用的小技巧