为什么Bcrypt会因MySQL不断失败,如何获取哈希?

时间:2019-01-23 21:14:54

标签: python flask bcrypt

我试图通过URL将密码从React Native传入Flask,然后将其插入MySQL。然后,我从数据库检索它并针对密码进行测试。我已经尝试过制作哈希和密码字符串以及编码的utf8的每种组合,但我只会出错,请让我知道出了什么问题。

散列并插入数据库的我的get password方法

@app.route('/signup', methods=['GET'])
def signup():
    password = str(request.args.get('password')).encode('utf8')
    username = str(request.args.get('username')).encode('utf8')
        c.execute("""INSERT INTO profiles 
                      (username, password
                           )
                      VALUES 
                      (%s, %s)""", ## gender true==male && false==female
                      (username, hashed
                       )
                    )
        con.commit()

我在mysql中看到这样的一行(varchar256)

+--------------------------------------------------------------+
| password                                                     |
+--------------------------------------------------------------+
| $2b$12$oD607B1ej5qXM/mFcPVdueX8R5zeWNfL39d2oNPuoM3KdUfP8McvO |
+--------------------------------------------------------------+

我的找回密码方法

@app.route('/check_hash', methods=['GET'])
def check_hash():

    try:
        username = request.args.get('username')   
        password = str(request.args.get('password')).encode("utf8")

        c.execute('select password from profiles where userID = ' + '"'          + str(username) +'"')

        hashed = (c.fetchone()[0]).encode('utf8')

        if bcrypt.hashpw(password, hashed) == (hashed):          
            return json.dumps('success')
        else:        
            return json.dumps('no')

    except Exception as e:
        return str(e)

每次在期望为true时失败,都为false

0 个答案:

没有答案