merged functions for master password hashing

This commit is contained in:
StNicolay 2022-10-29 19:35:54 +03:00
parent a5d93f0d5c
commit baef759929
2 changed files with 22 additions and 13 deletions

View File

@ -31,9 +31,7 @@ def add_record(
master_salt, hash_pass = master_password_from_db master_salt, hash_pass = master_password_from_db
if ( if (
cryptography.master_pass.encrypt_master_pass_known_salt( cryptography.master_pass.encrypt_master_pass(master_password, master_salt)
master_password, master_salt
)
!= hash_pass != hash_pass
): ):
return _send_tmp_message(bot, mes.chat.id, "Не подходит главный пароль") return _send_tmp_message(bot, mes.chat.id, "Не подходит главный пароль")
@ -107,10 +105,7 @@ def get_account(
if data[1] not in database.get.get_accounts(engine, mes.from_user.id): if data[1] not in database.get.get_accounts(engine, mes.from_user.id):
return _send_tmp_message(bot, mes.chat.id, "Нет такого аккаунта") return _send_tmp_message(bot, mes.chat.id, "Нет такого аккаунта")
if ( if cryptography.master_pass.encrypt_master_pass(data[2], master_salt) != hash_pass:
cryptography.master_pass.encrypt_master_pass_known_salt(data[2], master_salt)
!= hash_pass
):
return _send_tmp_message(bot, mes.chat.id, "Не подходит главный пароль") return _send_tmp_message(bot, mes.chat.id, "Не подходит главный пароль")
salt, enc_login, enc_pass = database.get.get_account_info( salt, enc_login, enc_pass = database.get.get_account_info(

View File

@ -1,12 +1,26 @@
from typing import overload
import bcrypt import bcrypt
@overload
def encrypt_master_pass(passwd: str, salt: bytes) -> bytes:
...
@overload
def encrypt_master_pass(passwd: str) -> tuple[bytes, bytes]: def encrypt_master_pass(passwd: str) -> tuple[bytes, bytes]:
...
def encrypt_master_pass(
passwd: str, salt: bytes | None = None
) -> tuple[bytes, bytes] | bytes:
"""Hashes master password and return tuple of hashed password and salt""" """Hashes master password and return tuple of hashed password and salt"""
if salt is None:
salt = bcrypt.gensalt() salt = bcrypt.gensalt()
gened_salt = True
else:
gened_salt = False
hashed = bcrypt.hashpw(passwd.encode("utf-8"), salt) hashed = bcrypt.hashpw(passwd.encode("utf-8"), salt)
return (hashed, salt) return (hashed, salt) if gened_salt else hashed
def encrypt_master_pass_known_salt(passwd: str, salt: bytes) -> bytes:
return bcrypt.hashpw(passwd.encode("utf-8"), salt)