Python hashlib模块实例使用详解


Posted in Python onDecember 24, 2019

这篇文章主要介绍了Python hashlib模块实例使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

hashlib模块主要的作用:

加密保护消息安全,常用的加密算法如MD5,SHA1等。

1、查看可用的算法有哪些

hashlib_algorithms.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import hashlib
# 始终可用的算法
print('始终可用的算法 : {}'.format(sorted(hashlib.algorithms_guaranteed)))
print('需要结合OpenSSL可用算法 : {}'.format(sorted(hashlib.algorithms_available)))

运行效果

[root@ mnt]# python3 hashlib_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/env python
# -*- coding: utf-8 -*-
import hashlib
md5_obj = hashlib.md5()
md5_obj.update('123456'.encode('utf-8'))
print(md5_obj.hexdigest())

运行效果

[root@ mnt]# python3 hashlib_md5.py 
e10adc3949ba59abbe56e057f20f883e

3、md5加密算法(加盐)

hashlib_md5_salt.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import hashlib

salt = '1234'
md5_obj = hashlib.md5(salt.encode('utf-8'))
md5_obj.update('123456'.encode('utf-8'))
print(md5_obj.hexdigest())

运行效果

[root@ mnt]# python3 hashlib_md5_salt.py 
b38e2bf274239ff5dd2b45ee9ae099c9

4、sha1加密算法

hashlib_sha1.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import hashlib
sha1_obj = hashlib.sha1()
sha1_obj.update('123456'.encode('utf-8'))
print(sha1_obj.hexdigest())
hashlib_sha1.py

运行效果

[root@ mnt]# python3 hashlib_sha1.py 
7c4a8d09ca3762af61e59520943dc26494f8941b

