Код: Выделить всё
hash_value = hashlib.pbkdf2_hmac(
# TypeError: a bytes-like object is required, not 'str'
Код: Выделить всё
CREATE TABLE IF NOT EXISTS myschema.mytable
(
--Unrelated fields...
password_hash character varying(500) COLLATE pg_catalog."default" NOT NULL,
salt character varying(100) COLLATE pg_catalog."default" NOT NULL,
--More unrelated fields...
)
Код: Выделить всё
# Code above that is setting up a DB utility, configs, etc...
# Hash the password
salt = os.urandom(16)
iterations = 100000
hash_value = hashlib.pbkdf2_hmac(
'sha256',
password.encode('utf-8') + app_config['PEPPER'].encode('utf-8'),
salt,
iterations
)
password_hash = salt + hash_value
# Redacted extra fields
query = "INSERT INTO mytable (password_hash, salt) VALUES (%s, %s);"
params = (password_hash, salt)
# This kicks off the standard cursor execute, etc...
db.query(query, params)
Код: Выделить всё
# Code above that is setting up a DB utility, configs, etc...
query = "SELECT password_hash, salt FROM mytable WHERE email = %s;"
params = (email,)
users = db.query(query, params)
db.close()
# No user found
if not users:
return False
db_password_hash, db_salt = users[0]
iterations = 100000
hash_value = hashlib.pbkdf2_hmac( # This will be the spot that throws the exception as it expects bytes for
'sha256',
password.encode('utf-8') + app_config['PEPPER'].encode('utf-8'),
db_salt,
iterations
)
# Then commence validation logic, etc...
Спасибо!
Подробнее здесь: https://stackoverflow.com/questions/793 ... ect-vs-str