This commit is contained in:
2025-05-22 21:22:15 +02:00
parent 3d57f842f9
commit 97cb9c8703
156 changed files with 1205 additions and 6603 deletions

View File

@@ -5,7 +5,6 @@ import hmac
import os
import posixpath
import secrets
import warnings
SALT_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
DEFAULT_PBKDF2_ITERATIONS = 600000
@@ -24,14 +23,6 @@ def gen_salt(length: int) -> str:
def _hash_internal(method: str, salt: str, password: str) -> tuple[str, str]:
if method == "plain":
warnings.warn(
"The 'plain' password method is deprecated and will be removed in"
" Werkzeug 3.0. Migrate to the 'scrypt' method.",
stacklevel=3,
)
return password, method
method, *args = method.split(":")
salt = salt.encode("utf-8")
password = password.encode("utf-8")
@@ -72,26 +63,20 @@ def _hash_internal(method: str, salt: str, password: str) -> tuple[str, str]:
f"pbkdf2:{hash_name}:{iterations}",
)
else:
warnings.warn(
f"The '{method}' password method is deprecated and will be removed in"
" Werkzeug 3.0. Migrate to the 'scrypt' method.",
stacklevel=3,
)
return hmac.new(salt, password, method).hexdigest(), method
raise ValueError(f"Invalid hash method '{method}'.")
def generate_password_hash(
password: str, method: str = "pbkdf2", salt_length: int = 16
password: str, method: str = "scrypt", salt_length: int = 16
) -> str:
"""Securely hash a password for storage. A password can be compared to a stored hash
using :func:`check_password_hash`.
The following methods are supported:
- ``scrypt``, more secure but not available on PyPy. The parameters are ``n``,
``r``, and ``p``, the default is ``scrypt:32768:8:1``. See
:func:`hashlib.scrypt`.
- ``pbkdf2``, the default. The parameters are ``hash_method`` and ``iterations``,
- ``scrypt``, the default. The parameters are ``n``, ``r``, and ``p``, the default
is ``scrypt:32768:8:1``. See :func:`hashlib.scrypt`.
- ``pbkdf2``, less secure. The parameters are ``hash_method`` and ``iterations``,
the default is ``pbkdf2:sha256:600000``. See :func:`hashlib.pbkdf2_hmac`.
Default parameters may be updated to reflect current guidelines, and methods may be