crypt.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. """Wrapper to the POSIX crypt library call and associated functionality."""
  2. import _crypt
  3. import string as _string
  4. from random import SystemRandom as _SystemRandom
  5. from collections import namedtuple as _namedtuple
  6. _saltchars = _string.ascii_letters + _string.digits + './'
  7. _sr = _SystemRandom()
  8. class _Method(_namedtuple('_Method', 'name ident salt_chars total_size')):
  9. """Class representing a salt method per the Modular Crypt Format or the
  10. legacy 2-character crypt method."""
  11. def __repr__(self):
  12. return '<crypt.METHOD_{}>'.format(self.name)
  13. def mksalt(method=None, *, rounds=None):
  14. """Generate a salt for the specified method.
  15. If not specified, the strongest available method will be used.
  16. """
  17. if method is None:
  18. method = methods[0]
  19. if rounds is not None and not isinstance(rounds, int):
  20. raise TypeError(f'{rounds.__class__.__name__} object cannot be '
  21. f'interpreted as an integer')
  22. if not method.ident: # traditional
  23. s = ''
  24. else: # modular
  25. s = f'${method.ident}$'
  26. if method.ident and method.ident[0] == '2': # Blowfish variants
  27. if rounds is None:
  28. log_rounds = 12
  29. else:
  30. log_rounds = int.bit_length(rounds-1)
  31. if rounds != 1 << log_rounds:
  32. raise ValueError('rounds must be a power of 2')
  33. if not 4 <= log_rounds <= 31:
  34. raise ValueError('rounds out of the range 2**4 to 2**31')
  35. s += f'{log_rounds:02d}$'
  36. elif method.ident in ('5', '6'): # SHA-2
  37. if rounds is not None:
  38. if not 1000 <= rounds <= 999_999_999:
  39. raise ValueError('rounds out of the range 1000 to 999_999_999')
  40. s += f'rounds={rounds}$'
  41. elif rounds is not None:
  42. raise ValueError(f"{method} doesn't support the rounds argument")
  43. s += ''.join(_sr.choice(_saltchars) for char in range(method.salt_chars))
  44. return s
  45. def crypt(word, salt=None):
  46. """Return a string representing the one-way hash of a password, with a salt
  47. prepended.
  48. If ``salt`` is not specified or is ``None``, the strongest
  49. available method will be selected and a salt generated. Otherwise,
  50. ``salt`` may be one of the ``crypt.METHOD_*`` values, or a string as
  51. returned by ``crypt.mksalt()``.
  52. """
  53. if salt is None or isinstance(salt, _Method):
  54. salt = mksalt(salt)
  55. return _crypt.crypt(word, salt)
  56. # available salting/crypto methods
  57. methods = []
  58. def _add_method(name, *args, rounds=None):
  59. method = _Method(name, *args)
  60. globals()['METHOD_' + name] = method
  61. salt = mksalt(method, rounds=rounds)
  62. result = crypt('', salt)
  63. if result and len(result) == method.total_size:
  64. methods.append(method)
  65. return True
  66. return False
  67. _add_method('SHA512', '6', 16, 106)
  68. _add_method('SHA256', '5', 16, 63)
  69. # Choose the strongest supported version of Blowfish hashing.
  70. # Early versions have flaws. Version 'a' fixes flaws of
  71. # the initial implementation, 'b' fixes flaws of 'a'.
  72. # 'y' is the same as 'b', for compatibility
  73. # with openwall crypt_blowfish.
  74. for _v in 'b', 'y', 'a', '':
  75. if _add_method('BLOWFISH', '2' + _v, 22, 59 + len(_v), rounds=1<<4):
  76. break
  77. _add_method('MD5', '1', 8, 34)
  78. _add_method('CRYPT', None, 2, 13)
  79. del _v, _add_method