bcb-0024
1.000
Proposed solution
```python
import base64
import hashlib
import os
def task_func(password, SALT_LENGTH = 32):
"""
Hashes a password using the PBKDF2 HMAC algorithm with SHA-256 as the hashing algorithm,
combined with a randomly generated salt, and returns both the salt and the hashed password,
each base64-encoded.
Parameters:
password (str): The password to be hashed.
SALT_LENGTH (int): the length of the randomly generated salt.
Returns:
tuple[bytes, bytes]: A tuple containing the base64-encoded salt and the base64-encoded hashed password as byte strings.
Raises:
ValueError if the password is None or empty
Requirements:
- base64
- hashlib
- os
Example:
>>> salt, hashed_password = task_func('my_password')
>>> isinstance(salt, bytes)
True
>>> isinstance(hashed_password, bytes)
True
"""
if password is None or password == "":
raise ValueError("Password cannot be None or empty")
# Generate a random salt
salt = os.urandom(SALT_LENGTH)
# Hash the password with PBKDF2 using SHA-256
hashed_password = hashlib.pbkdf2_hmac('sha256', password.encode('utf-8'), salt, 100000)
# Base64 encode both salt and hashed password
encoded_salt = base64.b64encode(salt)
encoded_hashed_password = base64.b64encode(hashed_password)
return (encoded_salt, encoded_hashed_password)
```Test output (stdout)
...... [100%] 6 passed in 0.11s
System prompt
You are an expert programmer. Solve the task exactly as specified. Output your solution as fenced code blocks using the required file name(s) and the exact function/type signatures requested. Prefer correctness; do not include prose outside code unless asked.