Python计算hash值——hashlib模块

#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问题

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值