Skip to content

Commit d2d587a

Browse files
committed
Add automatic hash detection function
1 parent a21b61b commit d2d587a

File tree

6 files changed

+88
-2
lines changed

6 files changed

+88
-2
lines changed

.github/workflows/python-package.yml

+3
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,6 @@ jobs:
3232
run: |
3333
black --check .
3434
35+
- name: Unit tests
36+
run: |
37+
pytest .

pyntegrity/config.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
1+
"""
2+
Pyntegrity is Python package that helps checking a file integrity.
3+
Copyright (C) 2022 Salah OSFOR
4+
5+
This program is free software: you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation, either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
"""
118
# Hash settings
219

320
SHA256_LENGTH = 64
421
MD5_LENGTH = 32
522

6-
SUPPORTED_HASH_ALGOS = {"SHA256": SHA256_LENGTH, "MD5": MD5_LENGTH}
23+
SHA256_REX = rf"^[A-Fa-f0-9]{SHA256_LENGTH}$"
24+
MD5_REX = rf"^[a-fA-F\d]{MD5_LENGTH}$"
25+
26+
SUPPORTED_HASH_ALGOS = {
27+
"sha256": {"REX": SHA256_REX, "LENGTH": SHA256_LENGTH},
28+
"md5": {"REX": MD5_REX, "LENGTH": MD5_LENGTH},
29+
}

pyntegrity/core.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
Pyntegrity is Python package that helps checking a file integrity.
3+
Copyright (C) 2022 Salah OSFOR
4+
5+
This program is free software: you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation, either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
"""
18+
from .exceptions import HashStrNotValidException
19+
from .exceptions import HashAlgorithmNotSupportedException
20+
21+
from .config import SUPPORTED_HASH_ALGOS
22+
23+
24+
def detect_hash_algo(hash_str: str):
25+
"""
26+
Detects hash algorithm based on its length.
27+
28+
:param hash_str: the hash string
29+
:return: the name of the hash algorithm
30+
:raises HashAlgorithmNotSupportedException: raised if the hash
31+
length isn't valid for any supported algorithm
32+
"""
33+
hash_len = len(hash_str)
34+
for name, infos in SUPPORTED_HASH_ALGOS.items():
35+
if infos["LENGTH"] == hash_len:
36+
return name
37+
else:
38+
raise HashAlgorithmNotSupportedException(
39+
detected_length=hash_len, hash_str=hash_str
40+
)

pyntegrity/exceptions.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ def __init__(self, detected_length: int, hash_str: str):
3333
"[!] The hash string length does not "
3434
"match any supported hash algorithm: ["
3535
)
36-
for hash_name, length in SUPPORTED_HASH_ALGOS.items():
36+
for hash_name, infos in SUPPORTED_HASH_ALGOS.items():
37+
length = infos["LENGTH"]
3738
self.message += f" {hash_name}: {length} chars, "
3839
self.message += "]"
3940

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
black~=22.1
2+
pytest~=7.0

tests/test_core.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from unittest import TestCase
2+
from pyntegrity.core import detect_hash_algo
3+
from pyntegrity.exceptions import HashAlgorithmNotSupportedException
4+
5+
6+
class TestDetectHashAlgo(TestCase):
7+
def test_detect_hash_algo_ok(self):
8+
str32_len = "xzazyokqvhqzjbeyxpldfntsjiaumxan"
9+
str64_len = "cmolcmtdtxffmfqnjgadqteqnnmacnhysxwpdanwtqzibkysmawoqxdiippzxpum"
10+
hash_name = detect_hash_algo(str32_len)
11+
self.assertEqual(hash_name, "md5")
12+
hash_name = detect_hash_algo(str64_len)
13+
self.assertEqual(hash_name, "sha256")
14+
15+
def test_detect_hash_algo_nok(self):
16+
invalid_hash_str = "6545ed"
17+
with self.assertRaises(HashAlgorithmNotSupportedException):
18+
detect_hash_algo(invalid_hash_str)

0 commit comments

Comments
 (0)