49 The function to hash the password for the security
51 password (str): The entered password
54 str: The hashed password
57 TypeError: if the provided password does not match the expected type.
59 title =
"_hash_password"
60 self.
disp.log_debug(
"Enter hash password", f
"{title}")
61 if not isinstance(password, (str, bytes)):
62 raise TypeError(
"The password is neither bytes nor a string")
63 if isinstance(password, str):
64 password = bytes(password, encoding=
"utf-8")
65 self.
disp.log_debug(
"Start register endpoint", f
"{title}")
67 safe_password = bcrypt.hashpw(password, salt)
68 return safe_password.decode(
"utf-8")
70 def check_password(self, password: Union[str, bytes], password_hash: bytes) -> bool:
72 The function to check the entered password with the hashed password
74 password (str): The entered password
75 password_hash (bytes): The hashed password
78 bool: True if it's the same, False if not
81 TypeError: if the provided password does not match the expected type.
83 msg = f
"password = {type(password)}, "
84 msg += f
"password_hash = {type(password_hash)}"
85 self.
disp.log_debug(msg,
"check_password")
86 if not isinstance(password, (str, bytes)):
87 raise TypeError(
"The password is neither bytes nor a string")
88 if not isinstance(password_hash, (str, bytes)):
89 raise TypeError(
"The password_hash is neither bytes nor a string")
90 if isinstance(password, str):
91 password = password.encode(
"utf-8")
92 if isinstance(password_hash, str):
93 password_hash = password_hash.encode(
"utf-8")
94 msg = f
"password = {type(password)}, password_hash = "
95 msg += f
"{type(password_hash)}"
96 self.
disp.log_debug(msg,
"check_password")
97 return bcrypt.checkpw(password, password_hash)