codekingpro/portable-devtools
114k
1"""passlib.win32 - MS Windows support - DEPRECATED, WILL BE REMOVED IN 1.82 3the LMHASH and NTHASH algorithms are used in various windows related contexts,4but generally not in a manner compatible with how passlib is structured.5 6in particular, they have no identifying marks, both being732 bytes of binary data. thus, they can't be easily identified8in a context with other hashes, so a CryptHandler hasn't been defined for them.9 10this module provided two functions to aid in any use-cases which exist.11 12.. warning::13 14 these functions should not be used for new code unless an existing15 system requires them, they are both known broken,16 and are beyond insecure on their own.17 18.. autofunction:: raw_lmhash19.. autofunction:: raw_nthash20 21See also :mod:`passlib.hash.nthash`.22"""23 24from warnings import warn25warn("the 'passlib.win32' module is deprecated, and will be removed in "26 "passlib 1.8; please use the 'passlib.hash.nthash' and "27 "'passlib.hash.lmhash' classes instead.",28 DeprecationWarning)29 30#=============================================================================31# imports32#=============================================================================33# core34from binascii import hexlify35# site36# pkg37from passlib.utils.compat import unicode38from passlib.crypto.des import des_encrypt_block39from passlib.hash import nthash40# local41__all__ = [42 "nthash",43 "raw_lmhash",44 "raw_nthash",45]46#=============================================================================47# helpers48#=============================================================================49LM_MAGIC = b"KGS!@#$%"50 51raw_nthash = nthash.raw_nthash52 53def raw_lmhash(secret, encoding="ascii", hex=False):54 """encode password using des-based LMHASH algorithm; returns string of raw bytes, or unicode hex"""55 # NOTE: various references say LMHASH uses the OEM codepage of the host56 # for its encoding. until a clear reference is found,57 # as well as a path for getting the encoding,58 # letting this default to "ascii" to prevent incorrect hashes59 # from being made w/o user explicitly choosing an encoding.60 if isinstance(secret, unicode):61 secret = secret.encode(encoding)62 ns = secret.upper()[:14] + b"\x00" * (14-len(secret))63 out = des_encrypt_block(ns[:7], LM_MAGIC) + des_encrypt_block(ns[7:], LM_MAGIC)64 return hexlify(out).decode("ascii") if hex else out65 66#=============================================================================67# eoc68#=============================================================================69 