我的应用程序可以成功地将密码存储在使用hashlib.md5加密的sqlite中,但是当我想要将输入与散列进行比较时,我确实遇到了一个问题。如有任何帮助,我们不胜感激!
def chkPass(self):
pas = self.password_entry.get()
try:
query = "SELECT system_password FROM 'system'"
cur.execute(query,)
records = cur.fetchone()
print(records)
if (hashlib.md5(pas.encode())) == records[0]:
messagebox.showinfo('Login was Successful', 'Welcome')
else:
messagebox.showerror('Error', 'The password is incorrect, please try again')
cur.close()
except:
messagebox.showwarning('Warning', 'Fatal Error!', icon = 'warning') 更新: def chkPass(self):
pas = self.password_entry.get()
try:
query = "SELECT system_password FROM 'system'"
cur.execute(query,)
records = cur.fetchone()
print(records)
if (hashlib.md5(pas.encode()).hexdigest()) == records[0]:
messagebox.showinfo('Login was Successful', 'Welcome')
else:
messagebox.showerror('Error', 'The password is incorrect, please try again')
print((hashlib.md5(pas.encode()).hexdigest()))
cur.close()
except:
messagebox.showwarning('Warning', 'Fatal Error! What the heck did you do?', icon = 'warning') 现在我有了通过和加密,但我仍然得到error.Maybe,加密和解密不是同一类型的?
SQLITE哈希-('<sha256 _hashlib.HASH object @ 0x000001E3E973C7F0>',) pass.input - 122f961db675f6a45b998594471a990b
我没有任何加密的经验。在sqlite中,传递存储如下:<sha256 _hashlib.HASH object @ 0x000001E3E973C7F0>可以吗?
下面是将pas写入sqlite的代码
def NewPwd(self):
pas = self.password_entry.get()
password = hashlib.md5()
password.update(pas.encode('utf-8').hexdigest())
if password != '':
try:
query = "UPDATE 'system' SET system_password =?"
cur.execute(query,(str(password), ))
con.commit()
print(password)
messagebox.showinfo('Success!', 'The new password has been set!', icon = 'info')
except Error as e:
print(e)
else:
messagebox.showwarning('Warning', 'Feilds are empty or password contains less than 5 characters', icon = 'warning') 发布于 2020-11-06 10:51:19
我找到了解决方案:
def chkPass(self):
def check_password(hashed_password, user_password):
password, salt = hashed_password.split(':')
return password == hashlib.sha256(salt.encode() + user_password.encode()).hexdigest()
pas = self.password_entry.get()
print('Guess pass',pas)
try:
query = "SELECT system_password FROM 'system'"
cur.execute(query,)
records = cur.fetchone()
old_pass = records[0]
print('DB pass ', old_pass)
if check_password(old_pass, pas):
messagebox.showinfo('Login was Successful', 'Welcome')
else:
messagebox.showerror('Error', 'The password is incorrect, please try again')
cur.close()
except:
messagebox.showwarning('Warning', 'Fatal Error! What the heck did you do?', icon = 'warning') https://stackoverflow.com/questions/64707345
复制相似问题