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.
 
 
 
 

69 lines
2.5 KiB

  1. """passlib.win32 - MS Windows support - DEPRECATED, WILL BE REMOVED IN 1.8
  2. the LMHASH and NTHASH algorithms are used in various windows related contexts,
  3. but generally not in a manner compatible with how passlib is structured.
  4. in particular, they have no identifying marks, both being
  5. 32 bytes of binary data. thus, they can't be easily identified
  6. in a context with other hashes, so a CryptHandler hasn't been defined for them.
  7. this module provided two functions to aid in any use-cases which exist.
  8. .. warning::
  9. these functions should not be used for new code unless an existing
  10. system requires them, they are both known broken,
  11. and are beyond insecure on their own.
  12. .. autofunction:: raw_lmhash
  13. .. autofunction:: raw_nthash
  14. See also :mod:`passlib.hash.nthash`.
  15. """
  16. from warnings import warn
  17. warn("the 'passlib.win32' module is deprecated, and will be removed in "
  18. "passlib 1.8; please use the 'passlib.hash.nthash' and "
  19. "'passlib.hash.lmhash' classes instead.",
  20. DeprecationWarning)
  21. #=============================================================================
  22. # imports
  23. #=============================================================================
  24. # core
  25. from binascii import hexlify
  26. # site
  27. # pkg
  28. from passlib.utils.compat import unicode
  29. from passlib.crypto.des import des_encrypt_block
  30. from passlib.hash import nthash
  31. # local
  32. __all__ = [
  33. "nthash",
  34. "raw_lmhash",
  35. "raw_nthash",
  36. ]
  37. #=============================================================================
  38. # helpers
  39. #=============================================================================
  40. LM_MAGIC = b"KGS!@#$%"
  41. raw_nthash = nthash.raw_nthash
  42. def raw_lmhash(secret, encoding="ascii", hex=False):
  43. """encode password using des-based LMHASH algorithm; returns string of raw bytes, or unicode hex"""
  44. # NOTE: various references say LMHASH uses the OEM codepage of the host
  45. # for its encoding. until a clear reference is found,
  46. # as well as a path for getting the encoding,
  47. # letting this default to "ascii" to prevent incorrect hashes
  48. # from being made w/o user explicitly choosing an encoding.
  49. if isinstance(secret, unicode):
  50. secret = secret.encode(encoding)
  51. ns = secret.upper()[:14] + b"\x00" * (14-len(secret))
  52. out = des_encrypt_block(ns[:7], LM_MAGIC) + des_encrypt_block(ns[7:], LM_MAGIC)
  53. return hexlify(out).decode("ascii") if hex else out
  54. #=============================================================================
  55. # eoc
  56. #=============================================================================