-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
147 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
wheel | ||
mypy | ||
pytest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,72 @@ | ||
import copy | ||
import base64 | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from ._crc32c import big_endian, crc32, crc32c, hardware_based | ||
|
||
if TYPE_CHECKING: | ||
from typing_extensions import Buffer, Self | ||
|
||
|
||
class CRC32CHash: | ||
"""Wrapper class for crc32c. Tries to conform to the interface of `hashlib` classes.""" | ||
|
||
class Crc32cHash: | ||
@property | ||
def digest_size(self): | ||
def digest_size(self) -> int: | ||
""" | ||
The size of the resulting hash in bytes. | ||
""" | ||
return 4 | ||
|
||
@property | ||
def name(self): | ||
def block_size(self) -> int: | ||
""" | ||
The internal block size of the hash algorithm in bytes. | ||
""" | ||
return 1 | ||
|
||
@property | ||
def name(self) -> str: | ||
""" | ||
The canonical name of this hash, | ||
always lowercase and always suitable as a parameter to new() to create another hash of this type. | ||
""" | ||
return "crc32c" | ||
|
||
def __init__(self, data): | ||
def __init__(self, data: Buffer = b"") -> None: | ||
""" | ||
Initialise the hash object with an optional bytes-like object. | ||
""" | ||
self._checksum = crc32c(data) | ||
|
||
def update(self, __data): | ||
self._checksum = crc32c(__data, self._checksum) | ||
|
||
def base64(self): | ||
return base64.b64encode(self.digest()).decode(encoding="ascii") | ||
def update(self, data: Buffer) -> None: | ||
""" | ||
Update the hash object with the bytes-like object. | ||
Repeated calls are equivalent to a single call with the concatenation of all the arguments: | ||
m.update(a); m.update(b) is equivalent to m.update(a+b). | ||
""" | ||
self._checksum = crc32c(data, self._checksum) | ||
|
||
def digest(self): | ||
def digest(self) -> bytes: | ||
""" | ||
Return the digest of the data passed to the update() method so far. | ||
This is a bytes object of size digest_size which may contain bytes in the whole range from 0 to 255. | ||
""" | ||
return self._checksum.to_bytes(4, "big") | ||
|
||
def hexdigest(self): | ||
def hexdigest(self) -> str: | ||
""" | ||
Like digest() except the digest is returned as a string object of double length, | ||
containing only hexadecimal digits. | ||
This may be used to exchange the value safely in email or other non-binary environments. | ||
""" | ||
return self.digest().hex() | ||
|
||
def copy(self): | ||
return copy.copy(self) | ||
def copy(self) -> Self: | ||
""" | ||
Return a copy (“clone”) of the hash object. This can be used to efficiently compute | ||
the digests of data sharing a common initial substring. | ||
""" | ||
res = type(self)() | ||
res._checksum = self._checksum | ||
return res |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from typing_extensions import Buffer | ||
|
||
big_endian: int | ||
hardware_based: bool | ||
|
||
|
||
def crc32(data: Buffer, value: int = 0, gil_release_mode: int = -1) -> int: ... | ||
def crc32c(data: Buffer, value: int = 0, gil_release_mode: int = -1) -> int: ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters