Skip to content

Potential fix for code scanning alert no. 1: Use of a broken or weak cryptographic hashing algorithm on sensitive data #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/backend/routers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,29 @@
tags=["auth"]
)

from argon2 import PasswordHasher

ph = PasswordHasher()

def hash_password(password):
"""Hash password using SHA-256"""
return hashlib.sha256(password.encode()).hexdigest()
"""Hash password using Argon2"""
return ph.hash(password)

@router.post("/login")
def login(username: str, password: str) -> Dict[str, Any]:
"""Login a teacher account"""
# Hash the provided password
hashed_password = hash_password(password)

# Find the teacher in the database
teacher = teachers_collection.find_one({"_id": username})

if not teacher or teacher["password"] != hashed_password:
if not teacher:
raise HTTPException(status_code=401, detail="Invalid username or password")

# Verify the provided password
try:
if not ph.verify(teacher["password"], password):
raise HTTPException(status_code=401, detail="Invalid username or password")
except Exception:
raise HTTPException(status_code=401, detail="Invalid username or password")

# Return teacher information (excluding password)
Expand Down