_legacy.py 2.4 KB

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