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
原文地址:https://www.cnblogs.com/qinyanli/p/8282347.html