From a781c0569c7d6318c2f074aba951cf60a3bbc171 Mon Sep 17 00:00:00 2001 From: funilrys Date: Tue, 5 Nov 2024 22:06:39 +0100 Subject: [PATCH] Get rid of cryptography - as dependency. This patch touches #401. This patch touches #381. cc: @Nilsonfsilva @DandelionSprout --- PyFunceble/helpers/hash.py | 26 +++++++++----------------- requirements.txt | 1 - requirements.win.txt | 1 - 3 files changed, 9 insertions(+), 19 deletions(-) diff --git a/PyFunceble/helpers/hash.py b/PyFunceble/helpers/hash.py index c5917509..24c2f602 100644 --- a/PyFunceble/helpers/hash.py +++ b/PyFunceble/helpers/hash.py @@ -50,11 +50,9 @@ limitations under the License. """ +import hashlib from typing import Optional, Union -from cryptography.hazmat.backends import default_backend -from cryptography.hazmat.primitives import hashes - from PyFunceble.helpers.file import FileHelper @@ -97,11 +95,12 @@ def algo(self, value: str) -> None: if not isinstance(value, str): raise TypeError(f" should be {str}, {type(value)} given.") - value = value.upper() + value = value.lower() - if not hasattr(hashes, value): + if value not in hashlib.algorithms_available: raise ValueError( - f" ({value!r}) in an unknown algorithm ({self.algo!r})." + f" ({value!r}) in an unknown algorithm " + f"({hashlib.algorithms_available})." ) self._algo = value @@ -118,13 +117,6 @@ def set_algo(self, value: str) -> "HashHelper": return self - def __get_hash(self) -> hashes.Hash: - """ - Provides the Hash to use. - """ - - return hashes.Hash(getattr(hashes, self.algo)(), backend=default_backend()) - def hash_file(self, file_path: str) -> str: """ Hashes the content of the given file. @@ -135,7 +127,7 @@ def hash_file(self, file_path: str) -> str: block_size = 4096 - digest = self.__get_hash() + digest = hashlib.new(self.algo) with FileHelper(file_path).open("rb") as file_stream: block = file_stream.read(block_size) @@ -144,7 +136,7 @@ def hash_file(self, file_path: str) -> str: digest.update(block) block = file_stream.read(block_size) - return digest.finalize().hex() + return digest.hexdigest() def hash_data(self, data: Union[str, bytes]) -> str: """ @@ -163,7 +155,7 @@ def hash_data(self, data: Union[str, bytes]) -> str: if isinstance(data, str): data = data.encode() - digest = self.__get_hash() + digest = hashlib.new(self.algo) digest.update(data) - return digest.finalize().hex() + return digest.hexdigest() diff --git a/requirements.txt b/requirements.txt index b6635b73..86d63960 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,5 @@ alembic colorama -cryptography>=42.0 dnspython[DOH]~=2.6.0 domain2idna~=1.12.0 inflection diff --git a/requirements.win.txt b/requirements.win.txt index ab923d81..542f6b99 100644 --- a/requirements.win.txt +++ b/requirements.win.txt @@ -1,6 +1,5 @@ alembic colorama -cryptography>=42.0 dnspython[DOH]~=2.6.0 domain2idna~=1.12.0 inflection