python hashlib模块

hashlib作用:

将明文加密成密文,有两种加密方法md5,sha

实例演练:

1.md5方法

1 import hashlib
2 
3 hash = hashlib.md5()
4 hash.update("hello world".encode('utf-8'))
5 print(hash.hexdigest())

打印结果:

5eb63bbbe01eeed093cb22bb8f5acdc3

备注:

python3默认编码格式是unicode,update方法只接受bytes,所以需要进行encode解码

2.sha方法

sha有很多种方法,后面算法越来越复杂,运算效率越低。一般应用sha256

1 hash = hashlib.sha256()
2 hash.update("hello world".encode('utf-8'))
3 print(hash.hexdigest())

打印结果:

b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9

 

posted @ 2018-01-14 09:02  秦艳莉  阅读(100)  评论(0编辑  收藏  举报