Files for 2.1b1 distribution.
[python/dscho.git] / Modules / md5module.c
blob269a298c05ade1fa5da388a452fbb835dcfb572b
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 staticforward 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_Parse(args, "s#", &cp, &len))
56 return NULL;
58 MD5Update(&self->md5, cp, len);
60 Py_INCREF(Py_None);
61 return Py_None;
64 static char 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, PyObject *args)
75 MD5_CTX mdContext;
76 unsigned char aDigest[16];
78 if (!PyArg_NoArgs(args))
79 return NULL;
81 /* make a temporary copy, and perform the final */
82 mdContext = self->md5;
83 MD5Final(aDigest, &mdContext);
85 return PyString_FromStringAndSize((char *)aDigest, 16);
88 static char digest_doc [] =
89 "digest() -> string\n\
90 \n\
91 Return the digest of the strings passed to the update() method so\n\
92 far. This is an 16-byte string which may contain non-ASCII characters,\n\
93 including null bytes.";
96 static PyObject *
97 md5_hexdigest(md5object *self, PyObject *args)
99 MD5_CTX mdContext;
100 unsigned char digest[16];
101 unsigned char hexdigest[32];
102 int i, j;
104 if (!PyArg_NoArgs(args))
105 return NULL;
107 /* make a temporary copy, and perform the final */
108 mdContext = self->md5;
109 MD5Final(digest, &mdContext);
111 /* Make hex version of the digest */
112 for(i=j=0; i<16; i++) {
113 char c;
114 c = (digest[i] >> 4) & 0xf;
115 c = (c>9) ? c+'a'-10 : c + '0';
116 hexdigest[j++] = c;
117 c = (digest[i] & 0xf);
118 c = (c>9) ? c+'a'-10 : c + '0';
119 hexdigest[j++] = c;
121 return PyString_FromStringAndSize((char*)hexdigest, 32);
125 static char hexdigest_doc [] =
126 "hexdigest() -> string\n\
128 Like digest(), but returns the digest as a string of hexadecimal digits.";
131 static PyObject *
132 md5_copy(md5object *self, PyObject *args)
134 md5object *md5p;
136 if (!PyArg_NoArgs(args))
137 return NULL;
139 if ((md5p = newmd5object()) == NULL)
140 return NULL;
142 md5p->md5 = self->md5;
144 return (PyObject *)md5p;
147 static char copy_doc [] =
148 "copy() -> md5 object\n\
150 Return a copy (``clone'') of the md5 object.";
153 static PyMethodDef md5_methods[] = {
154 {"update", (PyCFunction)md5_update, METH_OLDARGS, update_doc},
155 {"digest", (PyCFunction)md5_digest, METH_OLDARGS, digest_doc},
156 {"hexdigest", (PyCFunction)md5_hexdigest, METH_OLDARGS, hexdigest_doc},
157 {"copy", (PyCFunction)md5_copy, METH_OLDARGS, copy_doc},
158 {NULL, NULL} /* sentinel */
161 static PyObject *
162 md5_getattr(md5object *self, char *name)
164 return Py_FindMethod(md5_methods, (PyObject *)self, name);
167 static char module_doc [] =
169 "This module implements the interface to RSA's MD5 message digest\n\
170 algorithm (see also Internet RFC 1321). Its use is quite\n\
171 straightforward: use the new() to create an md5 object. You can now\n\
172 feed this object with arbitrary strings using the update() method, and\n\
173 at any point you can ask it for the digest (a strong kind of 128-bit\n\
174 checksum, a.k.a. ``fingerprint'') of the concatenation of the strings\n\
175 fed to it so far using the digest() method.\n\
177 Functions:\n\
179 new([arg]) -- return a new md5 object, initialized with arg if provided\n\
180 md5([arg]) -- DEPRECATED, same as new, but for compatibility\n\
182 Special Objects:\n\
184 MD5Type -- type object for md5 objects\n\
187 static char md5type_doc [] =
188 "An md5 represents the object used to calculate the MD5 checksum of a\n\
189 string of information.\n\
191 Methods:\n\
193 update() -- updates the current digest with an additional string\n\
194 digest() -- return the current digest value\n\
195 copy() -- return a copy of the current md5 object\n\
198 statichere PyTypeObject MD5type = {
199 PyObject_HEAD_INIT(NULL)
200 0, /*ob_size*/
201 "md5", /*tp_name*/
202 sizeof(md5object), /*tp_size*/
203 0, /*tp_itemsize*/
204 /* methods */
205 (destructor)md5_dealloc, /*tp_dealloc*/
206 0, /*tp_print*/
207 (getattrfunc)md5_getattr, /*tp_getattr*/
208 0, /*tp_setattr*/
209 0, /*tp_compare*/
210 0, /*tp_repr*/
211 0, /*tp_as_number*/
212 0, /*tp_as_sequence*/
213 0, /*tp_as_mapping*/
214 0, /*tp_hash*/
215 0, /*tp_call*/
216 0, /*tp_str*/
217 0, /*tp_getattro*/
218 0, /*tp_setattro*/
219 0, /*tp_as_buffer*/
220 0, /*tp_xxx4*/
221 md5type_doc, /*tp_doc*/
225 /* MD5 functions */
227 static PyObject *
228 MD5_new(PyObject *self, PyObject *args)
230 md5object *md5p;
231 unsigned char *cp = NULL;
232 int len = 0;
234 if (!PyArg_ParseTuple(args, "|s#:new", &cp, &len))
235 return NULL;
237 if ((md5p = newmd5object()) == NULL)
238 return NULL;
240 if (cp)
241 MD5Update(&md5p->md5, cp, len);
243 return (PyObject *)md5p;
246 static char new_doc [] =
247 "new([arg]) -> md5 object\n\
249 Return a new md5 object. If arg is present, the method call update(arg)\n\
250 is made.";
253 /* List of functions exported by this module */
255 static PyMethodDef md5_functions[] = {
256 {"new", (PyCFunction)MD5_new, METH_VARARGS, new_doc},
257 {"md5", (PyCFunction)MD5_new, METH_VARARGS, new_doc}, /* Backward compatibility */
258 {NULL, NULL} /* Sentinel */
262 /* Initialize this module. */
264 DL_EXPORT(void)
265 initmd5(void)
267 PyObject *m, *d;
269 MD5type.ob_type = &PyType_Type;
270 m = Py_InitModule3("md5", md5_functions, module_doc);
271 d = PyModule_GetDict(m);
272 PyDict_SetItemString(d, "MD5Type", (PyObject *)&MD5type);
273 /* No need to check the error here, the caller will do that */