From b711bf8775844c6bd32f0b33b96cef079c98a136 Mon Sep 17 00:00:00 2001 From: Gang Li Date: Mon, 19 Feb 2024 16:36:38 +0800 Subject: [PATCH] Add content digest util method --- charon/utils/files.py | 31 ++++++++++++++++++++++--------- tests/test_util.py | 10 +++++++++- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/charon/utils/files.py b/charon/utils/files.py index ffe08bef..f15f77c4 100644 --- a/charon/utils/files.py +++ b/charon/utils/files.py @@ -58,9 +58,30 @@ def read_sha1(file: str) -> str: def digest(file: str, hash_type=HashType.SHA1) -> str: + hash_obj = _hash_object(hash_type) + # BUF_SIZE is totally arbitrary, change for your app! BUF_SIZE = 65536 # lets read stuff in 64kb chunks! + with open(file, "rb") as f: + while True: + data = f.read(BUF_SIZE) + if not data: + break + hash_obj.update(data) + + return hash_obj.hexdigest() + +def digest_content(content: str, hash_type=HashType.SHA1) -> str: + """This function will caculate the hash value for the string content with the specified + hash type + """ + hash_obj = _hash_object(hash_type) + hash_obj.update(content.encode('utf-8')) + return hash_obj.hexdigest() + + +def _hash_object(hash_type: HashType): hash_obj = None if hash_type == HashType.SHA1: hash_obj = hashlib.sha1() @@ -70,15 +91,7 @@ def digest(file: str, hash_type=HashType.SHA1) -> str: hash_obj = hashlib.md5() else: raise Exception("Error: Unknown hash type for digesting.") - - with open(file, "rb") as f: - while True: - data = f.read(BUF_SIZE) - if not data: - break - hash_obj.update(data) - - return hash_obj.hexdigest() + return hash_obj def write_manifest(paths: List[str], root: str, product_key: str) -> Tuple[str, str]: diff --git a/tests/test_util.py b/tests/test_util.py index 35c9deff..7105491d 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. """ -from charon.utils.files import digest, read_sha1, HashType +from charon.utils.files import digest, digest_content, read_sha1, HashType import os import unittest @@ -29,6 +29,14 @@ def test_digest(self): digest(test_file, HashType.SHA256), ) + def test_digest_content(self): + test_content = "test common content" + self.assertEqual("8c7b70f25fb88bc6a0372f70f6805132e90e2029", digest_content(test_content)) + self.assertEqual( + "1a1c26da1f6830614ed0388bb30d9e849e05bba5de4031e2a2fa6b48032f5354", + digest_content(test_content, HashType.SHA256), + ) + def test_read_sha1(self): test_file = os.path.join(INPUTS, "commons-lang3.zip") # read the real sha1 hash