dimanche 3 mai 2015

How do i query SQlite using python and then compare the query result?

The aim of this code is to validate a password that is entered using the passwords and usernames stored in a database. The table contains username(nick) and password, passwords are encrypted using:

import hashlib
return hashlib.sha1(password.encode()).hexdigest()

My code at the moment consists of:

def check_login(db, usernick, password):
    """returns True if password matches stored"""
    cursor = db.cursor()
    pass1 = db.crypt(password)
    cursor.execute("SELECT password FROM users WHERE nick=?", (usernick,))
    passcheck = cursor.fetchone()
    if passcheck == pass1:
        return True
    else:
        return False

But i keep getting an assertion error when running a unit test:

File "C:\Users\cm_000\Desktop\Uni\Chamath.Mendis 43053327\level3_unit.py", line 29, in test_check_login
    self.assertTrue(users.check_login(self.db, nick, password), "Password check failed for nick %s" % nick)
AssertionError: False is not true : Password check failed for nick Bobalooba

When i use print to show what passcheck is retreiving it prints the correct encrypted pass word but inside of ('') tags (as a tuple i believe). But when i print cursor.fetchone() it says none. I'm unsure what is going wrong here, i encrypted the password being sent into the function so it can be correctly matched to that of the password stored in the DB. I assume passcheck would retrieve the password that corresponds to the usernick that is passed through the function.

Any and all help is much appreciated.

Aucun commentaire:

Enregistrer un commentaire