File tree 3 files changed +48
-2
lines changed
3 files changed +48
-2
lines changed Original file line number Diff line number Diff line change 20
20
SHA256_LENGTH = 64
21
21
MD5_LENGTH = 32
22
22
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 }$"
25
25
26
26
SUPPORTED_HASH_ALGOS = {
27
27
"sha256" : {"REX" : SHA256_REX , "LENGTH" : SHA256_LENGTH },
Original file line number Diff line number Diff line change 15
15
You should have received a copy of the GNU General Public License
16
16
along with this program. If not, see <https://www.gnu.org/licenses/>.
17
17
"""
18
+ import re
19
+
18
20
from .exceptions import HashStrNotValidException
19
21
from .exceptions import HashAlgorithmNotSupportedException
20
22
@@ -38,3 +40,19 @@ def detect_hash_algo(hash_str: str):
38
40
raise HashAlgorithmNotSupportedException (
39
41
detected_length = hash_len , hash_str = hash_str
40
42
)
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 )
Original file line number Diff line number Diff line change 1
1
from unittest import TestCase
2
+
2
3
from pyntegrity .core import detect_hash_algo
4
+ from pyntegrity .core import validate_hash_str
5
+
6
+ from pyntegrity .exceptions import HashStrNotValidException
3
7
from pyntegrity .exceptions import HashAlgorithmNotSupportedException
4
8
5
9
@@ -16,3 +20,27 @@ def test_detect_hash_algo_nok(self):
16
20
invalid_hash_str = "6545ed"
17
21
with self .assertRaises (HashAlgorithmNotSupportedException ):
18
22
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 )
You can’t perform that action at this time.
0 commit comments