From 9a1caee4128fc050ccc6f54ccfe137dee3f5ee51 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Sun, 18 Mar 2018 20:56:29 -0700 Subject: [PATCH] Add helper function hash_file. --- etiquette/helpers.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/etiquette/helpers.py b/etiquette/helpers.py index 0db28f0..b10651d 100644 --- a/etiquette/helpers.py +++ b/etiquette/helpers.py @@ -4,6 +4,7 @@ codebase but don't deserve to be methods of any class. ''' import datetime +import hashlib import math import mimetypes import os @@ -161,6 +162,15 @@ def get_mimetype(filepath): mimetype = mimetypes.guess_type(filepath)[0] return mimetype +def hash_file(filepath, hasher): + bytestream = read_filebytes(filepath) + for chunk in bytestream: + hasher.update(chunk) + return hasher.hexdigest() + +def hash_file_md5(filepath): + return hash_file(filepath, hasher=hashlib.md5()) + def hyphen_range(s): ''' Given a string like '1-3', return numbers (1, 3) representing lower