#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=utf8
from hashlib import md5
from hashlib import sha1
from hashlib import sha224
from hashlib import sha384
from hashlib import sha512
import 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问题