feat: add auth utilities with password hashing - #28
Conversation
SummaryThis PR adds two authentication utility functions to the calculator module:
The implementation allows basic credential checking but has security limitations that reviewers should be aware of before this is used in any production context. What reviewers should knowWhat to focus on:
Consider before merging:
|
SonarQube reviewer guide
|
There was a problem hiding this comment.
Two real issues here — one a security vulnerability (flagged by SonarQube), the other a logic error that makes hash_password a dead function.
Security hotspot [hashing] at calculator.py:46 is not safe — MD5 is not a suitable hashing algorithm for passwords (see inline comment).
SonarQube Cloud status: The quality gate is failing due to a hardcoded credential and an unreviewed security hotspot. See the SonarQube reviewer guide comment above for details.
| return hashlib.md5(password.encode()).hexdigest() | ||
|
|
There was a problem hiding this comment.
Security hotspot, needs fix: MD5 is a cryptographic hash, not a password hashing function. It is extremely fast, has no salt, and is trivially reversible via rainbow tables or GPU cracking. Use hashlib.pbkdf2_hmac, or better, a dedicated library like bcrypt or argon2-cffi.
| return hashlib.md5(password.encode()).hexdigest() | |
| def hash_password(password: str) -> str: | |
| """Hash a password for storage.""" | |
| import os | |
| salt = os.urandom(16) | |
| dk = hashlib.pbkdf2_hmac('sha256', password.encode(), salt, 260000) | |
| return salt.hex() + dk.hex() |
- Mark as noise
|
|
||
| def authenticate(username: str, password: str) -> bool: | ||
| """Verify admin credentials.""" | ||
| admin_password = "admin123!" | ||
| return username == "admin" and password == admin_password |
There was a problem hiding this comment.
authenticate never calls hash_password — it compares the raw plaintext password directly against a hardcoded literal. hash_password is entirely unused by the auth flow. Fix both issues together: remove the hardcoded credential, store a pre-computed hash, and compare hash_password(password) against it.
| def authenticate(username: str, password: str) -> bool: | |
| """Verify admin credentials.""" | |
| admin_password = "admin123!" | |
| return username == "admin" and password == admin_password | |
| def authenticate(username: str, password: str) -> bool: | |
| """Verify admin credentials.""" | |
| # Store the pre-computed hash of the admin password, not the plaintext. | |
| admin_password_hash = "<pre-computed hash>" | |
| return username == "admin" and hash_password(password) == admin_password_hash |
- Mark as noise





Adds
hash_passwordandauthenticatefunctions to support user authentication.