|
| 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) |
0 commit comments