好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Python的hashlib模块

Python的hashlib提供了常用的摘要算法,比如md5, sha1, sha224, sha256, sha384, sha512等。
常用的函数:
md5=hashlib.md5()  生成一个md5 hash对象
md5.update("xxxxxxxxx") 对字符串进行md5更新处理,m.update(a)之后在 m.update(b),相当于m.update(a+b)
md5.digest() 返回二进制的加密结果
md5.hexdigest() 返回十六进制的机密结果
练习文件的md5加密后比较:
import hashlib
def getHash(file_path):
    f = open(file_path, "rb")   #以二进制读模式打开
    line = f.readline()
    hash = hashlib.md5()
    while(line):
        hash.update(line)
        line = f.readline()
    f.close()
    return hash.hexdigest()
def IsHashEqual(file_path1, file_path2):
    str1 = getHash(file_path1)
    str2 = getHash(file_path2)
    return str1 == str2
if __name__ == '__main__':
    if IsHashEqual("E:\\GIT\\1.txt", "E:\\GIT\\2.txt"):
        print "文件内容相同!"
    else:
        print "文件内容不同!"

起步

对于一些字符,python2和python3的md5加密出来是不一样的.

?  

123456789 # python2.7         pwd         =         "xxx"         +         chr         (         163         )         +         "fj"         checkcode         =         hashlib.md5(pwd).hexdigest()         print         checkcode         # ea25a328180680aab82b2ef8c456b4ce          # python3.6         pwd         =         "xxx"         +         chr         (         163         )         +         "fj"         checkcode         =         hashlib.md5(pwd.encode(         "utf-8"         )).hexdigest()         print         (checkcode)         # b517e074034d1913b706829a1b9d1b67        

按代码差异来将,就是在 python3 中需要对字符串进行  encode  操作,如果没有则会报错:

?  

12 checkcode         =         hashlib.md5(pwd).hexdigest()         TypeError:         Unicode         -         objects must be encoded before hashing        

这是因为加密时需要将字符串转化为  bytes  类型,3默认编码是  utf-8  .所以我用utf-8进行解码.

分析

如果字符串中没有  chr(163)  ,那么两个版本结果是一致的,也就是说问题出在这个 chr(163) 中:

?  

1234567 # python2.7         >>>         chr         (         163         )         '\xa3'          # python3.6         >>>         chr         (         163         )         '\xa3'        

在这里说明通过  chr  得到的结果是一致的, 将它转为  bytes  类型看看:

?  

1234567 # python2.7         >>> bytes(         chr         (         163         ))         '\xa3'          # python3.6         >>>         chr         (         163         ).encode()         b         '\xc2\xa3'        

python3中,在  num<128  的时候,使用  chr(num).encode('utf-8')  得到的是  一个  字符的ascii十六进制,而  num>128  的时候,使用  chr(num).encode('utf-8')  得到的是  两个  字节的ascii十六进制.

解决 改用  latin1  编码进行解码:

?  

1234 # python3.6         pwd         =         "xxx"         +         chr         (         163         )         +         "fj"         checkcode         =         hashlib.md5(pwd.encode(         "latin1"         )).hexdigest()         print         (checkcode)          # ea25a328180680aab82b2ef8c456b4ce        

额外 为什么是  latin1  编码呢.答案还是挺有意思的.

先说chr函数,通过  help(chr)  可以查看:

?  

123 chr         (...)                    chr         (i)         -         >         Unicode         character                    Return a         Unicode         string of one character with ordinal i;         0         <         =         i <         =         0x10ffff         .        

意思是它返回Unicode编码中指定位置的一个字符.python3内部也是用Unicode表示左右字符,即str类型.而通过 encode 后会编码成  bytes  类型.

ascii编码中每个字符编码是一个byte,但只有1-127. 超过的部分128-255则属于  Extended ASCII  ,python3 中默认的ascii中不包含这部分,所以如果执行 chr(163).encode("ascii") 就会报错 'ascii' codec can't encode character '\xa3' in position 3: ordinal not in range(128)

因此需要一个含有128-255中的部分字符的编码,且采用1个Byte固定大小编码,比如 ISO 8859-1 ,也就是 latin1 .当然还有其他编码如 cp1252 也包含这些字符的.

查看更多关于Python的hashlib模块的详细内容...

  阅读:33次