看代码:
>>> import hashlib
>>> md5=hashlib.md5()
>>> md5.update('123'.encode('utf-8'))
>>> print(md5.hexdigest())
202cb962ac59075b964b07152d234b70
>>> md5.update('123'.encode('utf-8'))
>>> print(md5.hexdigest())
4297f44b13955235245b2497399d7a93 //有没有发现 和上面对字符串“123”md5加密后的结果不一样
经过测试发现 md5.update 会将每次字符串拼接,我们看下 “123123”的结果如下:
>>> md5=hashlib.md5() //重新定义md5
>>> md5.update('123123'.encode('utf-8'))
>>> print(md5.hexdigest())
4297f44b13955235245b2497399d7a93 //发现123123 和上面的结果一样 就验证了 md5.update 是拼接的效果
为了防止这种干扰我们每次都要重新实例化:
每次使用update之前都要重新定义:md5=hashlib.md5()
eg:
import hashlib
f = open('top10.md5.txt','w')
p = open('top10.txt','r')
for i in p:
md5 = hashlib.md5()
i=i.strip() //过滤空白字符
md5.update(i.encode('utf-8'))
c=md5.hexdigest()
c=c+'\n'
f.write(c)
====top10.txt=======
123456
password
12345678
qwerty
12345
123456789
1234567
1234567890
abc123
111111
=====top10.md5.txt===
e10adc3949ba59abbe56e057f20f883e
5f4dcc3b5aa765d61d8327deb882cf99
25d55ad283aa400af464c76d713c07ad
d8578edf8458ce06fbc5bb76a58c5ca4
827ccb0eea8a706c4c34a16891f84e7b
25f9e794323b453885f5181f1b624d0b
fcea920f7412b5da7be0cf42b8c93759
e807f1fcf82d132f9bb018ca6738a19f
e99a18c428cb38d5f260853678922e03
96e79218965eb72c92a549dd5a330112