# [CRUCIBLE-SEED]
# category: insecure_crypto
# severity: high
# language: python
# expected: MD5 used for password hashing

import hashlib
import psycopg2


def get_db():
    return psycopg2.connect(host="localhost", dbname="app", user="app", password="app")


def hash_password(password: str) -> str:
    # MD5 is cryptographically broken — must not be used for passwords
    return hashlib.md5(password.encode()).hexdigest()


def register_user(email: str, password: str) -> None:
    conn = get_db()
    cur = conn.cursor()
    pw_hash = hash_password(password)
    cur.execute("INSERT INTO users (email, password_hash) VALUES (%s, %s)", (email, pw_hash))
    conn.commit()


def login_user(email: str, password: str) -> bool:
    conn = get_db()
    cur = conn.cursor()
    pw_hash = hash_password(password)
    cur.execute("SELECT id FROM users WHERE email = %s AND password_hash = %s", (email, pw_hash))
    return cur.fetchone() is not None


def hash_file(file_path: str) -> str:
    with open(file_path, "rb") as f:
        return hashlib.md5(f.read()).hexdigest()
