0

我的应用程序可以成功地将密码存储在使用 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(自我):

    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')  

现在我既有通过也有加密,但我仍然收到错误。也许加密和解密不是同一类型?

SQLITE HASH - ('<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') 
4

1 回答 1

0

我找到了解决方案:

    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') 
于 2020-11-06T02:51:19.050 回答