import hashlib


def md5_sum(filename):
    """Figures out the md5 sum for a file given through the filename.
    If the file has an error on opening, the MD5sume comes back as 0.

    Source:
    http://www.pythoncentral.io/hashing-files-with-python/
    """
    BLOCKSIZE = 65536
    hasher = hashlib.md5()

    # Makes sure file can be read. May be come back permission denied.
    try:
        with open(filename, 'rb') as afile:
            buf = afile.read(BLOCKSIZE)
            while len(buf) > 0:
                hasher.update(buf)
                buf = afile.read(BLOCKSIZE)
        return hasher.hexdigest()
    except:
        print("Error reading the file: " + filename)
        return "0"