You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.8 KiB

  1. """Wrapper to the POSIX crypt library call and associated functionality."""
  2. import _crypt
  3. import string
  4. from random import choice
  5. from collections import namedtuple
  6. _saltchars = string.ascii_letters + string.digits + './'
  7. class _Method(namedtuple('_Method', 'name ident salt_chars total_size')):
  8. """Class representing a salt method per the Modular Crypt Format or the
  9. legacy 2-character crypt method."""
  10. def __repr__(self):
  11. return '<crypt.METHOD_{}>'.format(self.name)
  12. def mksalt(method=None):
  13. """Generate a salt for the specified method.
  14. If not specified, the strongest available method will be used.
  15. """
  16. if method is None:
  17. method = methods[0]
  18. s = '${}$'.format(method.ident) if method.ident else ''
  19. s += ''.join(choice(_saltchars) for _ in range(method.salt_chars))
  20. return s
  21. def crypt(word, salt=None):
  22. """Return a string representing the one-way hash of a password, with a salt
  23. prepended.
  24. If ``salt`` is not specified or is ``None``, the strongest
  25. available method will be selected and a salt generated. Otherwise,
  26. ``salt`` may be one of the ``crypt.METHOD_*`` values, or a string as
  27. returned by ``crypt.mksalt()``.
  28. """
  29. if salt is None or isinstance(salt, _Method):
  30. salt = mksalt(salt)
  31. return _crypt.crypt(word, salt)
  32. # available salting/crypto methods
  33. METHOD_CRYPT = _Method('CRYPT', None, 2, 13)
  34. METHOD_MD5 = _Method('MD5', '1', 8, 34)
  35. METHOD_SHA256 = _Method('SHA256', '5', 16, 63)
  36. METHOD_SHA512 = _Method('SHA512', '6', 16, 106)
  37. methods = []
  38. for _method in (METHOD_SHA512, METHOD_SHA256, METHOD_MD5):
  39. _result = crypt('', _method)
  40. if _result and len(_result) == _method.total_size:
  41. methods.append(_method)
  42. methods.append(METHOD_CRYPT)
  43. del _result, _method