Skip to content

Commit

Permalink
fix: use a modern hash when fingerprinting. #1189
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Jul 13, 2021
1 parent caf7639 commit 367f7c4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@ Unreleased

- TOML parsing now uses the `tomli`_ library.

- Use a modern hash algorithm when fingerprinting to speed HTML reports
(`issue 1189`_).

.. _Django coverage plugin: https://pypi.org/project/django-coverage-plugin/
.. _issue 1150: https://github.com/nedbat/coveragepy/issues/1150
.. _issue 1168: https://github.com/nedbat/coveragepy/issues/1168
.. _issue 1189: https://github.com/nedbat/coveragepy/issues/1189
.. _tomli: https://pypi.org/project/tomli/


Expand Down
16 changes: 8 additions & 8 deletions coverage/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,21 +198,21 @@ def filename_suffix(suffix):


class Hasher:
"""Hashes Python data into md5."""
"""Hashes Python data for fingerprinting."""
def __init__(self):
self.md5 = hashlib.md5()
self.hash = hashlib.new("sha3_256")

def update(self, v):
"""Add `v` to the hash, recursively if needed."""
self.md5.update(str(type(v)).encode("utf8"))
self.hash.update(str(type(v)).encode("utf8"))
if isinstance(v, str):
self.md5.update(v.encode('utf8'))
self.hash.update(v.encode('utf8'))
elif isinstance(v, bytes):
self.md5.update(v)
self.hash.update(v)
elif v is None:
pass
elif isinstance(v, (int, float)):
self.md5.update(str(v).encode("utf8"))
self.hash.update(str(v).encode("utf8"))
elif isinstance(v, (tuple, list)):
for e in v:
self.update(e)
Expand All @@ -230,11 +230,11 @@ def update(self, v):
continue
self.update(k)
self.update(a)
self.md5.update(b'.')
self.hash.update(b'.')

def hexdigest(self):
"""Retrieve the hex digest of the hash."""
return self.md5.hexdigest()
return self.hash.hexdigest()


def _needs_to_implement(that, func_name):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


class HasherTest(CoverageTest):
"""Test our wrapper of md5 hashing."""
"""Test our wrapper of fingerprint hashing."""

run_in_temp_dir = False

Expand Down

0 comments on commit 367f7c4

Please sign in to comment.