You can't do it like that!
You need to have a separate random salt for each password. Which means that you need to store the salt alongside the password.
As a result, you cannot filter on the
pw
column in your database. Instead, you need to load the salt and the hashed password for the supplied username, calculate the hash using the entered password and the stored salt, and then compare it to the hashed password from the database.
Secure Password Authentication Explained Simply[
^]
Salted Password Hashing - Doing it Right[
^]
Quote:
No, you're not to produce a separate salt for the password, confirmation and login verification. That would make the passwords different.
That's the whole point! You have a different salt value for each stored password. When you verify the entered password, you combine it with the salt value stored against that user's record to produce the hash, and then compare that hash to the stored hash.
The whole point of a salt is to make it much harder to find the original password, since an attacker can no longer rely on a "rainbow table" of precomputed hashes. It also makes it impossible for the attacker to see which accounts have the same password, since the random salt ensures that the stored hash will be different even if the plain-text password is the same.
EDIT 2:
I don't "do" Python, but the basic approach is as follows:
username = self.user.text()
password = self.pw.text()
conn = pyodbc.connect('Driver={SQL Server}; Server=mine1;database=logistics; Trusted_Connection=yes;')
print("connected to database")
result = conn.execute("SELECT pw, salt FROM employees WHERE userID = ?", username)
row = result.fetchone()
if row:
print("user found")
storedhash = row[0]
salt = row[1]
hashpass = str(hashlib.pbkdf2_hmac("sha256", password.encode(), salt, 10000))
if hashpass == storedhash:
msgBox.setText("LOGIN SUCCESSFUL")
msgBox.setWindowTitle("MESSAGE FROM DB")
msgBox.exec_()
self.toggle_logistics()
else:
print("Invalid credentials")
else:
print("Invalid credentials")
Obviously, you'll need a column in your database to store the salt value, and you'll need to update your user registration code to store the generated salt value in that column.
And avoid using
global
variables.