Skip to content

Commit 69dc2bf

Browse files
committed
Add function to automaticaly validate checksum string
1 parent d00d627 commit 69dc2bf

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

pyntegrity/config.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
SHA256_LENGTH = 64
2121
MD5_LENGTH = 32
2222

23-
SHA256_REX = rf"^[A-Fa-f0-9]{SHA256_LENGTH}$"
24-
MD5_REX = rf"^[a-fA-F\d]{MD5_LENGTH}$"
23+
SHA256_REX = r"^[A-Fa-f\d]{64}$"
24+
MD5_REX = r"^[a-fA-F\d]{32}$"
2525

2626
SUPPORTED_HASH_ALGOS = {
2727
"sha256": {"REX": SHA256_REX, "LENGTH": SHA256_LENGTH},

pyntegrity/core.py

+18
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
You should have received a copy of the GNU General Public License
1616
along with this program. If not, see <https://www.gnu.org/licenses/>.
1717
"""
18+
import re
19+
1820
from .exceptions import HashStrNotValidException
1921
from .exceptions import HashAlgorithmNotSupportedException
2022

@@ -38,3 +40,19 @@ def detect_hash_algo(hash_str: str):
3840
raise HashAlgorithmNotSupportedException(
3941
detected_length=hash_len, hash_str=hash_str
4042
)
43+
44+
45+
def validate_hash_str(hash_str: str):
46+
"""
47+
Checks if the str is a valid checksum in the detected algorithm
48+
49+
:param hash_str:the hash string
50+
:return: True if valid
51+
:raises HashStrNotValidException: raised if the hash str isn't valid
52+
"""
53+
hash_name = detect_hash_algo(hash_str)
54+
pattern = re.compile(SUPPORTED_HASH_ALGOS[hash_name]["REX"])
55+
if pattern.match(hash_str):
56+
return True
57+
else:
58+
raise HashStrNotValidException(detected_hash_algo=hash_name, hash_str=hash_str)

tests/test_core.py

+28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
from unittest import TestCase
2+
23
from pyntegrity.core import detect_hash_algo
4+
from pyntegrity.core import validate_hash_str
5+
6+
from pyntegrity.exceptions import HashStrNotValidException
37
from pyntegrity.exceptions import HashAlgorithmNotSupportedException
48

59

@@ -16,3 +20,27 @@ def test_detect_hash_algo_nok(self):
1620
invalid_hash_str = "6545ed"
1721
with self.assertRaises(HashAlgorithmNotSupportedException):
1822
detect_hash_algo(invalid_hash_str)
23+
24+
25+
class TestValidateHashStr(TestCase):
26+
def test_validate_hash_str_md5_ok(self):
27+
valid_md5 = "098f6bcd4621d373cade4e832627b4f6"
28+
self.assertTrue(validate_hash_str(valid_md5))
29+
30+
def test_validate_hash_str_md5_nok(self):
31+
invalid_md5 = "098f6bcd4621d373xade4e832627b4f6"
32+
with self.assertRaises(HashStrNotValidException):
33+
validate_hash_str(invalid_md5)
34+
35+
def test_validate_hash_str_sha256_ok(self):
36+
valid_sha256 = (
37+
"9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"
38+
)
39+
self.assertTrue(validate_hash_str(valid_sha256))
40+
41+
def test_validate_hash_str_sha256_nok(self):
42+
invalid_sha256 = (
43+
"9f86d081884x7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"
44+
)
45+
with self.assertRaises(HashStrNotValidException):
46+
validate_hash_str(invalid_sha256)

0 commit comments

Comments
 (0)