Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 2 | Copyright (c) 2000, BeOpen.com. |
| 3 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 4 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 5 | All rights reserved. |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 11 | /* MD5 module */ |
| 12 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 13 | /* This module provides an interface to the RSA Data Security, |
| 14 | Inc. MD5 Message-Digest Algorithm, described in RFC 1321. |
| 15 | It requires the files md5c.c and md5.h (which are slightly changed |
| 16 | from the versions in the RFC to avoid the "global.h" file.) */ |
| 17 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 18 | |
| 19 | /* MD5 objects */ |
| 20 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 21 | #include "Python.h" |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 22 | #include "md5.h" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 23 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 24 | typedef struct { |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 25 | PyObject_HEAD |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 26 | MD5_CTX md5; /* the context holder */ |
| 27 | } md5object; |
| 28 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 29 | staticforward PyTypeObject MD5type; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 30 | |
| 31 | #define is_md5object(v) ((v)->ob_type == &MD5type) |
| 32 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 33 | static md5object * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 34 | newmd5object(void) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 35 | { |
| 36 | md5object *md5p; |
| 37 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 38 | md5p = PyObject_New(md5object, &MD5type); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 39 | if (md5p == NULL) |
| 40 | return NULL; |
| 41 | |
| 42 | MD5Init(&md5p->md5); /* actual initialisation */ |
| 43 | return md5p; |
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 | |
| 46 | |
| 47 | /* MD5 methods */ |
| 48 | |
| 49 | static void |
Peter Schneider-Kamp | 286da3b | 2000-07-10 12:43:58 +0000 | [diff] [blame] | 50 | md5_dealloc(md5object *md5p) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 51 | { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 52 | PyObject_Del(md5p); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 53 | } |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 54 | |
| 55 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 56 | /* MD5 methods-as-attributes */ |
| 57 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 58 | static PyObject * |
Peter Schneider-Kamp | 286da3b | 2000-07-10 12:43:58 +0000 | [diff] [blame] | 59 | md5_update(md5object *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 60 | { |
| 61 | unsigned char *cp; |
| 62 | int len; |
| 63 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 64 | if (!PyArg_Parse(args, "s#", &cp, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 65 | return NULL; |
| 66 | |
| 67 | MD5Update(&self->md5, cp, len); |
| 68 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 69 | Py_INCREF(Py_None); |
| 70 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Guido van Rossum | d3a6a14 | 1998-10-14 13:46:57 +0000 | [diff] [blame] | 73 | static char update_doc [] = |
| 74 | "update (arg)\n\ |
| 75 | \n\ |
| 76 | Update the md5 object with the string arg. Repeated calls are\n\ |
| 77 | equivalent to a single call with the concatenation of all the\n\ |
| 78 | arguments."; |
| 79 | |
| 80 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 81 | static PyObject * |
Peter Schneider-Kamp | 286da3b | 2000-07-10 12:43:58 +0000 | [diff] [blame] | 82 | md5_digest(md5object *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 83 | { |
Barry Warsaw | 3ec0fbb | 2000-08-15 05:59:44 +0000 | [diff] [blame] | 84 | MD5_CTX mdContext; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 85 | unsigned char aDigest[16]; |
| 86 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 87 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 88 | return NULL; |
| 89 | |
| 90 | /* make a temporary copy, and perform the final */ |
| 91 | mdContext = self->md5; |
| 92 | MD5Final(aDigest, &mdContext); |
| 93 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 94 | return PyString_FromStringAndSize((char *)aDigest, 16); |
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 digest_doc [] = |
| 98 | "digest() -> string\n\ |
| 99 | \n\ |
| 100 | Return the digest of the strings passed to the update() method so\n\ |
| 101 | far. This is an 16-byte string which may contain non-ASCII characters,\n\ |
| 102 | including null bytes."; |
| 103 | |
| 104 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 105 | static PyObject * |
Barry Warsaw | 3ec0fbb | 2000-08-15 05:59:44 +0000 | [diff] [blame] | 106 | md5_hexdigest(md5object *self, PyObject *args) |
| 107 | { |
| 108 | MD5_CTX mdContext; |
| 109 | unsigned char digest[16]; |
| 110 | unsigned char hexdigest[32]; |
| 111 | int i, j; |
| 112 | |
| 113 | if (!PyArg_NoArgs(args)) |
| 114 | return NULL; |
| 115 | |
| 116 | /* make a temporary copy, and perform the final */ |
| 117 | mdContext = self->md5; |
| 118 | MD5Final(digest, &mdContext); |
| 119 | |
| 120 | /* Make hex version of the digest */ |
| 121 | for(i=j=0; i<16; i++) { |
| 122 | char c; |
| 123 | c = (digest[i] >> 4) & 0xf; |
| 124 | c = (c>9) ? c+'a'-10 : c + '0'; |
| 125 | hexdigest[j++] = c; |
| 126 | c = (digest[i] & 0xf); |
| 127 | c = (c>9) ? c+'a'-10 : c + '0'; |
| 128 | hexdigest[j++] = c; |
| 129 | } |
| 130 | return PyString_FromStringAndSize((char*)hexdigest, 32); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | static char hexdigest_doc [] = |
| 135 | "hexdigest() -> string\n\ |
| 136 | \n\ |
| 137 | Like digest(), but returns the digest as a string of hexadecimal digits."; |
| 138 | |
| 139 | |
| 140 | static PyObject * |
Peter Schneider-Kamp | 286da3b | 2000-07-10 12:43:58 +0000 | [diff] [blame] | 141 | md5_copy(md5object *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 142 | { |
| 143 | md5object *md5p; |
| 144 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 145 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 146 | return NULL; |
| 147 | |
| 148 | if ((md5p = newmd5object()) == NULL) |
| 149 | return NULL; |
| 150 | |
| 151 | md5p->md5 = self->md5; |
| 152 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 153 | return (PyObject *)md5p; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Guido van Rossum | d3a6a14 | 1998-10-14 13:46:57 +0000 | [diff] [blame] | 156 | static char copy_doc [] = |
| 157 | "copy() -> md5 object\n\ |
| 158 | \n\ |
| 159 | Return a copy (``clone'') of the md5 object."; |
| 160 | |
| 161 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 162 | static PyMethodDef md5_methods[] = { |
Barry Warsaw | 3ec0fbb | 2000-08-15 05:59:44 +0000 | [diff] [blame] | 163 | {"update", (PyCFunction)md5_update, METH_OLDARGS, update_doc}, |
| 164 | {"digest", (PyCFunction)md5_digest, METH_OLDARGS, digest_doc}, |
| 165 | {"hexdigest", (PyCFunction)md5_hexdigest, METH_OLDARGS, hexdigest_doc}, |
| 166 | {"copy", (PyCFunction)md5_copy, METH_OLDARGS, copy_doc}, |
| 167 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 168 | }; |
| 169 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 170 | static PyObject * |
Peter Schneider-Kamp | 286da3b | 2000-07-10 12:43:58 +0000 | [diff] [blame] | 171 | md5_getattr(md5object *self, char *name) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 172 | { |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 173 | return Py_FindMethod(md5_methods, (PyObject *)self, name); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Guido van Rossum | d3a6a14 | 1998-10-14 13:46:57 +0000 | [diff] [blame] | 176 | static char module_doc [] = |
| 177 | |
| 178 | "This module implements the interface to RSA's MD5 message digest\n\ |
| 179 | algorithm (see also Internet RFC 1321). Its use is quite\n\ |
| 180 | straightforward: use the new() to create an md5 object. You can now\n\ |
| 181 | feed this object with arbitrary strings using the update() method, and\n\ |
| 182 | at any point you can ask it for the digest (a strong kind of 128-bit\n\ |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 183 | checksum, a.k.a. ``fingerprint'') of the concatenation of the strings\n\ |
Guido van Rossum | d3a6a14 | 1998-10-14 13:46:57 +0000 | [diff] [blame] | 184 | fed to it so far using the digest() method.\n\ |
| 185 | \n\ |
| 186 | Functions:\n\ |
| 187 | \n\ |
| 188 | new([arg]) -- return a new md5 object, initialized with arg if provided\n\ |
| 189 | md5([arg]) -- DEPRECATED, same as new, but for compatibility\n\ |
| 190 | \n\ |
| 191 | Special Objects:\n\ |
| 192 | \n\ |
| 193 | MD5Type -- type object for md5 objects\n\ |
| 194 | "; |
| 195 | |
| 196 | static char md5type_doc [] = |
| 197 | "An md5 represents the object used to calculate the MD5 checksum of a\n\ |
| 198 | string of information.\n\ |
| 199 | \n\ |
| 200 | Methods:\n\ |
| 201 | \n\ |
| 202 | update() -- updates the current digest with an additional string\n\ |
| 203 | digest() -- return the current digest value\n\ |
| 204 | copy() -- return a copy of the current md5 object\n\ |
| 205 | "; |
| 206 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 207 | statichere PyTypeObject MD5type = { |
Fred Drake | 0d40ba4 | 2000-02-04 20:33:49 +0000 | [diff] [blame] | 208 | PyObject_HEAD_INIT(NULL) |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 209 | 0, /*ob_size*/ |
| 210 | "md5", /*tp_name*/ |
| 211 | sizeof(md5object), /*tp_size*/ |
| 212 | 0, /*tp_itemsize*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 213 | /* methods */ |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 214 | (destructor)md5_dealloc, /*tp_dealloc*/ |
| 215 | 0, /*tp_print*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 216 | (getattrfunc)md5_getattr, /*tp_getattr*/ |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 217 | 0, /*tp_setattr*/ |
| 218 | 0, /*tp_compare*/ |
| 219 | 0, /*tp_repr*/ |
| 220 | 0, /*tp_as_number*/ |
Guido van Rossum | d3a6a14 | 1998-10-14 13:46:57 +0000 | [diff] [blame] | 221 | 0, /*tp_as_sequence*/ |
| 222 | 0, /*tp_as_mapping*/ |
| 223 | 0, /*tp_hash*/ |
| 224 | 0, /*tp_call*/ |
| 225 | 0, /*tp_str*/ |
| 226 | 0, /*tp_getattro*/ |
| 227 | 0, /*tp_setattro*/ |
| 228 | 0, /*tp_as_buffer*/ |
| 229 | 0, /*tp_xxx4*/ |
| 230 | md5type_doc, /*tp_doc*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 231 | }; |
| 232 | |
| 233 | |
| 234 | /* MD5 functions */ |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 235 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 236 | static PyObject * |
Peter Schneider-Kamp | 286da3b | 2000-07-10 12:43:58 +0000 | [diff] [blame] | 237 | MD5_new(PyObject *self, PyObject *args) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 238 | { |
| 239 | md5object *md5p; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 240 | unsigned char *cp = NULL; |
Guido van Rossum | ef38b78 | 1995-07-26 17:33:10 +0000 | [diff] [blame] | 241 | int len = 0; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 242 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 243 | if (!PyArg_ParseTuple(args, "|s#:new", &cp, &len)) |
Guido van Rossum | ef38b78 | 1995-07-26 17:33:10 +0000 | [diff] [blame] | 244 | return NULL; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 245 | |
| 246 | if ((md5p = newmd5object()) == NULL) |
| 247 | return NULL; |
| 248 | |
| 249 | if (cp) |
| 250 | MD5Update(&md5p->md5, cp, len); |
| 251 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 252 | return (PyObject *)md5p; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 253 | } |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 254 | |
Guido van Rossum | d3a6a14 | 1998-10-14 13:46:57 +0000 | [diff] [blame] | 255 | static char new_doc [] = |
| 256 | "new([arg]) -> md5 object\n\ |
| 257 | \n\ |
| 258 | Return a new md5 object. If arg is present, the method call update(arg)\n\ |
| 259 | is made."; |
| 260 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 261 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 262 | /* List of functions exported by this module */ |
| 263 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 264 | static PyMethodDef md5_functions[] = { |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 265 | {"new", (PyCFunction)MD5_new, METH_VARARGS, new_doc}, |
| 266 | {"md5", (PyCFunction)MD5_new, METH_VARARGS, new_doc}, /* Backward compatibility */ |
Guido van Rossum | ef38b78 | 1995-07-26 17:33:10 +0000 | [diff] [blame] | 267 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 268 | }; |
| 269 | |
| 270 | |
| 271 | /* Initialize this module. */ |
| 272 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 273 | DL_EXPORT(void) |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 274 | initmd5(void) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 275 | { |
Guido van Rossum | d3a6a14 | 1998-10-14 13:46:57 +0000 | [diff] [blame] | 276 | PyObject *m, *d; |
Fred Drake | 0d40ba4 | 2000-02-04 20:33:49 +0000 | [diff] [blame] | 277 | |
| 278 | MD5type.ob_type = &PyType_Type; |
Guido van Rossum | d3a6a14 | 1998-10-14 13:46:57 +0000 | [diff] [blame] | 279 | m = Py_InitModule3("md5", md5_functions, module_doc); |
| 280 | d = PyModule_GetDict(m); |
| 281 | PyDict_SetItemString(d, "MD5Type", (PyObject *)&MD5type); |
| 282 | /* 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] | 283 | } |