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.
 
 
 
 

129 linhas
4.7 KiB

  1. """passlib.handlers.mysql
  2. MySQL 3.2.3 / OLD_PASSWORD()
  3. This implements Mysql's OLD_PASSWORD algorithm, introduced in version 3.2.3, deprecated in version 4.1.
  4. See :mod:`passlib.handlers.mysql_41` for the new algorithm was put in place in version 4.1
  5. This algorithm is known to be very insecure, and should only be used to verify existing password hashes.
  6. http://djangosnippets.org/snippets/1508/
  7. MySQL 4.1.1 / NEW PASSWORD
  8. This implements Mysql new PASSWORD algorithm, introduced in version 4.1.
  9. This function is unsalted, and therefore not very secure against rainbow attacks.
  10. It should only be used when dealing with mysql passwords,
  11. for all other purposes, you should use a salted hash function.
  12. Description taken from http://dev.mysql.com/doc/refman/6.0/en/password-hashing.html
  13. """
  14. #=============================================================================
  15. # imports
  16. #=============================================================================
  17. # core
  18. from hashlib import sha1
  19. import re
  20. import logging; log = logging.getLogger(__name__)
  21. from warnings import warn
  22. # site
  23. # pkg
  24. from passlib.utils import to_native_str
  25. from passlib.utils.compat import bascii_to_str, unicode, u, \
  26. byte_elem_value, str_to_uascii
  27. import passlib.utils.handlers as uh
  28. # local
  29. __all__ = [
  30. 'mysql323',
  31. 'mysq41',
  32. ]
  33. #=============================================================================
  34. # backend
  35. #=============================================================================
  36. class mysql323(uh.StaticHandler):
  37. """This class implements the MySQL 3.2.3 password hash, and follows the :ref:`password-hash-api`.
  38. It has no salt and a single fixed round.
  39. The :meth:`~passlib.ifc.PasswordHash.hash` and :meth:`~passlib.ifc.PasswordHash.genconfig` methods accept no optional keywords.
  40. """
  41. #===================================================================
  42. # class attrs
  43. #===================================================================
  44. name = "mysql323"
  45. checksum_size = 16
  46. checksum_chars = uh.HEX_CHARS
  47. #===================================================================
  48. # methods
  49. #===================================================================
  50. @classmethod
  51. def _norm_hash(cls, hash):
  52. return hash.lower()
  53. def _calc_checksum(self, secret):
  54. # FIXME: no idea if mysql has a policy about handling unicode passwords
  55. if isinstance(secret, unicode):
  56. secret = secret.encode("utf-8")
  57. MASK_32 = 0xffffffff
  58. MASK_31 = 0x7fffffff
  59. WHITE = b' \t'
  60. nr1 = 0x50305735
  61. nr2 = 0x12345671
  62. add = 7
  63. for c in secret:
  64. if c in WHITE:
  65. continue
  66. tmp = byte_elem_value(c)
  67. nr1 ^= ((((nr1 & 63)+add)*tmp) + (nr1 << 8)) & MASK_32
  68. nr2 = (nr2+((nr2 << 8) ^ nr1)) & MASK_32
  69. add = (add+tmp) & MASK_32
  70. return u("%08x%08x") % (nr1 & MASK_31, nr2 & MASK_31)
  71. #===================================================================
  72. # eoc
  73. #===================================================================
  74. #=============================================================================
  75. # handler
  76. #=============================================================================
  77. class mysql41(uh.StaticHandler):
  78. """This class implements the MySQL 4.1 password hash, and follows the :ref:`password-hash-api`.
  79. It has no salt and a single fixed round.
  80. The :meth:`~passlib.ifc.PasswordHash.hash` and :meth:`~passlib.ifc.PasswordHash.genconfig` methods accept no optional keywords.
  81. """
  82. #===================================================================
  83. # class attrs
  84. #===================================================================
  85. name = "mysql41"
  86. _hash_prefix = u("*")
  87. checksum_chars = uh.HEX_CHARS
  88. checksum_size = 40
  89. #===================================================================
  90. # methods
  91. #===================================================================
  92. @classmethod
  93. def _norm_hash(cls, hash):
  94. return hash.upper()
  95. def _calc_checksum(self, secret):
  96. # FIXME: no idea if mysql has a policy about handling unicode passwords
  97. if isinstance(secret, unicode):
  98. secret = secret.encode("utf-8")
  99. return str_to_uascii(sha1(sha1(secret).digest()).hexdigest()).upper()
  100. #===================================================================
  101. # eoc
  102. #===================================================================
  103. #=============================================================================
  104. # eof
  105. #=============================================================================