微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python-Hashlib解码

如何解决Python-Hashlib解码

我的应用程序可以成功将密码存储在具有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(s​​elf):

    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',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哈希-('<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') 

解决方法

我找到了解决方法:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。