Terarea  2
The automation project
Loading...
Searching...
No Matches
password_handling.py
Go to the documentation of this file.
1"""
2 File containing the class in charge of handling password for the server
3"""
4import bcrypt
5from display_tty import Disp, TOML_CONF, FILE_DESCRIPTOR, SAVE_TO_FILE, FILE_NAME
6
7
9 """__summary__
10 """
11
12 def __init__(self, error: int = 84, success: int = 0, debug: bool = False) -> None:
13 self.debug: bool = debug
14 self.success: int = success
15 self.error: int = error
16 self.salt_rounds = 10
17 # ------------------------ The logging function ------------------------
18 self.disp: Disp = Disp(
19 TOML_CONF,
20 FILE_DESCRIPTOR,
21 SAVE_TO_FILE,
22 FILE_NAME,
23 debug=self.debug,
24 logger=self.__class__.__name__
25 )
26
27 def hash_password(self, password: str) -> str:
28 """
29 The function to hash the password for the security
30 Args:
31 password (str): The entered password
32
33 Returns:
34 str: The hashed password
35 """
36 title = "_hash_password"
37 self.disp.log_debug("Enter hash password", f"{title}")
38 if isinstance(password, bytes) is False:
39 password = bytes(password, encoding="utf-8")
40 self.disp.log_debug("Start register endpoint", f"{title}")
41 salt = bcrypt.gensalt(rounds=self.salt_rounds)
42 safe_password = bcrypt.hashpw(password, salt)
43 return safe_password.decode("utf-8")
44
45 def check_password(self, password: str, password_hash: bytes) -> bool:
46 """
47 The function to check the entered password with the hashed password
48 Args:
49 password (str): The entered password
50 password_hash (bytes): The hashed password
51
52 Returns:
53 bool: True if it's the same, False if not
54 """
55 msg = f"password = {type(password)}, "
56 msg += f"password_hash = {type(password_hash)}"
57 self.disp.log_debug(msg, "check_password")
58 if isinstance(password, bytes) is False:
59 password = password.encode("utf-8")
60 if isinstance(password_hash, bytes) is False:
61 password_hash = password_hash.encode("utf-8")
62 msg = f"password = {type(password)}, password_hash = "
63 msg += f"{type(password_hash)}"
64 self.disp.log_debug(msg, "check_password")
65 return bcrypt.checkpw(password, password_hash)
None __init__(self, int error=84, int success=0, bool debug=False)
bool check_password(self, str password, bytes password_hash)