Class around PixMap objects that allows more python-like access. By Joe Strout.
[python/dscho.git] / Modules / md5module.c
blob2508f52dff7a3244743a2697128f8a671e4efd01
1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
15 permission.
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 /* MD5 module */
34 /* This module provides an interface to the RSA Data Security,
35 Inc. MD5 Message-Digest Algorithm, described in RFC 1321.
36 It requires the files md5c.c and md5.h (which are slightly changed
37 from the versions in the RFC to avoid the "global.h" file.) */
40 /* MD5 objects */
42 #include "Python.h"
43 #include "md5.h"
45 typedef struct {
46 PyObject_HEAD
47 MD5_CTX md5; /* the context holder */
48 } md5object;
50 staticforward PyTypeObject MD5type;
52 #define is_md5object(v) ((v)->ob_type == &MD5type)
54 static md5object *
55 newmd5object()
57 md5object *md5p;
59 md5p = PyObject_NEW(md5object, &MD5type);
60 if (md5p == NULL)
61 return NULL;
63 MD5Init(&md5p->md5); /* actual initialisation */
64 return md5p;
68 /* MD5 methods */
70 static void
71 md5_dealloc(md5p)
72 md5object *md5p;
74 PyMem_DEL(md5p);
78 /* MD5 methods-as-attributes */
80 static PyObject *
81 md5_update(self, args)
82 md5object *self;
83 PyObject *args;
85 unsigned char *cp;
86 int len;
88 if (!PyArg_Parse(args, "s#", &cp, &len))
89 return NULL;
91 MD5Update(&self->md5, cp, len);
93 Py_INCREF(Py_None);
94 return Py_None;
97 static char update_doc [] =
98 "update (arg)\n\
99 \n\
100 Update the md5 object with the string arg. Repeated calls are\n\
101 equivalent to a single call with the concatenation of all the\n\
102 arguments.";
105 static PyObject *
106 md5_digest(self, args)
107 md5object *self;
108 PyObject *args;
111 MD5_CTX mdContext;
112 unsigned char aDigest[16];
114 if (!PyArg_NoArgs(args))
115 return NULL;
117 /* make a temporary copy, and perform the final */
118 mdContext = self->md5;
119 MD5Final(aDigest, &mdContext);
121 return PyString_FromStringAndSize((char *)aDigest, 16);
124 static char digest_doc [] =
125 "digest() -> string\n\
127 Return the digest of the strings passed to the update() method so\n\
128 far. This is an 16-byte string which may contain non-ASCII characters,\n\
129 including null bytes.";
132 static PyObject *
133 md5_copy(self, args)
134 md5object *self;
135 PyObject *args;
137 md5object *md5p;
139 if (!PyArg_NoArgs(args))
140 return NULL;
142 if ((md5p = newmd5object()) == NULL)
143 return NULL;
145 md5p->md5 = self->md5;
147 return (PyObject *)md5p;
150 static char copy_doc [] =
151 "copy() -> md5 object\n\
153 Return a copy (``clone'') of the md5 object.";
156 static PyMethodDef md5_methods[] = {
157 {"update", (PyCFunction)md5_update, 0, update_doc},
158 {"digest", (PyCFunction)md5_digest, 0, digest_doc},
159 {"copy", (PyCFunction)md5_copy, 0, copy_doc},
160 {NULL, NULL} /* sentinel */
163 static PyObject *
164 md5_getattr(self, name)
165 md5object *self;
166 char *name;
168 return Py_FindMethod(md5_methods, (PyObject *)self, name);
171 static char module_doc [] =
173 "This module implements the interface to RSA's MD5 message digest\n\
174 algorithm (see also Internet RFC 1321). Its use is quite\n\
175 straightforward: use the new() to create an md5 object. You can now\n\
176 feed this object with arbitrary strings using the update() method, and\n\
177 at any point you can ask it for the digest (a strong kind of 128-bit\n\
178 checksum, a.k.a. ``fingerprint'') of the contatenation of the strings\n\
179 fed to it so far using the digest() method.\n\
181 Functions:\n\
183 new([arg]) -- return a new md5 object, initialized with arg if provided\n\
184 md5([arg]) -- DEPRECATED, same as new, but for compatibility\n\
186 Special Objects:\n\
188 MD5Type -- type object for md5 objects\n\
191 static char md5type_doc [] =
192 "An md5 represents the object used to calculate the MD5 checksum of a\n\
193 string of information.\n\
195 Methods:\n\
197 update() -- updates the current digest with an additional string\n\
198 digest() -- return the current digest value\n\
199 copy() -- return a copy of the current md5 object\n\
202 statichere PyTypeObject MD5type = {
203 PyObject_HEAD_INIT(&PyType_Type)
204 0, /*ob_size*/
205 "md5", /*tp_name*/
206 sizeof(md5object), /*tp_size*/
207 0, /*tp_itemsize*/
208 /* methods */
209 (destructor)md5_dealloc, /*tp_dealloc*/
210 0, /*tp_print*/
211 (getattrfunc)md5_getattr, /*tp_getattr*/
212 0, /*tp_setattr*/
213 0, /*tp_compare*/
214 0, /*tp_repr*/
215 0, /*tp_as_number*/
216 0, /*tp_as_sequence*/
217 0, /*tp_as_mapping*/
218 0, /*tp_hash*/
219 0, /*tp_call*/
220 0, /*tp_str*/
221 0, /*tp_getattro*/
222 0, /*tp_setattro*/
223 0, /*tp_as_buffer*/
224 0, /*tp_xxx4*/
225 md5type_doc, /*tp_doc*/
229 /* MD5 functions */
231 static PyObject *
232 MD5_new(self, args)
233 PyObject *self;
234 PyObject *args;
236 md5object *md5p;
237 unsigned char *cp = NULL;
238 int len = 0;
240 if (!PyArg_ParseTuple(args, "|s#", &cp, &len))
241 return NULL;
243 if ((md5p = newmd5object()) == NULL)
244 return NULL;
246 if (cp)
247 MD5Update(&md5p->md5, cp, len);
249 return (PyObject *)md5p;
252 static char new_doc [] =
253 "new([arg]) -> md5 object\n\
255 Return a new md5 object. If arg is present, the method call update(arg)\n\
256 is made.";
259 /* List of functions exported by this module */
261 static PyMethodDef md5_functions[] = {
262 {"new", (PyCFunction)MD5_new, 1, new_doc},
263 {"md5", (PyCFunction)MD5_new, 1, new_doc}, /* Backward compatibility */
264 {NULL, NULL} /* Sentinel */
268 /* Initialize this module. */
270 DL_EXPORT(void)
271 initmd5()
273 PyObject *m, *d;
274 m = Py_InitModule3("md5", md5_functions, module_doc);
275 d = PyModule_GetDict(m);
276 PyDict_SetItemString(d, "MD5Type", (PyObject *)&MD5type);
277 /* No need to check the error here, the caller will do that */