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 ...
随机推荐
- Java---计算机贷款支付额计算(用对话框实现)
本例演示如何编写程序来计算贷款支付问题. 下面是编写程序的步骤: 1.提示用户输入年利率.年数和贷款总额 2.利用年利率算出月利率 3.通过前面的公式计算月支付额. 4.计算总支付额,它是月支付额乘以 ...
- Agri-Net(prim算法,最小生成树问题)
看图便知道: 来来上代码: #include <cstdio>#include <cstdlib>#include <cstring>#includ ...
- [Locked] Palindrome Permutation I & II
Palindrome Permutation I Given a string, determine if a permutation of the string could form a palin ...
- BagTest
package cn.aust.zyw.demo; import java.util.Iterator; /** * Created by zyw on 2016/2/17. */ public cl ...
- 「书评」SAP内存计算——HANA
因为工作关系,长期跟SAP打交道,所以去年就对HANA有了一些了解,只是公司目前的应用规模还较小,暂时没有上马HANA的打算,但是提前作一些学习还是很有必要的.正好清华大学出版社最近出版了这本< ...
- 高性能MySql进化论(九):查询优化器常用的优化方式
1 介绍 1.1 处理流程 当MYSQL 收到一条查询请求时,会首先通过关键字对SQL语句进行解析,生成一颗“解析树”,然后预处理器会校验“解析树”是否合法(主要校验数据列和表明 ...
- POJ1184-------操作分离的BFS
题目地址:http://poj.org/problem?id=1184 题目意思: 给你两个6位数,一个是起始值,一个最终值 初始光标在最左边 你可以左移或者右移光变 在光标处+1或者-1 在光标处和 ...
- android.view.WindowLeaked解决办法
08-07 14:51:28.129: E/WindowManager(22277): Activity com.xxx.xxx.xxx.xxx.LoginActivity has leaked wi ...
- 当在类中的 Parcelable 接口使用 ArrayList < customObject > android 系统: nullPointerException
我想使调用音乐使用 parcelable,所以我可以访问两个不同的活动中的音乐的实例的类.我不想使用可序列化的速度的目的.当我尝试将它使用传递时,我总是收到 nullPointerException: ...
- LVS图解 ---阿里
LVS在大规模网络环境中的应用 1. SLB总体架构 LVS本身是开源的,我们对它进行了多方面的改进,并且也已开源-https://github.com/alibaba/LVS. 接下 ...