OTPSystem/accounts.py
2025-02-22 20:09:07 -06:00

48 lines
1.5 KiB
Python

from db import *
from sendmail import *
import bcrypt
class accounts:
def hash_password(password):
salt = bcrypt.gensalt()
hashed_password = bcrypt.hashpw(password.encode('utf-8'), salt)
return hashed_password
def register(email, password):
if (db.add_account(email, accounts.hash_password(password))):
return True
def authenticate_account(email, password): # Authenticate account with password
pass
def check_password(stored_hash, input_password):
# Check if the entered password matches the stored hashed password
return bcrypt.checkpw(input_password.encode('utf-8'), stored_hash)
# Verify the password
is_correct = check_password(hashed_password, "my_secure_password")
if is_correct:
print("Password is correct!")
else:
print("Invalid password.")
def create_account_otp(email):
if db.account_exists(email):
otp = mail.gen_code()
if db.account_add_otp(email, otp):
mail.send(email, otp)
return True
else:
return False
else:
return False
def authenticate_account_otp(email, otp): # Authenticate account without password
if (db.check_account_otp(email)):
if (int(otp) == int(db.get_account_otp(email))):
db.account_del_otp(email)
return True
elif (not db.check_account_otp(email)):
db.account_del_otp(email)
return False