Python计算hash值——hashlib模块
来源:互联网 发布:阿里云单域名管理 编辑:程序博客网 时间:2024/05/16 16:04
#python3.4
注意Python的版本2与3的区别
hashlib module - A common interface to many hash functions.
Hash objects have these methods:
- update(arg): Update the hash object with the bytes in arg. Repeated calls
are equivalent to a single call with the concatenation of all
the arguments.
生成hash对象后,就可以用update方法对字符串进行md5加密的更新处理
- digest(): Return the digest of the bytes passed to the update() method
so far.
得到的bytes:b'\x8f\xe2\xf6\xf8Y4\x11\xff\xb2S+\x00-\nyv'
- hexdigest(): Like digest() except the digest is returned as a unicode
object of double length, containing only hexadecimal digits.
得到的unicode 8fe2f6f8593411ffb2532b002d0a7976
hexdigest()常用
- 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.
#coding=utf8from hashlib import md5from hashlib import sha1from hashlib import sha224from hashlib import sha384from hashlib import sha512import hashlib#__all__ = ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'new', 'algorithms_guaranteed', 'algorithms_available', 'pbkdf2_hmac')#Python计算字符串的hash值def hashForString(method,srcbyte): #将字符串和汉字转化成byte类型 srcbyte = srcbyte.encode("gb2312") #new(name, data=b'') testnew = hashlib.new(method,data=srcbyte).hexdigest() print(testnew) if method == 'md5': m = md5() m.update(srcbyte) srcbyte = m.hexdigest() elif method == 'sha1': s = sha1() s.update(srcbyte) srcbyte = s.hexdigest() elif method == 'sha224': s = sha224() s.update(srcbyte) srcbyte = s.hexdigest() elif method == 'sha384': s = sha384() s.update(srcbyte) srcbyte = s.hexdigest() elif method == 'sha512': s = sha512() s.update(srcbyte) srcbyte = s.hexdigest() return srcbyte print (hashForString("md5","chaosju"))
程序遇到的问题1:
SyntaxError: Non-UTF-8 code starting with '\xc1' in file...
程序中出现中文,运行的时候出现如下错误:
SyntaxError: Non-UTF-8 code starting with '\xc1' in file E:\...\xxx.py on line 8, but no encoding declared; see
导致出错的根源就是编码问题。
解决方案是:
在程序最上面加上:# coding=gbk
这样程序就可以正常运行了。
程序遇到的问题2:
“Unicode-objects must be encoded before hashing”,意思是在进行哈希运算前,需要对数据进行编码
解决方案是:
srcbyte = srcbyte.encode("gb2312")
这种方式同样解决汉字的hash问题
- Python计算hash值——hashlib模块
- Python模块学习 ---- hashlib hash加密
- Python模块学习——hashlib
- Python模块学习笔记— —hashlib
- hash模块 hashlib 和hmac
- Python hashlib模块
- python模块之hashlib
- Python hashlib模块
- python模块之hashlib
- python hashlib模块
- python 模块学习 hashlib
- Python hashlib模块
- Python hashlib、hmac模块
- python 模块学习 hashlib
- python 模块学习 hashlib
- python hashlib 模块
- python hashlib模块
- Python的hashlib模块
- 手机晃动的检测 相关资料
- 手机分辨率相关信息
- 手势识别兼容Android 1.x和2.x的代码(转载)
- 手机底部菜单相关内容
- android手机gps相关资料
- Python计算hash值——hashlib模块
- android GPS开启方法 代码(转载)
- H2数据库攻略
- Android 调用相册 拍照 实现系统控件缩放 切割图片 .
- Android UI学习 - TableLayout
- python(二)练习笔记:7道例题
- Eclipse3.7默认字体修改-帮你找回Courser-New字体
- 从android系统图库中取图片的代码
- 成v程序现场v程序程序从许昌旭