Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions charon/utils/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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]:
Expand Down
10 changes: 9 additions & 1 deletion tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down