ʞɔı*_*ɔıu 15
也许这样的事情?
>>> import hashlib
>>> hashes = (hashlib.md5(), hashlib.sha1())
>>> f = open('some_file', 'r')
>>> for line in f:
... for hash in hashes:
... hash.update(line)
...
>>> for hash in hashes:
... print hash.name, hash.hexdigest()
Run Code Online (Sandbox Code Playgroud)
或者循环遍历f.read(1024)或类似的东西以获得固定长度的块
from __future__ import with_statement
from hashlib import md5, sha1
filename = 'hash_one-pass.py'
hashes = md5(), sha1()
chunksize = max(4096, max(h.block_size for h in hashes))
with open(filename, 'rb') as f:
while True:
chunk = f.read(chunksize)
if not chunk:
break
for h in hashes:
h.update(chunk)
for h in hashes:
print h.name, h.hexdigest()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
747 次 |
最近记录: |