python hashlib模块

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_size
print 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. Ut
enim ad minimveniam, quis nostrud exercitation ullamco laboris nisi
ut aliquip exea commodo consequat. Duis aute irure dolor in
reprehenderitin voluptate velit esse cillum dolore eu fugiat nulla
pariatur.Excepteur sint occaecat cupidatat non proident, sunt in
culpa 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
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值