5、按加密算法名字进行动态加密(即hashlib.new(‘算法名字'))

hashlib_new.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import hashlib
import argparse

lorem = 'Hello World'

parser = argparse.ArgumentParser('hashlib Demo')
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]# python3 hashlib_new.py md5 123456
e10adc3949ba59abbe56e057f20f883e

[root@ mnt]# python3 hashlib_new.py sha1 123456
7c4a8d09ca3762af61e59520943dc26494f8941b

[root@ mnt]# python3 hashlib_new.py sha256 123456
8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92


[root@mnt]# python3 hashlib_new.py sha512 123456
ba3253876aed6bc22d4a6ff53d8406c6ad864195ed144ab5c87621b6c233b548baeae6956df346ec8c17f5ea10f35ee3cbc514797ed7ddd3145464e2a0bab413

6、大文件切片md5加密算法

hashlib_update.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import hashlib

content = '''Lorem ipsum dolor sit amet, consectetur adipisicing
elit, sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis
aute irure dolor in reprehenderit in voluptate velit esse cillum
dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.'''

#一次性加密:缺点文件大的话,加载到内存会导致内存溢出
h = hashlib.md5()
h.update(content.encode('utf-8'))
all_at_once = h.hexdigest()

#利用生成器,切片加密,对大文件加密有用
def chunkize(size, text):
  start = 0
  while start < len(text):
    chuck = text[start:start + size]
    yield chuck
    start += size
  return

#一行一行加密
h = hashlib.md5()
for chunk in chunkize(64,content.encode(('utf-8'))):
  h.update(chunk)
line_by_line = h.hexdigest()

print('一性次加密结果 : ',all_at_once)
print('一行一行加密结果 : ',line_by_line)

运行效果

[root@ mnt]# python3 hashlib_update.py 
一性次加密结果 : 3f2fd2c9e25d60fb0fa5d593b802b7a8
一行一行加密结果 : 3f2fd2c9e25d60fb0fa5d593b802b7a8

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python实用日期时间处理方法汇总
May 09 Python
利用Python的Django框架生成PDF文件的教程
Jul 22 Python
python 实现tar文件压缩解压的实例详解
Aug 20 Python
python 实现数组list 添加、修改、删除的方法
Apr 04 Python
Python中numpy模块常见用法demo实例小结
Mar 16 Python
python中比较两个列表的实例方法
Jul 04 Python
python实现图片二值化及灰度处理方式
Dec 07 Python
tensorflow 自定义损失函数示例代码
Feb 05 Python
Python版中国省市经纬度
Feb 11 Python
python3 logging日志封装实例
Apr 08 Python
用python按照图像灰度值统计并筛选图片的操作(PIL,shutil,os)
Jun 04 Python
Python参数传递及收集机制原理解析
Jun 05 Python
Python实现使用dir获取类的方法列表
Dec 24 #Python
django数据模型on_delete, db_constraint的使用详解
Dec 24 #Python
Python中filter与lambda的结合使用详解
Dec 24 #Python
节日快乐! Python画一棵圣诞树送给你
Dec 24 #Python
Python 3 使用Pillow生成漂亮的分形树图片
Dec 24 #Python
python保存log日志,实现用log日志画图
Dec 24 #Python
Django 限制访问频率的思路详解
Dec 24 #Python
You might like
PHP脚本的10个技巧(5)
2006/10/09 PHP
php中使用addslashes函数报错问题的解决方法
2013/02/06 PHP
node.js开发中使用Node Supervisor实现监测文件修改并自动重启应用
2014/11/04 Javascript
Javascript基础教程之数据类型 (字符串 String)
2015/01/18 Javascript
js实现点击左右按钮轮播图片效果实例
2015/01/29 Javascript
JS显示下拉列表框内全部元素的方法
2015/03/31 Javascript
nodejs实现HTTPS发起POST请求
2015/04/23 NodeJs
JavaScript面试开发常用的知识点总结
2016/08/08 Javascript
ionic开发中点击input时键盘自动弹出
2016/12/23 Javascript
jQuery 中msgTips 顶部弹窗效果实现代码
2017/08/14 jQuery
layer子层给父层页面元素赋值,以达到向父层页面传值的效果实例
2017/09/22 Javascript
jQuery实现的网站banner图片无缝轮播效果完整实例
2019/01/28 jQuery
简单了解小程序+node梳理登陆流程
2019/06/24 Javascript
JS检索下拉列表框中被选项目的索引号(selectedIndex)
2019/12/17 Javascript
js实现图片实时时钟
2020/01/15 Javascript
解决vue+elementui项目打包后样式变化问题
2020/08/03 Javascript
Ant design vue中的联动选择取消操作
2020/10/31 Javascript
[10:53]2018DOTA2国际邀请赛寻真——EG
2018/08/11 DOTA
使用BeautifulSoup爬虫程序获取百度搜索结果的标题和url示例
2014/01/19 Python
用python结合jieba和wordcloud实现词云效果
2017/09/05 Python
git进行版本控制心得详谈
2017/12/10 Python
python调用API实现智能回复机器人
2018/04/10 Python
python数字图像处理之骨架提取与分水岭算法
2018/04/27 Python
在python中对变量判断是否为None的三种方法总结
2019/01/23 Python
Python中一些深不见底的“坑”
2019/06/12 Python
在jupyter notebook 添加 conda 环境的操作详解
2020/04/10 Python
Python Scrapy框架:通用爬虫之CrawlSpider用法简单示例
2020/04/11 Python
Python函数参数定义及传递方式解析
2020/06/10 Python
使用简单的CSS3属性实现炫酷读者墙效果
2014/01/08 HTML / CSS
Booking.com西班牙:全球酒店预订
2018/03/30 全球购物
官方授权图形T恤和服装:Fifth Sun
2019/06/12 全球购物
环境工程毕业生自荐信
2013/11/17 职场文书
迟到检讨书900字
2014/01/14 职场文书
增员口号大全
2014/06/18 职场文书
2015年维修电工工作总结
2015/04/25 职场文书
Django项目配置Memcached和Redis, 缓存选择哪个更有优势
2021/04/06 Python