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 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +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 | { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 74 | PyObject_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 | |
Guido van Rossum | d3a6a14 | 1998-10-14 13:46:57 +0000 | [diff] [blame] | 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."; |
| 103 | |
| 104 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 105 | static PyObject * |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 106 | md5_digest(self, args) |
| 107 | md5object *self; |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 108 | PyObject *args; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 109 | { |
| 110 | |
| 111 | MD5_CTX mdContext; |
| 112 | unsigned char aDigest[16]; |
| 113 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 114 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 115 | return NULL; |
| 116 | |
| 117 | /* make a temporary copy, and perform the final */ |
| 118 | mdContext = self->md5; |
| 119 | MD5Final(aDigest, &mdContext); |
| 120 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 121 | return PyString_FromStringAndSize((char *)aDigest, 16); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Guido van Rossum | d3a6a14 | 1998-10-14 13:46:57 +0000 | [diff] [blame] | 124 | static char digest_doc [] = |
| 125 | "digest() -> string\n\ |
| 126 | \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."; |
| 130 | |
| 131 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 132 | static PyObject * |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 133 | md5_copy(self, args) |
| 134 | md5object *self; |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 135 | PyObject *args; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 136 | { |
| 137 | md5object *md5p; |
| 138 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 139 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 140 | return NULL; |
| 141 | |
| 142 | if ((md5p = newmd5object()) == NULL) |
| 143 | return NULL; |
| 144 | |
| 145 | md5p->md5 = self->md5; |
| 146 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 147 | return (PyObject *)md5p; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Guido van Rossum | d3a6a14 | 1998-10-14 13:46:57 +0000 | [diff] [blame] | 150 | static char copy_doc [] = |
| 151 | "copy() -> md5 object\n\ |
| 152 | \n\ |
| 153 | Return a copy (``clone'') of the md5 object."; |
| 154 | |
| 155 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 156 | static PyMethodDef md5_methods[] = { |
Guido van Rossum | d3a6a14 | 1998-10-14 13:46:57 +0000 | [diff] [blame] | 157 | {"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 Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 160 | {NULL, NULL} /* sentinel */ |
| 161 | }; |
| 162 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 163 | static PyObject * |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 164 | md5_getattr(self, name) |
| 165 | md5object *self; |
| 166 | char *name; |
| 167 | { |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 168 | return Py_FindMethod(md5_methods, (PyObject *)self, name); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Guido van Rossum | d3a6a14 | 1998-10-14 13:46:57 +0000 | [diff] [blame] | 171 | static char module_doc [] = |
| 172 | |
| 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\ |
| 180 | \n\ |
| 181 | Functions:\n\ |
| 182 | \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\ |
| 185 | \n\ |
| 186 | Special Objects:\n\ |
| 187 | \n\ |
| 188 | MD5Type -- type object for md5 objects\n\ |
| 189 | "; |
| 190 | |
| 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\ |
| 194 | \n\ |
| 195 | Methods:\n\ |
| 196 | \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\ |
| 200 | "; |
| 201 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 202 | statichere PyTypeObject MD5type = { |
Fred Drake | 0d40ba4 | 2000-02-04 20:33:49 +0000 | [diff] [blame] | 203 | PyObject_HEAD_INIT(NULL) |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 204 | 0, /*ob_size*/ |
| 205 | "md5", /*tp_name*/ |
| 206 | sizeof(md5object), /*tp_size*/ |
| 207 | 0, /*tp_itemsize*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 208 | /* methods */ |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 209 | (destructor)md5_dealloc, /*tp_dealloc*/ |
| 210 | 0, /*tp_print*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 211 | (getattrfunc)md5_getattr, /*tp_getattr*/ |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 212 | 0, /*tp_setattr*/ |
| 213 | 0, /*tp_compare*/ |
| 214 | 0, /*tp_repr*/ |
| 215 | 0, /*tp_as_number*/ |
Guido van Rossum | d3a6a14 | 1998-10-14 13:46:57 +0000 | [diff] [blame] | 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*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 226 | }; |
| 227 | |
| 228 | |
| 229 | /* MD5 functions */ |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 230 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 231 | static PyObject * |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 232 | MD5_new(self, args) |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 233 | PyObject *self; |
| 234 | PyObject *args; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 235 | { |
| 236 | md5object *md5p; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 237 | unsigned char *cp = NULL; |
Guido van Rossum | ef38b78 | 1995-07-26 17:33:10 +0000 | [diff] [blame] | 238 | int len = 0; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 239 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 240 | if (!PyArg_ParseTuple(args, "|s#:new", &cp, &len)) |
Guido van Rossum | ef38b78 | 1995-07-26 17:33:10 +0000 | [diff] [blame] | 241 | return NULL; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 242 | |
| 243 | if ((md5p = newmd5object()) == NULL) |
| 244 | return NULL; |
| 245 | |
| 246 | if (cp) |
| 247 | MD5Update(&md5p->md5, cp, len); |
| 248 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 249 | return (PyObject *)md5p; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 250 | } |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 251 | |
Guido van Rossum | d3a6a14 | 1998-10-14 13:46:57 +0000 | [diff] [blame] | 252 | static char new_doc [] = |
| 253 | "new([arg]) -> md5 object\n\ |
| 254 | \n\ |
| 255 | Return a new md5 object. If arg is present, the method call update(arg)\n\ |
| 256 | is made."; |
| 257 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 258 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 259 | /* List of functions exported by this module */ |
| 260 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 261 | static PyMethodDef md5_functions[] = { |
Guido van Rossum | d3a6a14 | 1998-10-14 13:46:57 +0000 | [diff] [blame] | 262 | {"new", (PyCFunction)MD5_new, 1, new_doc}, |
| 263 | {"md5", (PyCFunction)MD5_new, 1, new_doc}, /* Backward compatibility */ |
Guido van Rossum | ef38b78 | 1995-07-26 17:33:10 +0000 | [diff] [blame] | 264 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 265 | }; |
| 266 | |
| 267 | |
| 268 | /* Initialize this module. */ |
| 269 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 270 | DL_EXPORT(void) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 271 | initmd5() |
| 272 | { |
Guido van Rossum | d3a6a14 | 1998-10-14 13:46:57 +0000 | [diff] [blame] | 273 | PyObject *m, *d; |
Fred Drake | 0d40ba4 | 2000-02-04 20:33:49 +0000 | [diff] [blame] | 274 | |
| 275 | MD5type.ob_type = &PyType_Type; |
Guido van Rossum | d3a6a14 | 1998-10-14 13:46:57 +0000 | [diff] [blame] | 276 | 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 Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 280 | } |