1 import string, warnings
2 from ldaptor import md4, config
3
4 lower='abcdefghijklmnopqrstuvwxyz'
5 upper=lower.upper()
6 toupper=string.maketrans(lower, upper)
7
9 """Generates nt md4 password hash for a given password."""
10
11 password=password[:128]
12 password=''.join([c+'\000' for c in password])
13 return md4.new(password).hexdigest().translate(toupper);
14
16 """
17 Generates a lanman password hash that matches no password.
18
19 Note that the author thinks LanMan hashes should be banished from
20 the face of the earth.
21 """
22 return 32*'X'
23
28
30 """
31 Generates lanman password hash for a given password.
32
33 Note that the author thinks LanMan hashes should be banished from
34 the face of the earth.
35 """
36
37 if not config.useLMhash():
38 return lmhash_locked()
39
40 password = (password+14*'\0')[:14]
41 password = password.upper()
42
43 return _deshash(password[:7]) + _deshash(password[7:])
44
45 try:
46 from Crypto.Cipher import DES
47 except ImportError:
48 lmhash = _no_lmhash
49 else:
50 lmhash = _have_lmhash
51
52 LM_MAGIC = "KGS!@#$%"
54
55
56 bits = []
57 for byte in [ord(c) for c in p]:
58 bits.extend([bool(byte & 128),
59 bool(byte & 64),
60 bool(byte & 32),
61 bool(byte & 16),
62 bool(byte & 8),
63 bool(byte & 4),
64 bool(byte & 2),
65 bool(byte & 1)])
66 def _pack(bits):
67 x = ((bits[0] << 7)
68 + (bits[1] << 6)
69 + (bits[2] << 5)
70 + (bits[3] << 4)
71 + (bits[4] << 3)
72 + (bits[5] << 2)
73 + (bits[6] << 1))
74 return x
75
76 bytes = (_pack(bits[:7]),
77 _pack(bits[7:14]),
78 _pack(bits[14:21]),
79 _pack(bits[21:28]),
80 _pack(bits[28:35]),
81 _pack(bits[35:42]),
82 _pack(bits[42:49]),
83 _pack(bits[49:]))
84 bytes = ''.join([chr(x) for x in bytes])
85 cipher = DES.new(bytes, DES.MODE_ECB)
86 raw = cipher.encrypt(LM_MAGIC)
87 l = ['%02X' % ord(x) for x in raw]
88 return ''.join(l)
89