Skip to content

Instantly share code, notes, and snippets.

@easydevmixin
Created October 11, 2016 12:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save easydevmixin/52b9b259f114b62204a40b9c0e44727c to your computer and use it in GitHub Desktop.
Save easydevmixin/52b9b259f114b62204a40b9c0e44727c to your computer and use it in GitHub Desktop.
import hashlib
from io import BytesIO
BLOCKSIZE = 65536
def digest_file(filename, hasher_alg=hashlib.sha1()):
hasher = hasher_alg
with open(filename, 'rb') as afile:
buf = afile.read(BLOCKSIZE)
while len(buf) > 0:
hasher.update(buf)
buf = afile.read(BLOCKSIZE)
return hasher.hexdigest()
def digest_string(content, encoding='utf8', hasher_alg=hashlib.sha1()):
hasher = hasher_alg
content_io = BytesIO(content.encode(encoding))
buf = content_io.read(BLOCKSIZE)
while len(buf) > 0:
hasher.update(buf)
buf = content_io.read(BLOCKSIZE)
return hasher.hexdigest()
import hashlib
from io import BytesIO
BLOCKSIZE = 65536
def digest_file_pythonic(filename, hasher_alg=hashlib.sha1()):
hasher = hasher_alg
with open(filename, 'rb') as afile:
for chunk in iter(lambda: afile.read(BLOCKSIZE), b''):
hasher.update(chunk)
return hasher.hexdigest()
def digest_string_pythonic(content, encoding='utf8', hasher_alg=hashlib.sha1()):
hasher = hasher_alg
content_io = BytesIO(content.encode(encoding))
for chunk in iter(lambda: content_io.read(BLOCKSIZE), b''):
hasher.update(chunk)
return hasher.hexdigest()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment