hmac模块:MD5或SHA摘要算法

 

improt hmac
from binascii import b2a_hex, b2a_base64

def
hashMac(text, key='', encode='16'): obj = hmac.new(key, text) if encode == '16': # MD5算法获取的数据摘要长度是128bits,十六进制字符串的每个字符占位为4bits,因此最后得到的字符串长度是128/4=32 return obj.hexdigest().upper().strip()
     # return b2a_hex(obj.digest()).upper().strip()
elif encode == '2': # MD5算法获取的数据摘要长度是128bits,二进制字符串的每个字符为一个字节为8bits,因此最后得到的字符串长度是128/8=16 return obj.digest().upper().strip() elif encode == '64': # MD5算法获取的数据摘要长度是128bits,六十四进制字符串的每个字符占位为8bits,因此最后得到的字符串长度是128/8=16 return b2a_base64(obj.digest()).upper().strip()

 

posted on 2018-04-04 16:00  myworldworld  阅读(151)  评论(0)    收藏  举报

导航