blob: 6856c68773f5c6e6e17c62f5a364a911d1d1bafe [file] [log] [blame]
Guido van Rossum5f59d601992-12-14 16:59:51 +00001/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossum5f59d601992-12-14 16:59:51 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossum5f59d601992-12-14 16:59:51 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossum5f59d601992-12-14 16:59:51 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossum5f59d601992-12-14 16:59:51 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossum5f59d601992-12-14 16:59:51 +000029
30******************************************************************/
Guido van Rossumb6775db1994-08-01 11:34:53 +000031
Guido van Rossum5f59d601992-12-14 16:59:51 +000032/* MD5 module */
33
Guido van Rossumb6775db1994-08-01 11:34:53 +000034/* 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.) */
38
Guido van Rossum5f59d601992-12-14 16:59:51 +000039
40/* MD5 objects */
41
Barry Warsaw8b43b191996-12-09 22:32:36 +000042#include "Python.h"
Guido van Rossum5f59d601992-12-14 16:59:51 +000043#include "md5.h"
Guido van Rossumb6775db1994-08-01 11:34:53 +000044
Guido van Rossum5f59d601992-12-14 16:59:51 +000045typedef struct {
Barry Warsaw8b43b191996-12-09 22:32:36 +000046 PyObject_HEAD
Guido van Rossum5f59d601992-12-14 16:59:51 +000047 MD5_CTX md5; /* the context holder */
48} md5object;
49
Barry Warsaw8b43b191996-12-09 22:32:36 +000050staticforward PyTypeObject MD5type;
Guido van Rossum5f59d601992-12-14 16:59:51 +000051
52#define is_md5object(v) ((v)->ob_type == &MD5type)
53
Guido van Rossum5f59d601992-12-14 16:59:51 +000054static md5object *
55newmd5object()
56{
57 md5object *md5p;
58
Guido van Rossumb18618d2000-05-03 23:44:39 +000059 md5p = PyObject_New(md5object, &MD5type);
Guido van Rossum5f59d601992-12-14 16:59:51 +000060 if (md5p == NULL)
61 return NULL;
62
63 MD5Init(&md5p->md5); /* actual initialisation */
64 return md5p;
Guido van Rossumb6775db1994-08-01 11:34:53 +000065}
Guido van Rossum5f59d601992-12-14 16:59:51 +000066
67
68/* MD5 methods */
69
70static void
71md5_dealloc(md5p)
72 md5object *md5p;
73{
Guido van Rossumb18618d2000-05-03 23:44:39 +000074 PyObject_Del(md5p);
Guido van Rossumb6775db1994-08-01 11:34:53 +000075}
Guido van Rossum5f59d601992-12-14 16:59:51 +000076
77
Guido van Rossumb6775db1994-08-01 11:34:53 +000078/* MD5 methods-as-attributes */
79
Barry Warsaw8b43b191996-12-09 22:32:36 +000080static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +000081md5_update(self, args)
82 md5object *self;
Barry Warsaw8b43b191996-12-09 22:32:36 +000083 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +000084{
85 unsigned char *cp;
86 int len;
87
Barry Warsaw8b43b191996-12-09 22:32:36 +000088 if (!PyArg_Parse(args, "s#", &cp, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +000089 return NULL;
90
91 MD5Update(&self->md5, cp, len);
92
Barry Warsaw8b43b191996-12-09 22:32:36 +000093 Py_INCREF(Py_None);
94 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +000095}
96
Guido van Rossumd3a6a141998-10-14 13:46:57 +000097static char update_doc [] =
98"update (arg)\n\
99\n\
100Update the md5 object with the string arg. Repeated calls are\n\
101equivalent to a single call with the concatenation of all the\n\
102arguments.";
103
104
Barry Warsaw8b43b191996-12-09 22:32:36 +0000105static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000106md5_digest(self, args)
107 md5object *self;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000108 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000109{
110
111 MD5_CTX mdContext;
112 unsigned char aDigest[16];
113
Barry Warsaw8b43b191996-12-09 22:32:36 +0000114 if (!PyArg_NoArgs(args))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000115 return NULL;
116
117 /* make a temporary copy, and perform the final */
118 mdContext = self->md5;
119 MD5Final(aDigest, &mdContext);
120
Barry Warsaw8b43b191996-12-09 22:32:36 +0000121 return PyString_FromStringAndSize((char *)aDigest, 16);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000122}
123
Guido van Rossumd3a6a141998-10-14 13:46:57 +0000124static char digest_doc [] =
125"digest() -> string\n\
126\n\
127Return the digest of the strings passed to the update() method so\n\
128far. This is an 16-byte string which may contain non-ASCII characters,\n\
129including null bytes.";
130
131
Barry Warsaw8b43b191996-12-09 22:32:36 +0000132static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000133md5_copy(self, args)
134 md5object *self;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000135 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000136{
137 md5object *md5p;
138
Barry Warsaw8b43b191996-12-09 22:32:36 +0000139 if (!PyArg_NoArgs(args))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000140 return NULL;
141
142 if ((md5p = newmd5object()) == NULL)
143 return NULL;
144
145 md5p->md5 = self->md5;
146
Barry Warsaw8b43b191996-12-09 22:32:36 +0000147 return (PyObject *)md5p;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000148}
149
Guido van Rossumd3a6a141998-10-14 13:46:57 +0000150static char copy_doc [] =
151"copy() -> md5 object\n\
152\n\
153Return a copy (``clone'') of the md5 object.";
154
155
Barry Warsaw8b43b191996-12-09 22:32:36 +0000156static PyMethodDef md5_methods[] = {
Guido van Rossumd3a6a141998-10-14 13:46:57 +0000157 {"update", (PyCFunction)md5_update, 0, update_doc},
158 {"digest", (PyCFunction)md5_digest, 0, digest_doc},
159 {"copy", (PyCFunction)md5_copy, 0, copy_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000160 {NULL, NULL} /* sentinel */
161};
162
Barry Warsaw8b43b191996-12-09 22:32:36 +0000163static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000164md5_getattr(self, name)
165 md5object *self;
166 char *name;
167{
Barry Warsaw8b43b191996-12-09 22:32:36 +0000168 return Py_FindMethod(md5_methods, (PyObject *)self, name);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000169}
170
Guido van Rossumd3a6a141998-10-14 13:46:57 +0000171static char module_doc [] =
172
173"This module implements the interface to RSA's MD5 message digest\n\
174algorithm (see also Internet RFC 1321). Its use is quite\n\
175straightforward: use the new() to create an md5 object. You can now\n\
176feed this object with arbitrary strings using the update() method, and\n\
177at any point you can ask it for the digest (a strong kind of 128-bit\n\
178checksum, a.k.a. ``fingerprint'') of the contatenation of the strings\n\
179fed to it so far using the digest() method.\n\
180\n\
181Functions:\n\
182\n\
183new([arg]) -- return a new md5 object, initialized with arg if provided\n\
184md5([arg]) -- DEPRECATED, same as new, but for compatibility\n\
185\n\
186Special Objects:\n\
187\n\
188MD5Type -- type object for md5 objects\n\
189";
190
191static char md5type_doc [] =
192"An md5 represents the object used to calculate the MD5 checksum of a\n\
193string of information.\n\
194\n\
195Methods:\n\
196\n\
197update() -- updates the current digest with an additional string\n\
198digest() -- return the current digest value\n\
199copy() -- return a copy of the current md5 object\n\
200";
201
Barry Warsaw8b43b191996-12-09 22:32:36 +0000202statichere PyTypeObject MD5type = {
Fred Drake0d40ba42000-02-04 20:33:49 +0000203 PyObject_HEAD_INIT(NULL)
Barry Warsaw8b43b191996-12-09 22:32:36 +0000204 0, /*ob_size*/
205 "md5", /*tp_name*/
206 sizeof(md5object), /*tp_size*/
207 0, /*tp_itemsize*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000208 /* methods */
Barry Warsaw8b43b191996-12-09 22:32:36 +0000209 (destructor)md5_dealloc, /*tp_dealloc*/
210 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000211 (getattrfunc)md5_getattr, /*tp_getattr*/
Barry Warsaw8b43b191996-12-09 22:32:36 +0000212 0, /*tp_setattr*/
213 0, /*tp_compare*/
214 0, /*tp_repr*/
215 0, /*tp_as_number*/
Guido van Rossumd3a6a141998-10-14 13:46:57 +0000216 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*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000226};
227
228
229/* MD5 functions */
Guido van Rossum5f59d601992-12-14 16:59:51 +0000230
Barry Warsaw8b43b191996-12-09 22:32:36 +0000231static PyObject *
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000232MD5_new(self, args)
Barry Warsaw8b43b191996-12-09 22:32:36 +0000233 PyObject *self;
234 PyObject *args;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000235{
236 md5object *md5p;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000237 unsigned char *cp = NULL;
Guido van Rossumef38b781995-07-26 17:33:10 +0000238 int len = 0;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000239
Guido van Rossum43713e52000-02-29 13:59:29 +0000240 if (!PyArg_ParseTuple(args, "|s#:new", &cp, &len))
Guido van Rossumef38b781995-07-26 17:33:10 +0000241 return NULL;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000242
243 if ((md5p = newmd5object()) == NULL)
244 return NULL;
245
246 if (cp)
247 MD5Update(&md5p->md5, cp, len);
248
Barry Warsaw8b43b191996-12-09 22:32:36 +0000249 return (PyObject *)md5p;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000250}
Guido van Rossum5f59d601992-12-14 16:59:51 +0000251
Guido van Rossumd3a6a141998-10-14 13:46:57 +0000252static char new_doc [] =
253"new([arg]) -> md5 object\n\
254\n\
255Return a new md5 object. If arg is present, the method call update(arg)\n\
256is made.";
257
Guido van Rossum5f59d601992-12-14 16:59:51 +0000258
Guido van Rossum5f59d601992-12-14 16:59:51 +0000259/* List of functions exported by this module */
260
Barry Warsaw8b43b191996-12-09 22:32:36 +0000261static PyMethodDef md5_functions[] = {
Guido van Rossumd3a6a141998-10-14 13:46:57 +0000262 {"new", (PyCFunction)MD5_new, 1, new_doc},
263 {"md5", (PyCFunction)MD5_new, 1, new_doc}, /* Backward compatibility */
Guido van Rossumef38b781995-07-26 17:33:10 +0000264 {NULL, NULL} /* Sentinel */
Guido van Rossum5f59d601992-12-14 16:59:51 +0000265};
266
267
268/* Initialize this module. */
269
Guido van Rossum3886bb61998-12-04 18:50:17 +0000270DL_EXPORT(void)
Guido van Rossum5f59d601992-12-14 16:59:51 +0000271initmd5()
272{
Guido van Rossumd3a6a141998-10-14 13:46:57 +0000273 PyObject *m, *d;
Fred Drake0d40ba42000-02-04 20:33:49 +0000274
275 MD5type.ob_type = &PyType_Type;
Guido van Rossumd3a6a141998-10-14 13:46:57 +0000276 m = Py_InitModule3("md5", md5_functions, module_doc);
277 d = PyModule_GetDict(m);
278 PyDict_SetItemString(d, "MD5Type", (PyObject *)&MD5type);
279 /* No need to check the error here, the caller will do that */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000280}