From: funilrys <contact@funilrys.com>
Date: Tue, 5 Nov 2024 22:06:39 +0100
Subject: Get rid of cryptography - as dependency.

This patch touches #401.
This patch touches #381.
Forwarded: https://github.com/funilrys/PyFunceble/issues/381

cc: @Nilsonfsilva @DandelionSprout
(cherry picked from commit a781c0569c7d6318c2f074aba951cf60a3bbc171)
---
 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 c591750..24c2f60 100644
--- a/PyFunceble/helpers/hash.py
+++ b/PyFunceble/helpers/hash.py
@@ -50,11 +50,9 @@ License:
     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 @@ class HashHelper:
         if not isinstance(value, str):
             raise TypeError(f"<value> 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> ({value!r}) in an unknown algorithm ({self.algo!r})."
+                f"<value> ({value!r}) in an unknown algorithm "
+                f"({hashlib.algorithms_available})."
             )
 
         self._algo = value
@@ -118,13 +117,6 @@ class 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 @@ class HashHelper:
 
         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 @@ class HashHelper:
                 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 @@ class HashHelper:
         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 9e00c51..951810f 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 19134c2..7dbcd0b 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
