python 常用模块---hashlib

大致记录一下hashlib模块md5方法使用过程中踩过的坑

>>> import hashlib
>>> hash = hashlib.md5()
>>> hash.update(b'timgo')
>>> hash.hexdigest()
'594a21ea0be60ae5f36ee00ec177fab3'
>>> hash.update(b'timgo')
>>> hash.hexdigest()
'fceb5c9110c53b489b0cd5e7b04d496a'

可以看到,同样的字符串,两次输出的md5值不一样,这肯定是不对的。为什么呢,再来看看

>>> import hashlib
>>> hash = hashlib.md5()
>>> hash.update(b'timgo')
>>> hash.hexdigest()
'594a21ea0be60ae5f36ee00ec177fab3'
>>> hash = hashlib.md5()
>>> hash.update(b'timgo')
>>> hash.hexdigest()
'594a21ea0be60ae5f36ee00ec177fab3'

这样两次输入就一样了,明白了,每次update之前,需要重新获取hashlib.md5()方法。

原文地址:https://www.cnblogs.com/Go-Spurs-Go/p/11014626.html