blob: f2653fbbf40766550303416380834e93c41a53ff [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
Barry Warsaw8b43b191996-12-09 22:32:36 +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{
Barry Warsaw8b43b191996-12-09 22:32:36 +000074 PyMem_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
Barry Warsaw8b43b191996-12-09 22:32:36 +000097static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +000098md5_digest(self, args)
99 md5object *self;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000100 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000101{
102
103 MD5_CTX mdContext;
104 unsigned char aDigest[16];
105
Barry Warsaw8b43b191996-12-09 22:32:36 +0000106 if (!PyArg_NoArgs(args))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000107 return NULL;
108
109 /* make a temporary copy, and perform the final */
110 mdContext = self->md5;
111 MD5Final(aDigest, &mdContext);
112
Barry Warsaw8b43b191996-12-09 22:32:36 +0000113 return PyString_FromStringAndSize((char *)aDigest, 16);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000114}
115
Barry Warsaw8b43b191996-12-09 22:32:36 +0000116static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000117md5_copy(self, args)
118 md5object *self;
Barry Warsaw8b43b191996-12-09 22:32:36 +0000119 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000120{
121 md5object *md5p;
122
Barry Warsaw8b43b191996-12-09 22:32:36 +0000123 if (!PyArg_NoArgs(args))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000124 return NULL;
125
126 if ((md5p = newmd5object()) == NULL)
127 return NULL;
128
129 md5p->md5 = self->md5;
130
Barry Warsaw8b43b191996-12-09 22:32:36 +0000131 return (PyObject *)md5p;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000132}
133
Barry Warsaw8b43b191996-12-09 22:32:36 +0000134static PyMethodDef md5_methods[] = {
135 {"update", (PyCFunction)md5_update},
136 {"digest", (PyCFunction)md5_digest},
137 {"copy", (PyCFunction)md5_copy},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000138 {NULL, NULL} /* sentinel */
139};
140
Barry Warsaw8b43b191996-12-09 22:32:36 +0000141static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000142md5_getattr(self, name)
143 md5object *self;
144 char *name;
145{
Barry Warsaw8b43b191996-12-09 22:32:36 +0000146 return Py_FindMethod(md5_methods, (PyObject *)self, name);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000147}
148
Barry Warsaw8b43b191996-12-09 22:32:36 +0000149statichere PyTypeObject MD5type = {
150 PyObject_HEAD_INIT(&PyType_Type)
151 0, /*ob_size*/
152 "md5", /*tp_name*/
153 sizeof(md5object), /*tp_size*/
154 0, /*tp_itemsize*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000155 /* methods */
Barry Warsaw8b43b191996-12-09 22:32:36 +0000156 (destructor)md5_dealloc, /*tp_dealloc*/
157 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000158 (getattrfunc)md5_getattr, /*tp_getattr*/
Barry Warsaw8b43b191996-12-09 22:32:36 +0000159 0, /*tp_setattr*/
160 0, /*tp_compare*/
161 0, /*tp_repr*/
162 0, /*tp_as_number*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000163};
164
165
166/* MD5 functions */
Guido van Rossum5f59d601992-12-14 16:59:51 +0000167
Barry Warsaw8b43b191996-12-09 22:32:36 +0000168static PyObject *
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000169MD5_new(self, args)
Barry Warsaw8b43b191996-12-09 22:32:36 +0000170 PyObject *self;
171 PyObject *args;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000172{
173 md5object *md5p;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000174 unsigned char *cp = NULL;
Guido van Rossumef38b781995-07-26 17:33:10 +0000175 int len = 0;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000176
Barry Warsaw8b43b191996-12-09 22:32:36 +0000177 if (!PyArg_ParseTuple(args, "|s#", &cp, &len))
Guido van Rossumef38b781995-07-26 17:33:10 +0000178 return NULL;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000179
180 if ((md5p = newmd5object()) == NULL)
181 return NULL;
182
183 if (cp)
184 MD5Update(&md5p->md5, cp, len);
185
Barry Warsaw8b43b191996-12-09 22:32:36 +0000186 return (PyObject *)md5p;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000187}
Guido van Rossum5f59d601992-12-14 16:59:51 +0000188
189
Guido van Rossum5f59d601992-12-14 16:59:51 +0000190/* List of functions exported by this module */
191
Barry Warsaw8b43b191996-12-09 22:32:36 +0000192static PyMethodDef md5_functions[] = {
193 {"new", (PyCFunction)MD5_new, 1},
194 {"md5", (PyCFunction)MD5_new, 1}, /* Backward compatibility */
Guido van Rossumef38b781995-07-26 17:33:10 +0000195 {NULL, NULL} /* Sentinel */
Guido van Rossum5f59d601992-12-14 16:59:51 +0000196};
197
198
199/* Initialize this module. */
200
201void
202initmd5()
203{
Barry Warsaw8b43b191996-12-09 22:32:36 +0000204 (void)Py_InitModule("md5", md5_functions);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000205}