Saved and restored logging._handlerList at the same time as saving/restoring logging...
[python.git] / Modules / md5module.c
blobe12bef86363b510d4a6552adc439a42bd4d9c8b3
2 /* MD5 module */
4 /* This module provides an interface to the RSA Data Security,
5 Inc. MD5 Message-Digest Algorithm, described in RFC 1321.
6 It requires the files md5c.c and md5.h (which are slightly changed
7 from the versions in the RFC to avoid the "global.h" file.) */
10 /* MD5 objects */
12 #include "Python.h"
13 #include "structmember.h"
14 #include "md5.h"
16 typedef struct {
17 PyObject_HEAD
18 MD5_CTX md5; /* the context holder */
19 } md5object;
21 static PyTypeObject MD5type;
23 #define is_md5object(v) ((v)->ob_type == &MD5type)
25 static md5object *
26 newmd5object(void)
28 md5object *md5p;
30 md5p = PyObject_New(md5object, &MD5type);
31 if (md5p == NULL)
32 return NULL;
34 MD5Init(&md5p->md5); /* actual initialisation */
35 return md5p;
39 /* MD5 methods */
41 static void
42 md5_dealloc(md5object *md5p)
44 PyObject_Del(md5p);
48 /* MD5 methods-as-attributes */
50 static PyObject *
51 md5_update(md5object *self, PyObject *args)
53 unsigned char *cp;
54 int len;
56 if (!PyArg_ParseTuple(args, "s#:update", &cp, &len))
57 return NULL;
59 MD5Update(&self->md5, cp, len);
61 Py_INCREF(Py_None);
62 return Py_None;
65 PyDoc_STRVAR(update_doc,
66 "update (arg)\n\
67 \n\
68 Update the md5 object with the string arg. Repeated calls are\n\
69 equivalent to a single call with the concatenation of all the\n\
70 arguments.");
73 static PyObject *
74 md5_digest(md5object *self)
76 MD5_CTX mdContext;
77 unsigned char aDigest[16];
79 /* make a temporary copy, and perform the final */
80 mdContext = self->md5;
81 MD5Final(aDigest, &mdContext);
83 return PyString_FromStringAndSize((char *)aDigest, 16);
86 PyDoc_STRVAR(digest_doc,
87 "digest() -> string\n\
88 \n\
89 Return the digest of the strings passed to the update() method so\n\
90 far. This is a 16-byte string which may contain non-ASCII characters,\n\
91 including null bytes.");
94 static PyObject *
95 md5_hexdigest(md5object *self)
97 MD5_CTX mdContext;
98 unsigned char digest[16];
99 unsigned char hexdigest[32];
100 int i, j;
102 /* make a temporary copy, and perform the final */
103 mdContext = self->md5;
104 MD5Final(digest, &mdContext);
106 /* Make hex version of the digest */
107 for(i=j=0; i<16; i++) {
108 char c;
109 c = (digest[i] >> 4) & 0xf;
110 c = (c>9) ? c+'a'-10 : c + '0';
111 hexdigest[j++] = c;
112 c = (digest[i] & 0xf);
113 c = (c>9) ? c+'a'-10 : c + '0';
114 hexdigest[j++] = c;
116 return PyString_FromStringAndSize((char*)hexdigest, 32);
120 PyDoc_STRVAR(hexdigest_doc,
121 "hexdigest() -> string\n\
123 Like digest(), but returns the digest as a string of hexadecimal digits.");
126 static PyObject *
127 md5_copy(md5object *self)
129 md5object *md5p;
131 if ((md5p = newmd5object()) == NULL)
132 return NULL;
134 md5p->md5 = self->md5;
136 return (PyObject *)md5p;
139 PyDoc_STRVAR(copy_doc,
140 "copy() -> md5 object\n\
142 Return a copy (``clone'') of the md5 object.");
145 static PyMethodDef md5_methods[] = {
146 {"update", (PyCFunction)md5_update, METH_VARARGS, update_doc},
147 {"digest", (PyCFunction)md5_digest, METH_NOARGS, digest_doc},
148 {"hexdigest", (PyCFunction)md5_hexdigest, METH_NOARGS, hexdigest_doc},
149 {"copy", (PyCFunction)md5_copy, METH_NOARGS, copy_doc},
150 {NULL, NULL} /* sentinel */
153 static PyObject *
154 md5_get_block_size(PyObject *self, void *closure)
156 return PyInt_FromLong(64);
159 static PyObject *
160 md5_get_digest_size(PyObject *self, void *closure)
162 return PyInt_FromLong(16);
165 static PyObject *
166 md5_get_name(PyObject *self, void *closure)
168 return PyString_FromStringAndSize("MD5", 3);
171 static PyGetSetDef md5_getseters[] = {
172 {"digest_size",
173 (getter)md5_get_digest_size, NULL,
174 NULL,
175 NULL},
176 {"block_size",
177 (getter)md5_get_block_size, NULL,
178 NULL,
179 NULL},
180 {"name",
181 (getter)md5_get_name, NULL,
182 NULL,
183 NULL},
184 /* the old md5 and sha modules support 'digest_size' as in PEP 247.
185 * the old sha module also supported 'digestsize'. ugh. */
186 {"digestsize",
187 (getter)md5_get_digest_size, NULL,
188 NULL,
189 NULL},
190 {NULL} /* Sentinel */
194 PyDoc_STRVAR(module_doc,
195 "This module implements the interface to RSA's MD5 message digest\n\
196 algorithm (see also Internet RFC 1321). Its use is quite\n\
197 straightforward: use the new() to create an md5 object. You can now\n\
198 feed this object with arbitrary strings using the update() method, and\n\
199 at any point you can ask it for the digest (a strong kind of 128-bit\n\
200 checksum, a.k.a. ``fingerprint'') of the concatenation of the strings\n\
201 fed to it so far using the digest() method.\n\
203 Functions:\n\
205 new([arg]) -- return a new md5 object, initialized with arg if provided\n\
206 md5([arg]) -- DEPRECATED, same as new, but for compatibility\n\
208 Special Objects:\n\
210 MD5Type -- type object for md5 objects");
212 PyDoc_STRVAR(md5type_doc,
213 "An md5 represents the object used to calculate the MD5 checksum of a\n\
214 string of information.\n\
216 Methods:\n\
218 update() -- updates the current digest with an additional string\n\
219 digest() -- return the current digest value\n\
220 hexdigest() -- return the current digest as a string of hexadecimal digits\n\
221 copy() -- return a copy of the current md5 object");
223 static PyTypeObject MD5type = {
224 PyObject_HEAD_INIT(NULL)
225 0, /*ob_size*/
226 "_md5.md5", /*tp_name*/
227 sizeof(md5object), /*tp_size*/
228 0, /*tp_itemsize*/
229 /* methods */
230 (destructor)md5_dealloc, /*tp_dealloc*/
231 0, /*tp_print*/
232 0, /*tp_getattr*/
233 0, /*tp_setattr*/
234 0, /*tp_compare*/
235 0, /*tp_repr*/
236 0, /*tp_as_number*/
237 0, /*tp_as_sequence*/
238 0, /*tp_as_mapping*/
239 0, /*tp_hash*/
240 0, /*tp_call*/
241 0, /*tp_str*/
242 0, /*tp_getattro*/
243 0, /*tp_setattro*/
244 0, /*tp_as_buffer*/
245 Py_TPFLAGS_DEFAULT, /*tp_flags*/
246 md5type_doc, /*tp_doc*/
247 0, /*tp_traverse*/
248 0, /*tp_clear*/
249 0, /*tp_richcompare*/
250 0, /*tp_weaklistoffset*/
251 0, /*tp_iter*/
252 0, /*tp_iternext*/
253 md5_methods, /*tp_methods*/
254 0, /*tp_members*/
255 md5_getseters, /*tp_getset*/
259 /* MD5 functions */
261 static PyObject *
262 MD5_new(PyObject *self, PyObject *args)
264 md5object *md5p;
265 unsigned char *cp = NULL;
266 int len = 0;
268 if (!PyArg_ParseTuple(args, "|s#:new", &cp, &len))
269 return NULL;
271 if ((md5p = newmd5object()) == NULL)
272 return NULL;
274 if (cp)
275 MD5Update(&md5p->md5, cp, len);
277 return (PyObject *)md5p;
280 PyDoc_STRVAR(new_doc,
281 "new([arg]) -> md5 object\n\
283 Return a new md5 object. If arg is present, the method call update(arg)\n\
284 is made.");
287 /* List of functions exported by this module */
289 static PyMethodDef md5_functions[] = {
290 {"new", (PyCFunction)MD5_new, METH_VARARGS, new_doc},
291 {NULL, NULL} /* Sentinel */
295 /* Initialize this module. */
297 PyMODINIT_FUNC
298 init_md5(void)
300 PyObject *m, *d;
302 MD5type.ob_type = &PyType_Type;
303 if (PyType_Ready(&MD5type) < 0)
304 return;
305 m = Py_InitModule3("_md5", md5_functions, module_doc);
306 if (m == NULL)
307 return;
308 d = PyModule_GetDict(m);
309 PyDict_SetItemString(d, "MD5Type", (PyObject *)&MD5type);
310 PyModule_AddIntConstant(m, "digest_size", 16);
311 /* No need to check the error here, the caller will do that */