password hasheds

This commit is contained in:
starnakin 2023-02-11 16:06:36 +01:00
parent 8fe3dccfd2
commit 39574a2c06
10 changed files with 48 additions and 29 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1 +1 @@
{"users": {"1": {"camille@chauvet.pro": "Fgzf4BY6R8oBfoz6VrHziwxjZiz4dB2cU7FcXP5kh"}}} {"users": {"1": {"camille@chauvet.pro": "b'$2b$12$AMuNu9CU/lUyaQjDmyWypeVg8beyRA795lrldMGAmHMXaeyfNnke.'"}}}

View File

@ -1,4 +1,5 @@
from tinydb import TinyDB, Query from tinydb import TinyDB, Query
import hasher
db = TinyDB("./database.json") db = TinyDB("./database.json")
users = db.table("users"); users = db.table("users");
@ -15,7 +16,12 @@ def user_exist(email: str):
return (get_user_by_email(email) != None) return (get_user_by_email(email) != None)
def add_user(email: str, password: str): def add_user(email: str, password: str):
users.insert({email: password}); password_hashed = hasher.hash_text(password)
users.insert({email: str(password_hashed)});
def check_password(email: str, password: str): def check_password(email: str, password: str):
return (get_user_by_email(email).get(email) == password) password_hashed = get_user_by_email(email).get(email)
password_hashed = bytes(password_hashed[2:-1], "utf-8")
return (hasher.is_same(password, password_hashed))
resets = db.table("resets")

26
hash.py
View File

@ -1,26 +0,0 @@
import bcrypt
# Declaring our password
password = b'GeekPassword'
# Adding the salt to password
salt = bcrypt.gensalt()
# Hashing the password
hashed = bcrypt.hashpw(password, salt)
print(salt)
print(type(hashed))
salt = hashed[:29]
print(salt)
print(password == bcrypt.hashpw(password, salt))
print(password == bcrypt.hashpw(password, salt))
print(password == bcrypt.hashpw(password, salt))
print(password == bcrypt.hashpw(password, salt))
print(password == bcrypt.hashpw(password, salt))
print(password == bcrypt.hashpw(password, salt))
print(password == bcrypt.hashpw(password, salt))
print(password == bcrypt.hashpw(password, salt))
print(password == bcrypt.hashpw(password, salt))
print(password == bcrypt.hashpw(password, salt))

9
hasher.py Normal file
View File

@ -0,0 +1,9 @@
import bcrypt
def hash_text(text:str) -> bytes:
text = bytes(text, "utf-8")
return (bcrypt.hashpw(text, bcrypt.gensalt()))
def is_same(text:str, hashed: bytes) -> bool:
text = text.encode("utf-8")
return (bcrypt.checkpw(text, hashed))

26
mail.py Normal file
View File

@ -0,0 +1,26 @@
import ssl
import smtplib
from email.message import EmailMessage
config = {
"server": "ssl0.ovh.net",
"port": 465,
"email": "auto@chauvet.pro",
"password": "#FL7Sf*9hZMkund24$a@46ny7Dx",
"display_name": "no-reply@chauvet.pro"
}
def send_mail(mail_add:str, subject:str, mail_content:str):
email = EmailMessage()
email['From'] = config["display_name"]
email["To"] = mail_add;
email["subject"] = subject;
email.set_content(mail_content);
context = ssl.create_default_context();
with smtplib.SMTP_SSL(config["server"], config["port"], context=context) as smtp:
smtp.login(config["email"], config["password"]);
smtp.sendmail(config["email"], mail_add, email.as_string());
send_mail("camille@chauvet.pro", "test", "text")

View File

@ -25,6 +25,8 @@ def login():
@app.route('/login', methods=['POST']) @app.route('/login', methods=['POST'])
def login_post(): def login_post():
if (not session.get("email")):
return (redirect("/connected"))
email = request.form.get('email') email = request.form.get('email')
password = request.form.get('password') password = request.form.get('password')
if (not database.user_exist(email)): if (not database.user_exist(email)):
@ -45,6 +47,8 @@ def signin():
@app.route('/signin', methods=['POST']) @app.route('/signin', methods=['POST'])
def signup_post(): def signup_post():
if (not session.get("email")):
return (redirect("/connected"))
email = request.form.get('email') email = request.form.get('email')
password = request.form.get('password') password = request.form.get('password')
repassword = request.form.get('repassword') repassword = request.form.get('repassword')