Skip to content

Commit 0affc9c

Browse files
committed
Add file checksum functionality
1 parent 9360d52 commit 0affc9c

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

pyntegrity/core.py

+16-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import os
1919
import re
2020
import enum
21+
import hashlib
2122
from pathlib import Path
2223

2324
from .exceptions import FileNotFoundException
@@ -45,22 +46,24 @@ def detect_hash_algo(checksum_str: str):
4546
hash_len = len(checksum_str)
4647
for name, infos in SUPPORTED_HASH_ALGOS.items():
4748
if infos["LENGTH"] == hash_len:
48-
return name
49+
if validate_checksum_str(checksum_str=checksum_str, hash_algo=name):
50+
return name
4951
else:
5052
raise HashAlgorithmNotSupportedException(
5153
detected_length=hash_len, checksum_str=checksum_str
5254
)
5355

5456

55-
def validate_checksum_str(checksum_str: str):
57+
def validate_checksum_str(checksum_str: str, hash_algo: str):
5658
"""
5759
Checks if the str is a valid checksum in the detected algorithm
5860
5961
:param checksum_str:the hash string
62+
:param hash_algo: the hash algorithm
6063
:return: True if valid
6164
:raises HashStrNotValidException: raised if the hash str isn't valid
6265
"""
63-
hash_name = detect_hash_algo(checksum_str)
66+
hash_name = hash_algo
6467
pattern = re.compile(SUPPORTED_HASH_ALGOS[hash_name]["REX"])
6568
if pattern.match(checksum_str):
6669
return True
@@ -92,11 +95,18 @@ def get_file_path_from_str(str_path: str):
9295

9396

9497
class IntegrityValidator:
95-
def __init__(self, str_path: str):
98+
def __init__(self, str_path: str, checksum_str: str):
99+
self.hash_algo = detect_hash_algo(checksum_str=checksum_str)
100+
self.checksum_str = checksum_str
96101
self.file = get_file_path_from_str(str_path)
102+
self.hashlib_obj = hashlib.new(self.hash_algo)
97103

98104
def validate_file_integrity(self):
99-
pass
105+
file_checksum = self.get_file_checksum()
106+
return file_checksum == self.checksum_str
100107

101108
def get_file_checksum(self):
102-
pass
109+
with self.file.open() as file_to_validate:
110+
file_content = file_to_validate.read()
111+
self.hashlib_obj.update(file_content)
112+
return self.hashlib_obj.hexdigest()

tests/test_core.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
class TestDetectHashAlgo(TestCase):
1515
def test_detect_hash_algo_ok(self):
16-
str32_len = "xzazyokqvhqzjbeyxpldfntsjiaumxan"
17-
str64_len = "cmolcmtdtxffmfqnjgadqteqnnmacnhysxwpdanwtqzibkysmawoqxdiippzxpum"
16+
str32_len = "098f6bcd4621d373cade4e832627b4f6"
17+
str64_len = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"
1818
hash_name = detect_hash_algo(str32_len)
1919
self.assertEqual(hash_name, "md5")
2020
hash_name = detect_hash_algo(str64_len)
@@ -29,25 +29,25 @@ def test_detect_hash_algo_nok(self):
2929
class TestValidateHashStr(TestCase):
3030
def test_validate_checksum_str_md5_ok(self):
3131
valid_md5 = "098f6bcd4621d373cade4e832627b4f6"
32-
self.assertTrue(validate_checksum_str(valid_md5))
32+
self.assertTrue(validate_checksum_str(valid_md5, "md5"))
3333

3434
def test_validate_checksum_str_md5_nok(self):
3535
invalid_md5 = "098f6bcd4621d373xade4e832627b4f6"
3636
with self.assertRaises(HashStrNotValidException):
37-
validate_checksum_str(invalid_md5)
37+
validate_checksum_str(invalid_md5, "md5")
3838

3939
def test_validate_checksum_str_sha256_ok(self):
4040
valid_sha256 = (
4141
"9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"
4242
)
43-
self.assertTrue(validate_checksum_str(valid_sha256))
43+
self.assertTrue(validate_checksum_str(valid_sha256, "sha256"))
4444

4545
def test_validate_checksum_str_sha256_nok(self):
4646
invalid_sha256 = (
4747
"9f86d081884x7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"
4848
)
4949
with self.assertRaises(HashStrNotValidException):
50-
validate_checksum_str(invalid_sha256)
50+
validate_checksum_str(invalid_sha256, "sha256")
5151

5252

5353
class TestGetFileFromStr(TestCase):

0 commit comments

Comments
 (0)