Spaces:
Sleeping
Sleeping
samar m commited on
Commit ·
d01ea0f
1
Parent(s): f089dd2
feat: implement bcrypt password hashing
Browse files- backend/auth/password.py +9 -1
- tests/__init__.py +0 -0
- tests/test_password.py +23 -0
backend/auth/password.py
CHANGED
|
@@ -1 +1,9 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import bcrypt
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def hash_password(plain: str) -> str:
|
| 5 |
+
return bcrypt.hashpw(plain.encode(), bcrypt.gensalt(rounds=12)).decode()
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def verify_password(plain: str, hashed: str) -> bool:
|
| 9 |
+
return bcrypt.checkpw(plain.encode(), hashed.encode())
|
tests/__init__.py
ADDED
|
File without changes
|
tests/test_password.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from backend.auth.password import hash_password, verify_password
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def test_hash_returns_string():
|
| 5 |
+
result = hash_password("secret123")
|
| 6 |
+
assert isinstance(result, str)
|
| 7 |
+
assert result != "secret123"
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def test_verify_correct_password():
|
| 11 |
+
hashed = hash_password("mypassword")
|
| 12 |
+
assert verify_password("mypassword", hashed) is True
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def test_verify_wrong_password():
|
| 16 |
+
hashed = hash_password("mypassword")
|
| 17 |
+
assert verify_password("wrongpassword", hashed) is False
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def test_same_password_different_hashes():
|
| 21 |
+
h1 = hash_password("same")
|
| 22 |
+
h2 = hash_password("same")
|
| 23 |
+
assert h1 != h2 # bcrypt uses random salt
|