Debian Codegraph @

src/argon2/_legacy.py

raw symbols SCIP
1 # SPDX-License-Identifier: MIT
2
3 """
4 Legacy mid-level functions.
5 """
6
7 from __future__ import annotations
8
9 import os
10 import warnings
11
12 from typing import Literal
13
14 from ._password_hasher import (
15 DEFAULT_HASH_LENGTH,
16 DEFAULT_MEMORY_COST,
17 DEFAULT_PARALLELISM,
18 DEFAULT_RANDOM_SALT_LENGTH,
19 DEFAULT_TIME_COST,
20 )
21 from .low_level import Type, hash_secret, hash_secret_raw, verify_secret
22
23
24 _INSTEAD = " is deprecated, use argon2.PasswordHasher instead"
25
26
27 def hash_password(
28 password: bytes,
29 salt: bytes | None = None,
30 time_cost: int = DEFAULT_TIME_COST,
31 memory_cost: int = DEFAULT_MEMORY_COST,
32 parallelism: int = DEFAULT_PARALLELISM,
33 hash_len: int = DEFAULT_HASH_LENGTH,
34 type: Type = Type.I,
35 ) -> bytes:
36 """
37 Legacy alias for :func:`argon2.low_level.hash_secret` with default
38 parameters.
39
40 .. deprecated:: 16.0.0
41 Use :class:`argon2.PasswordHasher` for passwords.
42 """
43 warnings.warn(
44 "argon2.hash_password" + _INSTEAD, DeprecationWarning, stacklevel=2
45 )
46 if salt is None:
47 salt = os.urandom(DEFAULT_RANDOM_SALT_LENGTH)
48 return hash_secret(
49 password, salt, time_cost, memory_cost, parallelism, hash_len, type
50 )
51
52
53 def hash_password_raw(
54 password: bytes,
55 salt: bytes | None = None,
56 time_cost: int = DEFAULT_TIME_COST,
57 memory_cost: int = DEFAULT_MEMORY_COST,
58 parallelism: int = DEFAULT_PARALLELISM,
59 hash_len: int = DEFAULT_HASH_LENGTH,
60 type: Type = Type.I,
61 ) -> bytes:
62 """
63 Legacy alias for :func:`argon2.low_level.hash_secret_raw` with default
64 parameters.
65
66 .. deprecated:: 16.0.0
67 Use :class:`argon2.PasswordHasher` for passwords.
68 """
69 warnings.warn(
70 "argon2.hash_password_raw" + _INSTEAD, DeprecationWarning, stacklevel=2
71 )
72 if salt is None:
73 salt = os.urandom(DEFAULT_RANDOM_SALT_LENGTH)
74 return hash_secret_raw(
75 password, salt, time_cost, memory_cost, parallelism, hash_len, type
76 )
77
78
79 def verify_password(
80 hash: bytes, password: bytes, type: Type = Type.I
81 ) -> Literal[True]:
82 """
83 Legacy alias for :func:`argon2.low_level.verify_secret` with default
84 parameters.
85
86 .. deprecated:: 16.0.0
87 Use :class:`argon2.PasswordHasher` for passwords.
88 """
89 warnings.warn(
90 "argon2.verify_password" + _INSTEAD, DeprecationWarning, stacklevel=2
91 )
92 return verify_secret(hash, password, type)