Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

56 linhas
2.2 KiB

  1. """passlib.handlers.postgres_md5 - MD5-based algorithm used by Postgres for pg_shadow table"""
  2. #=============================================================================
  3. # imports
  4. #=============================================================================
  5. # core
  6. from hashlib import md5
  7. import logging; log = logging.getLogger(__name__)
  8. # site
  9. # pkg
  10. from passlib.utils import to_bytes
  11. from passlib.utils.compat import str_to_uascii, unicode, u
  12. import passlib.utils.handlers as uh
  13. # local
  14. __all__ = [
  15. "postgres_md5",
  16. ]
  17. #=============================================================================
  18. # handler
  19. #=============================================================================
  20. class postgres_md5(uh.HasUserContext, uh.StaticHandler):
  21. """This class implements the Postgres MD5 Password hash, and follows the :ref:`password-hash-api`.
  22. It does a single round of hashing, and relies on the username as the salt.
  23. The :meth:`~passlib.ifc.PasswordHash.hash`, :meth:`~passlib.ifc.PasswordHash.genhash`, and :meth:`~passlib.ifc.PasswordHash.verify` methods all require the
  24. following additional contextual keywords:
  25. :type user: str
  26. :param user: name of postgres user account this password is associated with.
  27. """
  28. #===================================================================
  29. # algorithm information
  30. #===================================================================
  31. name = "postgres_md5"
  32. _hash_prefix = u("md5")
  33. checksum_chars = uh.HEX_CHARS
  34. checksum_size = 32
  35. #===================================================================
  36. # primary interface
  37. #===================================================================
  38. def _calc_checksum(self, secret):
  39. if isinstance(secret, unicode):
  40. secret = secret.encode("utf-8")
  41. user = to_bytes(self.user, "utf-8", param="user")
  42. return str_to_uascii(md5(secret + user).hexdigest())
  43. #===================================================================
  44. # eoc
  45. #===================================================================
  46. #=============================================================================
  47. # eof
  48. #=============================================================================