【问题标题】:How do i get the password from the SQL database without it quoting the output?如何在不引用输出的情况下从 SQL 数据库获取密码?
【发布时间】:2021-01-20 10:18:37
【问题描述】:

我现在的代码是

import mysql.connector import hashlib
 
cnx = mysql.connector.connect(user='root', password='!Joshua1',
                               host='127.0.0.1',
                               database='passwordhashtest')
 
cursor = cnx.cursor()
 
Password = input("Enter the password >>")       
hashpass = hashlib.md5(Password.encode('utf8')).hexdigest()
 
Email=input("number")
 
passwordcheck='''SELECT b FROM password2 WHERE a = %s AND b = %s'''

values=(Email,hashpass)
 
cursor.execute(passwordcheck,values) 
 
hashedpassindatabase=cursor.fetchone()
 
print(hashedpassindatabase)
 
if hashedpassindatabase==hashpass:
    print("Success!") else:
    print("error")`

我的输出如下:

('d1133275ee2118be63a577af759fc052',)

error

看到我的问题是引号和逗号! 我该如何删除它!?!?!??!

这似乎是不可能的,我尝试了所有我能想到的!

哈希密码存储为

d1133275ee2118be63a577af759fc052

如果我从 mysql 获取的数据不包括引号和逗号,那么事情将很容易得到验证,但事实并非如此。那是我不明白的!!!!!!!!!!救命!!!!!!!!!!!!

【问题讨论】:

标签: python mysql mysql-connector-python


【解决方案1】:

DB API2 compliantcursor.fetchone() 的返回值是一个序列(或None)。

在您的情况下,结果是一个包含单个项目的元组:

t = ('d1133275ee2118be63a577af759fc052',)

要访问此项目,请使用t[0]

或者坚持你的变量名:

hashedpassindatabase = ('d1133275ee2118be63a577af759fc052',)
actual_value = hashedpassindatabase[0]
print(actual_value)

输出:

d1133275ee2118be63a577af759fc052

【讨论】:

  • 非常感谢!!!!!!!!!此外,这种验证密码的方法不会成为隐私权问题吗?
  • 存储密码哈希然后比较哈希是可行的方法,而存储纯文本密码是不行的,所以你走在正确的轨道上。题外话:MD5是一个弱哈希,如果你想使用更强的加密,你可以查看passlib
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-28
  • 2013-02-28
  • 1970-01-01
  • 2016-08-14
  • 1970-01-01
相关资源
最近更新 更多