diff --git a/database.db b/database.db new file mode 100644 index 0000000..99b4e67 Binary files /dev/null and b/database.db differ diff --git a/main.py b/main.py index d2957b3..c3de155 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,43 @@ -from fastapi import FastAPI +# Imports +from typing import Annotated + +from fastapi import FastAPI, Depends, HTTPException, Query +from sqlmodel import Field, Session, SQLModel, create_engine, select import hashlib import random +# Initialize the FastAPI App app = FastAPI() +# Create the user table + +class User(SQLModel, table=True): + id: int = Field(default=None, primary_key=True) + name: str = Field(index=True) + age: int + password: str + +# SQLModel stuff + +sqlite_file_name = "database.db" +sqlite_url = f"sqlite:///{sqlite_file_name}" + +connect_args = {"check_same_thread": False} +engine = create_engine(sqlite_url, connect_args=connect_args) + +def create_db_and_tables(): + SQLModel.metadata.create_all(engine) + +def get_session(): + with Session(engine) as session: + yield session + +SessionDep = Annotated[Session, Depends(get_session)] + +@app.on_event("startup") +def on_startup(): + create_db_and_tables() + @app.get("/") def hello_world(): return {"message": "Hello World!"} @@ -17,9 +51,18 @@ def hash(password: str): @app.get("/verify/{password}/{hash}/{salt}") def verify(password: str, hash: str, salt: int): - password = password + str(salt) - hashed = hashlib.sha256(password.encode()).hexdigest() + if salt != 0: + password = password + str(salt) + hashed = hashlib.sha256(password.encode()).hexdigest() + else: + hashed = hashlib.sha256(password.encode()).hexdigest() + if hashed == hash: return {"message": "Password is correct", "correct": True} return {"message": "Password is incorrect", "correct": False} + +@app.get("/hash/no-salt/{password}") +def no_salt(password: str): + hashed = hashlib.sha256(password.encode()).hexdigest() + return {"hash": hashed}