diff --git a/CHANGELOG.md b/CHANGELOG.md index 835f301..795b37b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * Added a `gil_relese_mode` parameter to the `CRC32CHash` constructor used by all underlying `crc32c` function calls (#51). +* Added `CRC32CHash.checksum` auxiliary property. ## [2.6] diff --git a/README.rst b/README.rst index 2fd941e..5487346 100644 --- a/README.rst +++ b/README.rst @@ -59,13 +59,15 @@ It can be set to the following values: On top of the ``crc32c`` function, a ``CRC32CHash(data=b"", gil_release_mode=-1)`` class is also offered. It is modelled after the "hash objects" of the ``hashlib`` module -of the standard library: +of the standard library. It also offers a ``checksum`` property: .. code-block:: python crc32c_hash = crc32c.CRC32CHash() crc32c_hash.update(b'hello') crc32c_hash.update(b' world') + print(crc32c_hash.checksum == crc32c.crc32c(b'hello world')) + # True print(crc32c_hash.digest()) # b'\xc9\x94e\xaa' digest_as_int = int.from_bytes(crc32c_hash.digest(), "big") diff --git a/src/crc32c/__init__.py b/src/crc32c/__init__.py index 3cfda47..63507af 100644 --- a/src/crc32c/__init__.py +++ b/src/crc32c/__init__.py @@ -36,6 +36,13 @@ def name(self) -> str: """ return "crc32c" + @property + def checksum(self) -> int: + """ + The checksum calculated so far. Not part of the hashlib interface. + """ + return self._checksum + def __init__(self, data: Buffer = b"", gil_release_mode: int = -1) -> None: """ Initialise the hash object with an optional bytes-like object. diff --git a/test/test_crc32c.py b/test/test_crc32c.py index dd4875a..060dbb8 100644 --- a/test/test_crc32c.py +++ b/test/test_crc32c.py @@ -174,6 +174,7 @@ def test_copy(self) -> None: class TestSpecificValues: @staticmethod def _check_values(crc32c_hash: crc32c.CRC32CHash, crc: int) -> None: + assert crc32c_hash.checksum == crc assert int.from_bytes(crc32c_hash.digest(), "big") == crc assert len(crc32c_hash.digest()) == 4 assert int(crc32c_hash.hexdigest(), 16) == crc