Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 524b588 | 1995-01-04 19:10:35 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 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. |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 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. |
| 20 | |
| 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. |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 31 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 32 | /* MD5 module */ |
| 33 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 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.) */ |
| 38 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 39 | |
| 40 | /* MD5 objects */ |
| 41 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 42 | #include "Python.h" |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 43 | #include "md5.h" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 45 | typedef struct { |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 46 | PyObject_HEAD |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 47 | MD5_CTX md5; /* the context holder */ |
| 48 | } md5object; |
| 49 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 50 | staticforward PyTypeObject MD5type; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 51 | |
| 52 | #define is_md5object(v) ((v)->ob_type == &MD5type) |
| 53 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 54 | static md5object * |
| 55 | newmd5object() |
| 56 | { |
| 57 | md5object *md5p; |
| 58 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 59 | md5p = PyObject_NEW(md5object, &MD5type); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 60 | if (md5p == NULL) |
| 61 | return NULL; |
| 62 | |
| 63 | MD5Init(&md5p->md5); /* actual initialisation */ |
| 64 | return md5p; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 65 | } |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 66 | |
| 67 | |
| 68 | /* MD5 methods */ |
| 69 | |
| 70 | static void |
| 71 | md5_dealloc(md5p) |
| 72 | md5object *md5p; |
| 73 | { |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 74 | PyMem_DEL(md5p); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 75 | } |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 76 | |
| 77 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 78 | /* MD5 methods-as-attributes */ |
| 79 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 80 | static PyObject * |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 81 | md5_update(self, args) |
| 82 | md5object *self; |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 83 | PyObject *args; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 84 | { |
| 85 | unsigned char *cp; |
| 86 | int len; |
| 87 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 88 | if (!PyArg_Parse(args, "s#", &cp, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 89 | return NULL; |
| 90 | |
| 91 | MD5Update(&self->md5, cp, len); |
| 92 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 93 | Py_INCREF(Py_None); |
| 94 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 97 | static PyObject * |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 98 | md5_digest(self, args) |
| 99 | md5object *self; |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 100 | PyObject *args; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 101 | { |
| 102 | |
| 103 | MD5_CTX mdContext; |
| 104 | unsigned char aDigest[16]; |
| 105 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 106 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 107 | return NULL; |
| 108 | |
| 109 | /* make a temporary copy, and perform the final */ |
| 110 | mdContext = self->md5; |
| 111 | MD5Final(aDigest, &mdContext); |
| 112 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 113 | return PyString_FromStringAndSize((char *)aDigest, 16); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 116 | static PyObject * |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 117 | md5_copy(self, args) |
| 118 | md5object *self; |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 119 | PyObject *args; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 120 | { |
| 121 | md5object *md5p; |
| 122 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 123 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 124 | return NULL; |
| 125 | |
| 126 | if ((md5p = newmd5object()) == NULL) |
| 127 | return NULL; |
| 128 | |
| 129 | md5p->md5 = self->md5; |
| 130 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 131 | return (PyObject *)md5p; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 134 | static PyMethodDef md5_methods[] = { |
| 135 | {"update", (PyCFunction)md5_update}, |
| 136 | {"digest", (PyCFunction)md5_digest}, |
| 137 | {"copy", (PyCFunction)md5_copy}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 138 | {NULL, NULL} /* sentinel */ |
| 139 | }; |
| 140 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 141 | static PyObject * |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 142 | md5_getattr(self, name) |
| 143 | md5object *self; |
| 144 | char *name; |
| 145 | { |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 146 | return Py_FindMethod(md5_methods, (PyObject *)self, name); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 149 | statichere 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 Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 155 | /* methods */ |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 156 | (destructor)md5_dealloc, /*tp_dealloc*/ |
| 157 | 0, /*tp_print*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 158 | (getattrfunc)md5_getattr, /*tp_getattr*/ |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 159 | 0, /*tp_setattr*/ |
| 160 | 0, /*tp_compare*/ |
| 161 | 0, /*tp_repr*/ |
| 162 | 0, /*tp_as_number*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | |
| 166 | /* MD5 functions */ |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 167 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 168 | static PyObject * |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 169 | MD5_new(self, args) |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 170 | PyObject *self; |
| 171 | PyObject *args; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 172 | { |
| 173 | md5object *md5p; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 174 | unsigned char *cp = NULL; |
Guido van Rossum | ef38b78 | 1995-07-26 17:33:10 +0000 | [diff] [blame] | 175 | int len = 0; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 176 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 177 | if (!PyArg_ParseTuple(args, "|s#", &cp, &len)) |
Guido van Rossum | ef38b78 | 1995-07-26 17:33:10 +0000 | [diff] [blame] | 178 | return NULL; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 179 | |
| 180 | if ((md5p = newmd5object()) == NULL) |
| 181 | return NULL; |
| 182 | |
| 183 | if (cp) |
| 184 | MD5Update(&md5p->md5, cp, len); |
| 185 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 186 | return (PyObject *)md5p; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 187 | } |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 188 | |
| 189 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 190 | /* List of functions exported by this module */ |
| 191 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 192 | static PyMethodDef md5_functions[] = { |
| 193 | {"new", (PyCFunction)MD5_new, 1}, |
| 194 | {"md5", (PyCFunction)MD5_new, 1}, /* Backward compatibility */ |
Guido van Rossum | ef38b78 | 1995-07-26 17:33:10 +0000 | [diff] [blame] | 195 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | |
| 199 | /* Initialize this module. */ |
| 200 | |
| 201 | void |
| 202 | initmd5() |
| 203 | { |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 204 | (void)Py_InitModule("md5", md5_functions); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 205 | } |