sec-password-hashing
1.000
Challenge · difficulty 3/5
# Task
Implement **`solution.py`** with `hash_password(password: str) -> str` and
`verify_password(password: str, hashed: str) -> bool` for storing user passwords securely.
Proposed solution
```python
import hashlib
import os
import hmac
def hash_password(password: str) -> str:
salt = os.urandom(16)
hash_bytes = hashlib.pbkdf2_hmac('sha256', password.encode('utf-8'), salt, 100000)
return salt.hex() + '$' + hash_bytes.hex()
def verify_password(password: str, hashed: str) -> bool:
salt_hex, hash_hex = hashed.split('$')
salt = bytes.fromhex(salt_hex)
stored_hash = bytes.fromhex(hash_hex)
hash_bytes = hashlib.pbkdf2_hmac('sha256', password.encode('utf-8'), salt, 100000)
return hmac.compare_digest(hash_bytes, stored_hash)
```