I have a postgresql db which contains following data: enter image description here credentials are bytes stored in table with usage of psycopg2.Binary.
The issue is that the SELECT query run via Python returns strings (I need bytes to verify the credentials)
def login_to_account(self):
query = '''SELECT * FROM users WHERE login = %s'''
try:
result = db_ops.execute_queries(query, (self.username_input.text(),))
is_correct_password(salt=result[2], pw_hash=result[3], password=self.password_input.text())
except:
QMessageBox.about(self, "Błąd logowania", "<b><p align='center'>Login lub hasło niepoprawne!<br>")
How can I convert it back? If this is a wrong way of storing and verifying credentials please let me know, Im new to this kind of stuff.
This is my salt+hash function that I use to encrypt and decrypt the data
import hmac
def hash_new_password(password: str) -> Tuple[bytes, bytes]:
"""
Hash the provided password with a randomly-generated salt and return the
salt and hash to store in the database.
"""
salt = os.urandom(16)
pw_hash = hashlib.pbkdf2_hmac('sha256', password.encode(), salt, 100000)
return salt, pw_hash
def is_correct_password(salt: bytes, pw_hash: bytes, password: str) -> bool:
"""
Given a previously-stored salt and hash, and a password provided by a user
trying to log in, check whether the password is correct.
"""
return hmac.compare_digest(
pw_hash,
hashlib.pbkdf2_hmac('sha256', password.encode(), salt, 100000)
)