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