python hashlib模块

作用:可以用来加密。md5、sha1、sha256、sha512

 1 >>> import hashlib                #导入hashlib加密模块
 2 >>> m=hashlib.md5()            #定义一个md5对象
 3 >>> print(m)
 4 <md5 HASH object @ 0x7fb78f99c6c0>      #输出对象m可以看出是一个md5 hash 对象
 5 >>> m.update("hello world".encode('utf8'))      #对hello world进行md5加密,字符编码使用utf8格式,python3中字符编码默认都是unicode格式。
 6 >>> 
 7 >>> print m
 8 <md5 HASH object @ 0x7fb78f99c6c0>
 9 >>> print m.hexdigest()          #以十六进制方式输出m,会发现里面的字符没有超过f的(十六进制范围)。
10 5eb63bbbe01eeed093cb22bb8f5acdc3        
11 >>> m.update('qiyuanchang'.encode('utf8'))    #这里的update是在上一次hello wold的基础上基础加密的,不是一次新的加密。
12 >>> print m.hexdigest()
13 8e307de43044db33276a97776259562b
14
>>> m2=hashlib.md5()              #这里重新创建一个hash加密对象m2,加密字符和上面的两次操作一样,发现加密后的结果是一样的。
15 >>> m2.update("hello worldqiyuanchang".encode('utf8'))
16 >>> print m2.hexdigest()
17 8e307de43044db33276a97776259562b
原文地址:https://www.cnblogs.com/qiyuanchang/p/7401541.html