|
1 |
/* |
1 |
/* |
2 |
Python Samba Password Hash Generating module |
2 |
Python Samba Password Hash Generating module |
3 |
|
3 |
|
4 |
Copyright (C) 2002 Barry Pederson <bp@barryp.org> |
4 |
Copyright (C) 2002 Barry Pederson <bp@barryp.org> |
5 |
|
5 |
|
6 |
This program is free software; you can redistribute it and/or modify |
6 |
This program is free software; you can redistribute it and/or modify |
|
23 |
|
23 |
|
24 |
*/ |
24 |
*/ |
25 |
|
25 |
|
26 |
|
26 |
|
27 |
#include "Python.h" |
27 |
#include "Python.h" |
28 |
#include <memory.h> |
28 |
#include <memory.h> |
29 |
#include <ctype.h> |
29 |
#include <ctype.h> |
30 |
|
30 |
|
|
56 |
static char HEXCHARS[] = "0123456789ABCDEF"; |
56 |
static char HEXCHARS[] = "0123456789ABCDEF"; |
57 |
|
57 |
|
58 |
static PyObject * |
58 |
static PyObject * |
59 |
hash_to_string(char *tmp) |
59 |
hash_to_string(char *tmp) |
60 |
{ |
60 |
{ |
61 |
int i; |
61 |
int i; |
62 |
unsigned char c; |
62 |
unsigned char c; |
63 |
char outbuffer[32]; |
63 |
char outbuffer[32]; |
64 |
|
64 |
|
65 |
/* build string from binary hash */ |
65 |
/* build string from binary hash */ |
66 |
for(i = 0; i < 16; i++) |
66 |
for(i = 0; i < 16; i++) |
67 |
{ |
67 |
{ |
68 |
c=tmp[i]; |
68 |
c=tmp[i]; |
69 |
outbuffer[ i*2 ] = HEXCHARS[(c>>4) & 0x0f]; |
69 |
outbuffer[ i*2 ] = HEXCHARS[(c>>4) & 0x0f]; |
70 |
outbuffer[(i*2)+1] = HEXCHARS[ c & 0x0f]; |
70 |
outbuffer[(i*2)+1] = HEXCHARS[ c & 0x0f]; |
71 |
} |
71 |
} |
72 |
|
72 |
|
73 |
return PyString_FromStringAndSize(outbuffer, 32); |
73 |
return PyString_FromStringAndSize(outbuffer, 32); |
74 |
} |
74 |
} |
75 |
|
75 |
|
76 |
|
76 |
|
77 |
static PyObject * |
77 |
static PyObject * |
78 |
lmhash(PyObject *self, PyObject *args) |
78 |
lmhash(PyObject *self, PyObject *args) |
79 |
{ |
79 |
{ |
80 |
char *pwd; |
80 |
char *pwd; |
Lines 88-156
lmhash(PyObject *self, PyObject *args)
|
Link Here
|
---|
|
88 |
if (!(PyArg_ParseTuple(args, "s#", &pwd, &pwd_len))) |
88 |
if (!(PyArg_ParseTuple(args, "s#", &pwd, &pwd_len))) |
89 |
return NULL; |
89 |
return NULL; |
90 |
|
90 |
|
91 |
/* make a copy that's truncated to 14 chars and uppercased */ |
91 |
/* make a copy that's truncated to 14 chars and uppercased */ |
92 |
if (pwd_len > LMPASSWDLEN) |
92 |
if (pwd_len > LMPASSWDLEN) |
93 |
pwd_len = LMPASSWDLEN; |
93 |
pwd_len = LMPASSWDLEN; |
94 |
memset(lmpwd, '\0', sizeof(lmpwd)); |
94 |
memset(lmpwd, '\0', sizeof(lmpwd)); |
95 |
for (i = 0; i < pwd_len; i++) |
95 |
for (i = 0; i < pwd_len; i++) |
96 |
lmpwd[i]=toupper(pwd[i]); |
96 |
lmpwd[i]=toupper(pwd[i]); |
97 |
|
97 |
|
98 |
/* Generate the hash */ |
98 |
/* Generate the hash */ |
99 |
memset(hashout,'\0',sizeof(hashout)); |
99 |
memset(hashout,'\0',sizeof(hashout)); |
100 |
E_P16(lmpwd, hashout); |
100 |
E_P16(lmpwd, hashout); |
101 |
|
101 |
|
102 |
/* clean things up - don't know how much good it does since |
102 |
/* clean things up - don't know how much good it does since |
103 |
Python presumably has other copies of the password floating |
103 |
Python presumably has other copies of the password floating |
104 |
around, but what the hell - I'll do my part since I know it's |
104 |
around, but what the hell - I'll do my part since I know it's |
105 |
safe. |
105 |
safe. |
106 |
*/ |
106 |
*/ |
107 |
memset(lmpwd, '\0', sizeof(lmpwd)); |
107 |
memset(lmpwd, '\0', sizeof(lmpwd)); |
108 |
|
108 |
|
109 |
return hash_to_string(hashout); |
109 |
return hash_to_string(hashout); |
110 |
} |
110 |
} |
111 |
|
111 |
|
112 |
|
112 |
|
113 |
static PyObject * |
113 |
static PyObject * |
114 |
nthash(PyObject *self, PyObject *args) |
114 |
nthash(PyObject *self, PyObject *args) |
115 |
{ |
115 |
{ |
116 |
char *pwd; |
116 |
char *pwd; |
117 |
int pwd_len; |
117 |
int pwd_len; |
118 |
char hashout[17]; |
118 |
char hashout[17]; |
119 |
|
119 |
|
120 |
pwd = NULL; |
120 |
pwd = NULL; |
121 |
if (!(PyArg_ParseTuple(args, "es#", "utf-16le", &pwd, &pwd_len))) |
121 |
if (!(PyArg_ParseTuple(args, "es#", "utf-16le", &pwd, &pwd_len))) |
122 |
return NULL; |
122 |
return NULL; |
123 |
|
123 |
|
124 |
memset(hashout,'\0',sizeof(hashout)); |
124 |
memset(hashout,'\0',sizeof(hashout)); |
125 |
mdfour(hashout, pwd, pwd_len); |
125 |
mdfour(hashout, pwd, pwd_len); |
126 |
|
126 |
|
127 |
/* clean things up - don't know how much good it does since |
127 |
/* clean things up - don't know how much good it does since |
128 |
Python presumably has other copies of the password floating |
128 |
Python presumably has other copies of the password floating |
129 |
around, but what the hell - I'll do my part since I'm pretty |
129 |
around, but what the hell - I'll do my part since I'm pretty |
130 |
sure it's safe, from my understanding of PyArg_ParseTuple and "es#" |
130 |
sure it's safe, from my understanding of PyArg_ParseTuple and "es#" |
131 |
*/ |
131 |
*/ |
132 |
memset(pwd, '\0', pwd_len); |
132 |
memset(pwd, '\0', pwd_len); |
133 |
PyMem_Free(pwd); |
133 |
PyMem_Free(pwd); |
134 |
|
134 |
|
135 |
return hash_to_string(hashout); |
135 |
return hash_to_string(hashout); |
136 |
} |
136 |
} |
137 |
|
137 |
|
138 |
|
138 |
|
139 |
static PyObject * |
139 |
static PyObject * |
140 |
hash(PyObject *self, PyObject *args) |
140 |
hash(PyObject *self, PyObject *args) |
141 |
{ |
141 |
{ |
142 |
PyObject *result; |
142 |
PyObject *result; |
143 |
|
143 |
|
144 |
result = PyTuple_New(2); |
144 |
result = PyTuple_New(2); |
145 |
PyTuple_SetItem(result, 0, lmhash(self, args)); |
145 |
PyTuple_SetItem(result, 0, lmhash(self, args)); |
146 |
PyTuple_SetItem(result, 1, nthash(self, args)); |
146 |
PyTuple_SetItem(result, 1, nthash(self, args)); |
147 |
|
147 |
|
148 |
return result; |
148 |
return result; |
149 |
} |
149 |
} |
150 |
|
150 |
|
151 |
|
151 |
|
152 |
/* List of functions exported by this module */ |
152 |
/* List of functions exported by this module */ |
153 |
static PyMethodDef smbpasswd_functions[] = |
153 |
static PyMethodDef smbpasswd_functions[] = |
154 |
{ |
154 |
{ |
155 |
{"lmhash", lmhash, METH_VARARGS}, |
155 |
{"lmhash", lmhash, METH_VARARGS}, |
156 |
{"nthash", nthash, METH_VARARGS}, |
156 |
{"nthash", nthash, METH_VARARGS}, |
Lines 160-166
static PyMethodDef smbpasswd_functions[] =
|
Link Here
|
---|
|
160 |
|
160 |
|
161 |
|
161 |
|
162 |
/* Initialize this module. */ |
162 |
/* Initialize this module. */ |
163 |
void |
163 |
void |
164 |
initsmbpasswd(void) |
164 |
initsmbpasswd(void) |
165 |
{ |
165 |
{ |
166 |
Py_InitModule3("smbpasswd", smbpasswd_functions, module_doc); |
166 |
Py_InitModule3("smbpasswd", smbpasswd_functions, module_doc); |
167 |
- |
|
|
168 |
-- |
169 |
smbpasswd.c | 31 ++++++++++++++++++++++++++++++- |
167 |
smbpasswd.c | 31 ++++++++++++++++++++++++++++++- |
170 |
1 file changed, 30 insertions(+), 1 deletion(-) |
168 |
1 file changed, 30 insertions(+), 1 deletion(-) |