【问题标题】:Python - Hashlib decodePython - Hashlib 解码
【发布时间】:2020-11-06 00:45:22
【问题描述】:

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

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

SQLITE 哈希 -('<sha256 _hashlib.HASH object @ 0x000001E3E973C7F0>',) pass.input - 122f961db675f6a45b998594471a990b

我对加密没有任何经验。在 sqlite 中,pass 是这样存储的:<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') 

【问题讨论】:

  • 具体是什么问题?
  • 我们需要pasrecord[0],可能还有用来写db 的代码。我认为您想保存并稍后比较hashlib.md5(pas.encode()).digest().hexdigest(),因为HASH 对象本身不会进行比较。例如hashlib.md5(b"foobar") != hashlib.md5(b"foobar"),但他们的摘要可以。
  • 这里以stackoverflow为例,你可以完全摆脱prompt、gui和db。只需分配一个字符串,例如pas = "foobar",然后尝试加密和比较,看看有什么用。这将是在这里发布的好代码。
  • 您现在在验证步骤中有十六进制摘要,但您还需要修复编写数据库的代码。您保存了 HASH 对象的字符串表示形式,而不是它的十六进制摘要。
  • tdelaney 我只是在提示下尝试过,但我得到了两个不同的值。

标签: python sqlite hashlib


【解决方案1】:

我找到了解决方案:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-15
    • 2013-04-04
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-01
    相关资源
    最近更新 更多