博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python hashlib模块
阅读量:4170 次
发布时间:2019-05-26

本文共 2238 字,大约阅读时间需要 7 分钟。

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             #
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 #
print m1 #
print m.hexdigest()==m1.hexdigest() #True

示例二:使用new()创建指定加密模式的hash对象

import hashlib  h = hashlib.new('md5')  print h     #
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
结果:

All at once     : fda376c90e12c5f0ec0c8dc86b056aa3

All at many time: fda376c90e12c5f0ec0c8dc86b056aa3
the two is same?: True

(完)

转载地址:http://vnyai.baihongyu.com/

你可能感兴趣的文章
Congratulations! Oracle DBA 10g Certified Master Practicum Results
查看>>
Excel简单五子棋
查看>>
Java之synchronized小例
查看>>
jstl之set与out小例
查看>>
apploc.bat
查看>>
配置Thunderbird支持msn邮箱,无需webmail插件(测试通过)
查看>>
乱撞解决word只能以安全模式启动
查看>>
Oracle外部表小例
查看>>
在VS.NET的VC++中运行控制台程序后暂停
查看>>
Linux下rz,sz与ssh,SecureCRT的配合使用
查看>>
Oracle EBS R12 - 以Excel查看输出格式为“文本”的请求时乱码
查看>>
DB2数据库常见问题汇总
查看>>
db2关闭命令行CLP自动提交
查看>>
db2像oracle一样使用hints(guidelines)
查看>>
db2中获取某个表/索引占用空间的大小
查看>>
db2 - 一个bigint问题
查看>>
Python 值传递和引用传递
查看>>
计算Windows下目录大小
查看>>
python web框架企业实战详解(第六期)\第三课时-css&bootstrap
查看>>
python web框架企业实战详解(第六期)\第三课时-ajax&jquery&webpy
查看>>