2

My goal of this code is to be able to type in the username and password for the account to be registered and have that username & password be added to the database as hashtrings of the string they typed in the input field for username and password.

What I expected to happen is that when I typed in my username and password is that the values would convert to hash values using the hashlib python package using the md5 method, then add that hash value to the database. What happened is that I received an error that states as follow: >mysql.connector.errors.ProgrammingError: Failed processing format-parameters; Python 'hash' cannot be converted to a MySQL type.

i have tried to do a few things to fix this problem. I've tried to do a direct data type conversion on that hash value, here is an example.

example_string = "Example"
hash_value = hashlib.md5(example_string.encode())
converted_hash = string(hash_value)

This resulted in the object being added into the database, and not the actual hash value like I wanted.

i also tried to iterate through the hash & and add each character as a string in to another string variable. here is an example

example_string = "Example"
hash_value = hashlib.md5(example_string.encode())
hash_string = ""
for char in hash_value:
    hash_string += string(char)

this resulted in me getting an error that I can not iterate through python hash values.

This is my current code and what I am stuck with.

def register():
    username = input("Choose a username?")
    password = input("Choose a password?")

    hash_user = hashlib.md5(username.encode())
    hash_pass = hashlib.md5(password.encode())

    sql = "INSERT INTO account " \
          "   (username, password) " \
          "   VALUES (%s, %s)"

    val = (hash_user, hash_pass)
    cursor.execute(sql, val)

    main_db.commit()

    return "Your account has been created successfully!"

and just for future reference. the SQL attribute type for username & password is VARCHAR(255)

1 Answer 1

1

This is the answer

def register():
    username = input("Choose a username?")
    password = input("Choose a password?")

    hash_user = hashlib.md5(username.encode("utf-8")).hexdigest()
    hash_pass = hashlib.md5(password.encode("utf-8")).hexdigest()

    sql = "INSERT INTO account " \
          "   (username, password) " \
          "   VALUES (%s, %s)"

    val = (hash_user, hash_pass)
    cursor.execute(sql, val)

    main_db.commit()

    return "Your account has been created successfully!"
Sign up to request clarification or add additional context in comments.

2 Comments

Maybe you can add a little prose about what you changed and why
@Artjom B. - It is necessary to convert into hex the calculated md5() value, this is done with hexdigest() method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.