Python hashlib模块 (主要记录md5加密)
python提供了一个进行hash加密的模块:hashlib
下面主要记录下其中的md5加密方式(sha1加密一样把MD5换成sha1)
- >>> import hashlib
- >>> m = hashlib.md5()
- >>> m.update("Nobody inspects")
- >>> m.update(" the spammish repetition")
- >>> m.digest()
- '\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'
- >>> m.hexdigest()
- 'bb649c83dd1ea5c9d9dec9a18df0ffe9'
对以上代码的说明:
1.首先从python直接导入hashlib模块
2.调用hashlib里的md5()生成一个md5 hash对象
3.生成hash对象后,就可以用update方法对字符串进行md5加密的更新处理
4.继续调用update方法会在前面加密的基础上更新加密
5.加密后的二进制结果
6.十六进制结果
如果只需对一条字符串进行加密处理,也可以用一条语句的方式:
- >>>print hashlib.new("md5", "Nobody inspects the spammish repetition").hexdigest()
- 'bb649c83dd1ea5c9d9dec9a18df0ffe9'
引用官方文档部分:
The following values are provided as constant attributes of the hash objects returned by the constructors:
hash.digest_size
The size of the resulting hash in bytes.
hash.block_size
The internal block size of the hash algorithm in bytes.
A hash object has the following methods:
hash.update(arg)
Update the hash object with the string arg. Repeated calls are equivalent to a single call with the concatenation of all the arguments: m.update(a); m.update(b) is equivalent to m.update(a+b).
hash.digest()
Return the digest of the strings passed to the update() method so far. This is a string of digest_size bytes which may contain non-ASCII characters, including null bytes.
hash.hexdigest()
Like digest() except the digest is returned as a string of double length, containing only hexadecimal digits. This may be used to exchange the value safely in email or other non-binary environments.
hash.copy()
Return a copy (“clone”) of the hash object. This can be used to efficiently compute the digests of strings that share a common initial substring.
Python hashlib模块 (主要记录md5加密)的更多相关文章
- python hashlib模块 md5加密 sha256加密 sha1加密 sha512加密 sha384加密 MD5加盐
python hashlib模块 hashlib hashlib主要提供字符加密功能,将md5和sha模块整合到了一起,支持md5,sha1, sha224, sha256, sha384, ...
- python hashlib模块学习
目录 hashlib 模块 破解密码 hmac 模块 hashlib 模块 1.干嘛用的: 对字符进行加密,其实就是一个自定义的字符编码表,我们原来接触的是计算机语言0和1然后转化成字符,而hashl ...
- Python hashlib 模块
使用 md5 加密 import hashlib m = hashlib.md5() m.update('hello world'.encode('utf-8')) # 加密的字符串需要先编码成 ut ...
- python hashlib模块 logging模块 subprocess模块
一 hashlib模块 import hashlib md5=hashlib.md5() #可以传参,加盐处理 print(md5) md5.update(b'alex') #update参数必须是b ...
- python hashlib模块
用于加密相关的操作,3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法 import hashlib m=hash ...
- 关于python hashlib模块的使用
hashlib hashlib主要提供字符加密功能,将md5和sha模块整合到了一起,支持md5,sha1, sha224, sha256, sha384, sha512等算法 #!/usr/bin/ ...
- python --- hashlib模块使用详解
这个模块实现了一个通用的接口来实现多个不同的安全哈希和消息摘要算法.包括FIPS安全散列算法SHA1,SHA224,SHA256,SHA384和SHA512(在FIPS 180-2中定义)以及RSA的 ...
- Python有关模块学习记录
1 pandas numpy模块 首先安装搭建好jupyter notebook,运行成功后的截图如下: 安装使用步骤(PS:确定Python安装路径和安装路径里面Scripts文件夹路径已经配置到环 ...
- 【学习】Python os模块常用方法 记录
记录一些工作中常用到的用法 os.walk() 模块os中的walk()函数可以遍历文件夹下所有的文件. os.walk(top, topdown=Ture, onerror=None, follow ...
随机推荐
- PowerDesigner自定义Word导出格式
本文主要讲解如何将PDM的表结构导出成word以及如何自定义导出格式和显示效果,希望对大家有所帮助...(以下步骤以PowerDesigner 15版本为例) 一.新建导出报表,进入报表设计界面: 1 ...
- [AS/400] 基本概念
本文内容源于 Go4AS400 在 AS/400 中,AS 代表着 Application System.AS/400 是一个安全性高的系统,可以限制用户只能访问.处理特定的信息.AS/400 整合了 ...
- 【模拟赛】BYVoid魔兽世界模拟赛 解题报告
题目名称(点击进入相关题解) 血色先锋军 灵魂分流药剂 地铁重组 埃雷萨拉斯寻宝 源文件名(.c/.cpp/.pas) scarlet soultap subway eldrethalas 输入文件名 ...
- Postman 基本操作学习
History 所有使用postman发送的request都会保存在这里.点击之后会在当前Tab打开. 参考: Requests History Environments 这里用来设定当前reques ...
- C - How Many Tables - HDU-1213
某个人举办生日宴会邀请了很多人来参加,不过呢,这些人有个毛病他们只会与熟悉人的坐在一起,当然他们也信奉朋友的朋友也是朋友这一法则,所以问最少需要多少张桌子...... 好吧我承认这才是裸并查集.... ...
- Spring 3.x企业应用开发实战(9-1)----依赖注入
Spring中的依赖注入方式:属性注入.构造函数注入和工厂方式注入. 1.属性注入 属性注入即通过setXxx()方法注入Bean的属性值或依赖对象. 属性注入要求Bean提供一个默认的构造函数,在J ...
- javaweb笔记5之请求编码问题
post提交: 设置实体内容的编码:request.setCharacterEncoding("utf-8"); 注意:一定要在获取所有参数之前设置,否则设置无效! get方式提交 ...
- 理解Android的startservice和bindservice(转)
一.首先,让我们确认下什么是service? service就是android系统中的服务,它有这么几个特点:它无法与用户直接进行交互.它必须由用户或者其他程序显式的启动.它的优先级比较高,它比处于前 ...
- 利用MyEclipse配置S2SH三大框架篇-Spring配置
1.配置完Struts2后,然后配置Spring 2.单击“MyEclipse->Project Capabilities->Add Spring Capabilities” 3.选择Sp ...
- 缓存的概念(反向代理、CDN)
缓存在我们的日常开发中随处可见,理解缓存的概念,本质就是就近处理,比如很多热点数据,访问量很多,我们需要使用,就可以把它 缓存起来,然后下次访问就不用再去数据库的去查询了,而是直接使用缓存,现在说说大 ...