Python hashlib模块

 1 import hashlib
 2 
 3 s=hashlib.md5() #<md5 HASH object @ 0x000001AEA891C418>
 4 
 5 s.update('hello world'.encode('utf8'))
 6 print(s.hexdigest())#5eb63bbbe01eeed093cb22bb8f5acdc3
 7 #python3默认为UNICODE编码,需要使用utf8进行编码,update函数参数需要字节"bytes"类型
 8 hexdigest方法用来展示十六进制表达方式
 9 
10 
11 s.update('alex'.encode('utf8'))
12 print(s.hexdigest())#82bb8a99b05a2d8b0de2ed691576341a
13 s1=hashlib.md5()
14 s1.update('hello worldalex'.encode('utf8'))
15 print(s1.hexdigest())#82bb8a99b05a2d8b0de2ed691576341a
16 #在创建完md5对象进行转换后,再次进行update函数时前一次会影响后一次,后一次转换为前一次与后一次的字符串拼接后转换
17 
18 m=hashlib.sha256()
19 m.update('hello world'.encode('utf8'))
20 print(m.hexdigest())#b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
21 #SHA256加密方式

 

posted on 2018-01-14 18:02  可爱的春哥  阅读(89)  评论(0)    收藏  举报

导航