Skip to content

Instantly share code, notes, and snippets.

@gustavofonseca
Last active January 14, 2016 19:06
Show Gist options
  • Save gustavofonseca/feb12ef62fd7f1643b5b to your computer and use it in GitHub Desktop.
Save gustavofonseca/feb12ef62fd7f1643b5b to your computer and use it in GitHub Desktop.
import hashlib
def checksum_file(filepath, algorithm=hashlib.md5):
"""
Calcula o ``checksum`` de um arquivo em disco.
:param filepath: caminho no sistema de arquivos.
:param algorithm: (opcional) o padrão é ``hashlib.md5``.
"""
with open(filepath, 'rb') as fp:
hash = algorithm()
while True:
chunk = fp.read(1024)
if not chunk:
break
hash.update(chunk)
return hash.hexdigest()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment