Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/commoncode/hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ def checksum(location, name, base64=False):
"""
Return a checksum from the content of the file at ``location`` using the ``name`` checksum
algorithm. The checksum is a string as a hexdigest or is base64-encoded is ``base64`` is True.

Return None if ``location`` is not a file or an empty file.
"""
if not filetype.is_file(location):
return
Expand Down Expand Up @@ -290,6 +292,9 @@ def multi_checksums(location, checksum_names=("md5", "sha1", "sha256", "sha512",
if not filetype.is_file(location):
return {name: None for name in checksum_names}
file_size = get_file_size(location)
if file_size == 0:
return {name: None for name in checksum_names}

hashers = {
name: get_hasher_instance_by_name(name=name, total_length=file_size)
for name in checksum_names
Expand Down
Empty file added tests/data/hash/empty
Empty file.
5 changes: 5 additions & 0 deletions tests/test_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,8 @@ def test_checksum_from_chunks_from_stream_is_same_as_plain(self):
for _ in range(100):
result2.update(chunk)
assert result1 == result2.hexdigest()

def test_checksum_empty_file(self):
test_file = self.get_test_loc("hash/empty")
checksums = multi_checksums(location=test_file, checksum_names=("sha1",))
assert checksums == {"sha1": None}