0

I am working on this application and I have set up a registration and login page. Everything works perfectly fine, the only issue is with the password validation. I am using SHA256 and every time I try logging I get this error : ValueError: not a valid sha256_crypt hash here is a piece of def register

def register():
    if request.method=="POST":
        name = request.form.get("name")
        username = request.form.get("username")
        password = request.form.get("password")
        confirm = request.form.get("confirm")
        secure_password = sha256_crypt.encrypt(str(password))
and here is a login

def login():
    if request.method == "POST":
        username = request.form.get("username")
        password = request.form.get("password")
      
        usernamedata = db.execute("SELECT username FROM users WHERE username=:username", {"username":username}).fetchone()
        passwordata = db.execute("SELECT password FROM users WHERE username=:username", {"username":username}).fetchone()

        if usernamedata is None:
            flash("No username", "danger")
            return render_template("/login.html")
        else:
              for passwor_data in passwordata:
                if sha256_crypt.verify(password,passwor_data):
                    flash("You are now logged in","success")
                    return redirect("/index.html")
Gerardo Zinno
  • 1,518
  • 1
  • 13
  • 35

1 Answers1

0

You are iterating over each character of the hash in for passwor_data in passwordata:, so it throws this error, complaining that the second argument is not a valid sha256 hash.

It should be something like :

if sha256_crypt.verify(password, passwordata):
    flash("You are now logged in","success")
    return redirect("/index.html")
  • password is the clear text password received from user input
  • passwordata is sha256 hash

Short example :

from passlib.hash import sha256_crypt

password = "test"
passwordata = sha256_crypt.encrypt(password)

if sha256_crypt.verify(password, passwordata):
    print("OK")
Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159