Python’da sqlite ile oluşturdum db’ye eklediğim password’un hashlenmesini istiyorum lakin olmuyor. hashlib kütüphanesini kullanarak app.py içerisinde yakaladığım password’u hashing için gerekli modüle gönderiyorum, oradan da bu gönderdiğim veriyi alıp hashleyip create_db fonksiyonuna gönderiyorum. Lakin saçma sapan hatalar alıyorum. Hatanın ne olduğunu bile anlamadığımdan sizlere sorunumu net bir şekilde söyleyemiyorum. ChatGPT’ye de o kadar çok sordum ki timeout yedim. Yapmak istediğim şeyden yola çıkarak kodlarımda neleri değiştirebileceğime dair yardımcı olursanız sevinirim.
app.py:
from flask import Flask, render_template, request
from users_db import create_db, password_hashing
app = Flask(__name__)
#Endpoint
@app.route('/register', methods = ['POST', 'GET'])
def register():
if request.method == 'POST':
#Here, it sends the data it captures with get to the create_db function.
username = request.form.get('username')
password = request.form.get('password')
password_hashing(password)
create_db(username)
else:
return render_template('register.html')
return render_template('Server Side Error')
if __name__ == '__main__':
app.run(debug=True)
users_create ve password_hashing:
import sqlite3
import hashlib
def password_hashing(password):
password = hashlib.sha256(password.encode()).hexdigest()
hashed_password = password
create_db(hashed_password)
#Create users
def create_db(username, hashed_password):
#Classic sqllite operations.
conn = sqlite3.connect('users.db')
cursor = conn.cursor()
create_table = """CREATE TABLE IF NOT EXISTS users (username, password)"""
#Question marks are placed to prevent sqli vulnerability.
input_users = """INSERT INTO users (username, password) VALUES (?,?)"""
cursor.execute(create_table)
#Here we add the username and password values that we have obtained from the register.html file to the db.
cursor.execute(input_users, (username, hashed_password))
conn.commit()