I currently have a Flask app that registers a Username and password to a MySQL database. My problem is that using Bcrypt
to hash passwords and then save to them to the database, the outputted hash gets truncated by MySQL. My function looks like this:
class PasswordResource(Resource):
@app.route('/password', methods=['GET'])
@jwt_required
def sendPassword():
data = get_jwt_identity()
hashed_password = bcrypt.hashpw(data['user_password'].encode('utf8'), bcrypt.gensalt())
print(hashed_password)
return mysqldb.addPassword("{}".format(str(data['user_name'])),"{}".format(hashed_password))
and connects to the database:
def addPassword(Username,Userpassword):
try:
cursorObject = connection.cursor()
cursorObject.execute("call sp_createPassword('{}'".format(Username)+",'{}')".format(Userpassword))
connection.commit()
connection.close()
finally:
return 'User added succesfully!'
However, the hash gets truncated by MySql even though I have tried various VARCHAR()
for the password table. This is an error i get in console Warning: (1265, u"Data truncated for column 'p_password' at row 1") self._do_get_result()
. My password column currently is configured to VARCHAR(255), Not Null
. How can I fix this error and avoid having the hashes getting truncated?
UPDATE
This is would be the schema for my password table:
+---------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tbl_passwords | CREATE TABLE `tbl_passwords` (
`user_name` varchar(255) DEFAULT NULL,
`user_password` varchar(255) NOT NULL,
`user_number` bigint(20) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`user_number`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+---------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+