Search code examples
pythonformattingbcryptmysql-connector

Python, Bcrypt password checking with SQL-Conncector


I am using SQL-Connector and Bcrypt in my Python program, I have this password stored in a database:

$2b$12$rRkfWn03an8WxtfRSMNd2ebbPrhpbkRONzsAdCmM/goV9XobpOYk6

I would like to check if a user input is the same as the one in the SQL database, I am able to do this using the right Bcrypt function but my issue is, When it pulls that password from the database it pulls it in this format:

[('$2b$12$rRkfWn03an8WxtfRSMNd2ebbPrhpbkRONzsAdCmM/goV9XobpOYk6',)]

Obviously I cant check it because the brackets etc make them not match, They need to be identical to pass. I tried doing it by encrypting the input, Then checking if it is IN that string, But obviously each time you encrypt a password, Even if its the same password it gives a different hash so I have to use the Bcrypt function, Which only passes if they are identical... Does anyone know how I would retrieve the encrypted password from the database as just the password without the formatting either side?

TLDR: How would I retrieve a string from an SQL database as string as appose to [('string')]

Many Thanks!!!


Solution

  • first, usally you dont store passwords in a database. normally you store the hash code from the password.

    .

    (I suck at explaining but ill try my best)

    Hash works like this: you put in a string, and it converts is to a number. this number will always be the same if you put in the same string, but you cant get the string if you only know the hash code. Then, when you check the password, you create the hash code and check if it is the same as in the database. This way its way more secure.

    now, to your actual question. I dont know anything about SQL, but you can trim the string like this:

    password = "(['ThisIsAVeryGoodPassword'])"
    
    letters_to_trim_front = 3
    letters_to_trim_back = -3
    
    trimmed_password = password[letters_to_trim_front:letters_to_trim_back]
    
    print(trimmed_password)
    

    should return ThisIsAVeryGoodPassword