Skip to content

Commit 70359e5

Browse files
committed
Init exceptions classes for the planned functionnalities
1 parent 7fa2073 commit 70359e5

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

pyntegrity/config.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Hash settings
2+
3+
SHA256_LENGTH = 64
4+
MD5_LENGTH = 32
5+
6+
SUPPORTED_HASH_ALGOS = {"SHA256": SHA256_LENGTH, "MD5": MD5_LENGTH}

pyntegrity/exceptions.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 .config import SUPPORTED_HASH_ALGOS
19+
20+
21+
class HashAlgorithmNotSupportedException(Exception):
22+
def __init__(self, detected_length: int, hash_str: str):
23+
"""
24+
Exception raised when the hash length does not match
25+
any supported Hash algorithm;
26+
27+
:param detected_length: the length of the checked hash string
28+
:param hash_str: the hash string
29+
"""
30+
self.detected_length = detected_length
31+
self.hash_str = hash_str
32+
self.message = (
33+
"[!] The hash string length does not "
34+
"match any supported hash algorithm: ["
35+
)
36+
for hash_name, length in SUPPORTED_HASH_ALGOS.items():
37+
self.message += f" {hash_name}: {length} chars, "
38+
self.message += "]"
39+
40+
super().__init__(self.message)
41+
42+
43+
class HashStrNotValidException(Exception):
44+
def __init__(self, detected_hash_algo: str, hash_str: str):
45+
"""
46+
Exception raised when hash string isn't valid
47+
for the detected hash algorithm;
48+
49+
:param detected_hash_algo: the name of the detected algo
50+
:param hash_str: the hash string
51+
"""
52+
self.detected_hash_algo = detected_hash_algo
53+
self.hash_str = hash_str
54+
self.message = (
55+
f"[!] The hash string doesn't "
56+
f'seem valid for the detected algorithm"{detected_hash_algo}"'
57+
)
58+
super().__init__(self.message)

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
black~=22.1

0 commit comments

Comments
 (0)