Python hashlib模块实例使用详解
这篇文章主要介绍了Pythonhashlib模块实例使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
hashlib模块主要的作用:
加密保护消息安全,常用的加密算法如MD5,SHA1等。
1、查看可用的算法有哪些
hashlib_algorithms.py
#!/usr/bin/envpython #-*-coding:utf-8-*- importhashlib #始终可用的算法 print('始终可用的算法:{}'.format(sorted(hashlib.algorithms_guaranteed))) print('需要结合OpenSSL可用算法:{}'.format(sorted(hashlib.algorithms_available)))
运行效果
[root@mnt]#python3hashlib_algorithms.py 始终可用的算法:['blake2b','blake2s','md5','sha1','sha224','sha256','sha384','sha3_224','sha3_256','sha3_384','sha3_512','sha512','shake_128','shake_256'] 需要结合OpenSSL可用算法:['DSA','DSA-SHA','MD4','MD5','RIPEMD160','SHA','SHA1','SHA224','SHA256','SHA384','SHA512','blake2b','blake2s','dsaEncryption','dsaWithSHA','ecdsa-with-SHA1','md4','md5','ripemd160','sha','sha1','sha224','sha256','sha384','sha3_224','sha3_256','sha3_384','sha3_512','sha512','shake_128','shake_256','whirlpool']
2、md5加密算法(没有加盐)
hashlib_md5.py
#!/usr/bin/envpython #-*-coding:utf-8-*- importhashlib md5_obj=hashlib.md5() md5_obj.update('123456'.encode('utf-8')) print(md5_obj.hexdigest())
运行效果
[root@mnt]#python3hashlib_md5.py e10adc3949ba59abbe56e057f20f883e
3、md5加密算法(加盐)
hashlib_md5_salt.py
#!/usr/bin/envpython #-*-coding:utf-8-*- importhashlib salt='1234' md5_obj=hashlib.md5(salt.encode('utf-8')) md5_obj.update('123456'.encode('utf-8')) print(md5_obj.hexdigest())
运行效果
[root@mnt]#python3hashlib_md5_salt.py b38e2bf274239ff5dd2b45ee9ae099c9
4、sha1加密算法
hashlib_sha1.py
#!/usr/bin/envpython #-*-coding:utf-8-*- importhashlib sha1_obj=hashlib.sha1() sha1_obj.update('123456'.encode('utf-8')) print(sha1_obj.hexdigest()) hashlib_sha1.py
运行效果
[root@mnt]#python3hashlib_sha1.py 7c4a8d09ca3762af61e59520943dc26494f8941b
5、按加密算法名字进行动态加密(即hashlib.new(‘算法名字'))
hashlib_new.py
#!/usr/bin/envpython #-*-coding:utf-8-*- importhashlib importargparse lorem='HelloWorld' parser=argparse.ArgumentParser('hashlibDemo') parser.add_argument( 'hash_name', choices=hashlib.algorithms_available, help='请输入hashlib的名字' ) parser.add_argument( 'data', nargs='?', default=lorem, help='请输入要加密的数据' ) args=parser.parse_args() h=hashlib.new(args.hash_name) h.update(args.data.encode('utf-8')) print(h.hexdigest())
运行效果
[root@mnt]#python3hashlib_new.pymd5123456 e10adc3949ba59abbe56e057f20f883e [root@mnt]#python3hashlib_new.pysha1123456 7c4a8d09ca3762af61e59520943dc26494f8941b [root@mnt]#python3hashlib_new.pysha256123456 8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92 [root@mnt]#python3hashlib_new.pysha512123456 ba3253876aed6bc22d4a6ff53d8406c6ad864195ed144ab5c87621b6c233b548baeae6956df346ec8c17f5ea10f35ee3cbc514797ed7ddd3145464e2a0bab413
6、大文件切片md5加密算法
hashlib_update.py
#!/usr/bin/envpython #-*-coding:utf-8-*- importhashlib content='''Loremipsumdolorsitamet,consecteturadipisicing elit,seddoeiusmodtemporincididuntutlaboreetdoloremagna aliqua.Utenimadminimveniam,quisnostrudexercitation ullamcolaborisnisiutaliquipexeacommodoconsequat.Duis auteiruredolorinreprehenderitinvoluptatevelitessecillum doloreeufugiatnullapariatur.Excepteursintoccaecat cupidatatnonproident,suntinculpaquiofficiadeserunt mollitanimidestlaborum.''' #一次性加密:缺点文件大的话,加载到内存会导致内存溢出 h=hashlib.md5() h.update(content.encode('utf-8')) all_at_once=h.hexdigest() #利用生成器,切片加密,对大文件加密有用 defchunkize(size,text): start=0 whilestart运行效果
[root@mnt]#python3hashlib_update.py 一性次加密结果:3f2fd2c9e25d60fb0fa5d593b802b7a8 一行一行加密结果:3f2fd2c9e25d60fb0fa5d593b802b7a8以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。