Update for release.
[python/dscho.git] / Modules / md5module.c
blob6c9f2c688635fc3dde2b3908218175ec003a17d0
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 "md5.h"
15 typedef struct {
16 PyObject_HEAD
17 MD5_CTX md5; /* the context holder */
18 } md5object;
20 static PyTypeObject MD5type;
22 #define is_md5object(v) ((v)->ob_type == &MD5type)
24 static md5object *
25 newmd5object(void)
27 md5object *md5p;
29 md5p = PyObject_New(md5object, &MD5type);
30 if (md5p == NULL)
31 return NULL;
33 MD5Init(&md5p->md5); /* actual initialisation */
34 return md5p;
38 /* MD5 methods */
40 static void
41 md5_dealloc(md5object *md5p)
43 PyObject_Del(md5p);
47 /* MD5 methods-as-attributes */
49 static PyObject *
50 md5_update(md5object *self, PyObject *args)
52 unsigned char *cp;
53 int len;
55 if (!PyArg_ParseTuple(args, "s#:update", &cp, &len))
56 return NULL;
58 MD5Update(&self->md5, cp, len);
60 Py_INCREF(Py_None);
61 return Py_None;
64 PyDoc_STRVAR(update_doc,
65 "update (arg)\n\
66 \n\
67 Update the md5 object with the string arg. Repeated calls are\n\
68 equivalent to a single call with the concatenation of all the\n\
69 arguments.");
72 static PyObject *
73 md5_digest(md5object *self)
75 MD5_CTX mdContext;
76 unsigned char aDigest[16];
78 /* make a temporary copy, and perform the final */
79 mdContext = self->md5;
80 MD5Final(aDigest, &mdContext);
82 return PyString_FromStringAndSize((char *)aDigest, 16);
85 PyDoc_STRVAR(digest_doc,
86 "digest() -> string\n\
87 \n\
88 Return the digest of the strings passed to the update() method so\n\
89 far. This is an 16-byte string which may contain non-ASCII characters,\n\
90 including null bytes.");
93 static PyObject *
94 md5_hexdigest(md5object *self)
96 MD5_CTX mdContext;
97 unsigned char digest[16];
98 unsigned char hexdigest[32];
99 int i, j;
101 /* make a temporary copy, and perform the final */
102 mdContext = self->md5;
103 MD5Final(digest, &mdContext);
105 /* Make hex version of the digest */
106 for(i=j=0; i<16; i++) {
107 char c;
108 c = (digest[i] >> 4) & 0xf;
109 c = (c>9) ? c+'a'-10 : c + '0';
110 hexdigest[j++] = c;
111 c = (digest[i] & 0xf);
112 c = (c>9) ? c+'a'-10 : c + '0';
113 hexdigest[j++] = c;
115 return PyString_FromStringAndSize((char*)hexdigest, 32);
119 PyDoc_STRVAR(hexdigest_doc,
120 "hexdigest() -> string\n\
122 Like digest(), but returns the digest as a string of hexadecimal digits.");
125 static PyObject *
126 md5_copy(md5object *self)
128 md5object *md5p;
130 if ((md5p = newmd5object()) == NULL)
131 return NULL;
133 md5p->md5 = self->md5;
135 return (PyObject *)md5p;
138 PyDoc_STRVAR(copy_doc,
139 "copy() -> md5 object\n\
141 Return a copy (``clone'') of the md5 object.");
144 static PyMethodDef md5_methods[] = {
145 {"update", (PyCFunction)md5_update, METH_VARARGS, update_doc},
146 {"digest", (PyCFunction)md5_digest, METH_NOARGS, digest_doc},
147 {"hexdigest", (PyCFunction)md5_hexdigest, METH_NOARGS, hexdigest_doc},
148 {"copy", (PyCFunction)md5_copy, METH_NOARGS, copy_doc},
149 {NULL, NULL} /* sentinel */
152 static PyObject *
153 md5_getattr(md5object *self, char *name)
155 if (strcmp(name, "digest_size") == 0) {
156 return PyInt_FromLong(16);
159 return Py_FindMethod(md5_methods, (PyObject *)self, name);
162 PyDoc_STRVAR(module_doc,
163 "This module implements the interface to RSA's MD5 message digest\n\
164 algorithm (see also Internet RFC 1321). Its use is quite\n\
165 straightforward: use the new() to create an md5 object. You can now\n\
166 feed this object with arbitrary strings using the update() method, and\n\
167 at any point you can ask it for the digest (a strong kind of 128-bit\n\
168 checksum, a.k.a. ``fingerprint'') of the concatenation of the strings\n\
169 fed to it so far using the digest() method.\n\
171 Functions:\n\
173 new([arg]) -- return a new md5 object, initialized with arg if provided\n\
174 md5([arg]) -- DEPRECATED, same as new, but for compatibility\n\
176 Special Objects:\n\
178 MD5Type -- type object for md5 objects");
180 PyDoc_STRVAR(md5type_doc,
181 "An md5 represents the object used to calculate the MD5 checksum of a\n\
182 string of information.\n\
184 Methods:\n\
186 update() -- updates the current digest with an additional string\n\
187 digest() -- return the current digest value\n\
188 hexdigest() -- return the current digest as a string of hexadecimal digits\n\
189 copy() -- return a copy of the current md5 object");
191 static PyTypeObject MD5type = {
192 PyObject_HEAD_INIT(NULL)
193 0, /*ob_size*/
194 "md5.md5", /*tp_name*/
195 sizeof(md5object), /*tp_size*/
196 0, /*tp_itemsize*/
197 /* methods */
198 (destructor)md5_dealloc, /*tp_dealloc*/
199 0, /*tp_print*/
200 (getattrfunc)md5_getattr, /*tp_getattr*/
201 0, /*tp_setattr*/
202 0, /*tp_compare*/
203 0, /*tp_repr*/
204 0, /*tp_as_number*/
205 0, /*tp_as_sequence*/
206 0, /*tp_as_mapping*/
207 0, /*tp_hash*/
208 0, /*tp_call*/
209 0, /*tp_str*/
210 0, /*tp_getattro*/
211 0, /*tp_setattro*/
212 0, /*tp_as_buffer*/
213 0, /*tp_xxx4*/
214 md5type_doc, /*tp_doc*/
218 /* MD5 functions */
220 static PyObject *
221 MD5_new(PyObject *self, PyObject *args)
223 md5object *md5p;
224 unsigned char *cp = NULL;
225 int len = 0;
227 if (!PyArg_ParseTuple(args, "|s#:new", &cp, &len))
228 return NULL;
230 if ((md5p = newmd5object()) == NULL)
231 return NULL;
233 if (cp)
234 MD5Update(&md5p->md5, cp, len);
236 return (PyObject *)md5p;
239 PyDoc_STRVAR(new_doc,
240 "new([arg]) -> md5 object\n\
242 Return a new md5 object. If arg is present, the method call update(arg)\n\
243 is made.");
246 /* List of functions exported by this module */
248 static PyMethodDef md5_functions[] = {
249 {"new", (PyCFunction)MD5_new, METH_VARARGS, new_doc},
250 {"md5", (PyCFunction)MD5_new, METH_VARARGS, new_doc}, /* Backward compatibility */
251 {NULL, NULL} /* Sentinel */
255 /* Initialize this module. */
257 PyMODINIT_FUNC
258 initmd5(void)
260 PyObject *m, *d;
262 MD5type.ob_type = &PyType_Type;
263 m = Py_InitModule3("md5", md5_functions, module_doc);
264 d = PyModule_GetDict(m);
265 PyDict_SetItemString(d, "MD5Type", (PyObject *)&MD5type);
266 PyModule_AddIntConstant(m, "digest_size", 16);
267 /* No need to check the error here, the caller will do that */