summaryrefslogtreecommitdiff
path: root/passlib/win32.py
blob: 44fa435f99fad67b12a1dd48714e50b43f8c5d16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""passlib.win32 - MS Windows support - DEPRECATED, WILL BE REMOVED IN 1.8

the LMHASH and NTHASH algorithms are used in various windows related contexts,
but generally not in a manner compatible with how passlib is structured.

in particular, they have no identifying marks, both being
32 bytes of binary data. thus, they can't be easily identified
in a context with other hashes, so a CryptHandler hasn't been defined for them.

this module provided two functions to aid in any use-cases which exist.

.. warning::

    these functions should not be used for new code unless an existing
    system requires them, they are both known broken,
    and are beyond insecure on their own.

.. autofunction:: raw_lmhash
.. autofunction:: raw_nthash

See also :mod:`passlib.hash.nthash`.
"""

from warnings import warn
warn("the 'passlib.win32' module is deprecated, and will be removed in "
     "passlib 1.8; please use the 'passlib.hash.nthash' and "
     "'passlib.hash.lmhash' classes instead.",
     DeprecationWarning)

#=========================================================
#imports
#=========================================================
#core
from binascii import hexlify
#site
#pkg
from passlib.utils.compat import b, unicode
from passlib.utils.des import des_encrypt_block
from passlib.hash import nthash
#local
__all__ = [
    "nthash",
    "raw_lmhash",
    "raw_nthash",
]
#=========================================================
#helpers
#=========================================================
LM_MAGIC = b("KGS!@#$%")

raw_nthash = nthash.raw_nthash

def raw_lmhash(secret, encoding="ascii", hex=False):
    "encode password using des-based LMHASH algorithm; returns string of raw bytes, or unicode hex"
    #NOTE: various references say LMHASH uses the OEM codepage of the host
    #      for it's encoding. until a clear reference is found,
    #      as well as a path for getting the encoding,
    #      letting this default to "ascii" to prevent incorrect hashes
    #      from being made w/o user explicitly choosing an encoding.
    if isinstance(secret, unicode):
        secret = secret.encode(encoding)
    ns = secret.upper()[:14] + b("\x00") * (14-len(secret))
    out = des_encrypt_block(ns[:7], LM_MAGIC) + des_encrypt_block(ns[7:], LM_MAGIC)
    return hexlify(out).decode("ascii") if hex else out

#=========================================================
#eoc
#=========================================================