Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 2 | /* MD5 module */ |
| 3 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4 | /* This module provides an interface to the RSA Data Security, |
| 5 | Inc. MD5 Message-Digest Algorithm, described in RFC 1321. |
| 6 | It requires the files md5c.c and md5.h (which are slightly changed |
| 7 | from the versions in the RFC to avoid the "global.h" file.) */ |
| 8 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 9 | |
| 10 | /* MD5 objects */ |
| 11 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 12 | #include "Python.h" |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 13 | #include "structmember.h" |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 14 | #include "md5.h" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 15 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 16 | typedef struct { |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 17 | PyObject_HEAD |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 18 | md5_state_t md5; /* the context holder */ |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 19 | } md5object; |
| 20 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 21 | static PyTypeObject MD5type; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 22 | |
| 23 | #define is_md5object(v) ((v)->ob_type == &MD5type) |
| 24 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 25 | static md5object * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 26 | newmd5object(void) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 27 | { |
| 28 | md5object *md5p; |
| 29 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 30 | md5p = PyObject_New(md5object, &MD5type); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 31 | if (md5p == NULL) |
| 32 | return NULL; |
| 33 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 34 | md5_init(&md5p->md5); /* actual initialisation */ |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 35 | return md5p; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 36 | } |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 37 | |
| 38 | |
| 39 | /* MD5 methods */ |
| 40 | |
| 41 | static void |
Peter Schneider-Kamp | 286da3b | 2000-07-10 12:43:58 +0000 | [diff] [blame] | 42 | md5_dealloc(md5object *md5p) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 43 | { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 44 | PyObject_Del(md5p); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 45 | } |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 46 | |
| 47 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 48 | /* MD5 methods-as-attributes */ |
| 49 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 50 | static PyObject * |
Peter Schneider-Kamp | 286da3b | 2000-07-10 12:43:58 +0000 | [diff] [blame] | 51 | md5_update(md5object *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 52 | { |
| 53 | unsigned char *cp; |
| 54 | int len; |
| 55 | |
Neal Norwitz | ba3a16c | 2002-03-31 15:27:00 +0000 | [diff] [blame] | 56 | if (!PyArg_ParseTuple(args, "s#:update", &cp, &len)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 57 | return NULL; |
| 58 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 59 | md5_append(&self->md5, cp, len); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 60 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 61 | Py_INCREF(Py_None); |
| 62 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 65 | PyDoc_STRVAR(update_doc, |
Guido van Rossum | d3a6a14 | 1998-10-14 13:46:57 +0000 | [diff] [blame] | 66 | "update (arg)\n\ |
| 67 | \n\ |
| 68 | Update the md5 object with the string arg. Repeated calls are\n\ |
| 69 | equivalent to a single call with the concatenation of all the\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 70 | arguments."); |
Guido van Rossum | d3a6a14 | 1998-10-14 13:46:57 +0000 | [diff] [blame] | 71 | |
| 72 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 73 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 74 | md5_digest(md5object *self) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 75 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 76 | md5_state_t mdContext; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 77 | unsigned char aDigest[16]; |
| 78 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 79 | /* make a temporary copy, and perform the final */ |
| 80 | mdContext = self->md5; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 81 | md5_finish(&mdContext, aDigest); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 82 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 83 | return PyString_FromStringAndSize((char *)aDigest, 16); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 86 | PyDoc_STRVAR(digest_doc, |
Guido van Rossum | d3a6a14 | 1998-10-14 13:46:57 +0000 | [diff] [blame] | 87 | "digest() -> string\n\ |
| 88 | \n\ |
| 89 | Return the digest of the strings passed to the update() method so\n\ |
Skip Montanaro | bd2de3a | 2003-07-08 21:17:25 +0000 | [diff] [blame] | 90 | far. This is a 16-byte string which may contain non-ASCII characters,\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 91 | including null bytes."); |
Guido van Rossum | d3a6a14 | 1998-10-14 13:46:57 +0000 | [diff] [blame] | 92 | |
| 93 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 94 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 95 | md5_hexdigest(md5object *self) |
Barry Warsaw | 3ec0fbb | 2000-08-15 05:59:44 +0000 | [diff] [blame] | 96 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 97 | md5_state_t mdContext; |
Barry Warsaw | 3ec0fbb | 2000-08-15 05:59:44 +0000 | [diff] [blame] | 98 | unsigned char digest[16]; |
| 99 | unsigned char hexdigest[32]; |
| 100 | int i, j; |
| 101 | |
Barry Warsaw | 3ec0fbb | 2000-08-15 05:59:44 +0000 | [diff] [blame] | 102 | /* make a temporary copy, and perform the final */ |
| 103 | mdContext = self->md5; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 104 | md5_finish(&mdContext, digest); |
Barry Warsaw | 3ec0fbb | 2000-08-15 05:59:44 +0000 | [diff] [blame] | 105 | |
| 106 | /* Make hex version of the digest */ |
| 107 | for(i=j=0; i<16; i++) { |
| 108 | char c; |
| 109 | c = (digest[i] >> 4) & 0xf; |
| 110 | c = (c>9) ? c+'a'-10 : c + '0'; |
| 111 | hexdigest[j++] = c; |
| 112 | c = (digest[i] & 0xf); |
| 113 | c = (c>9) ? c+'a'-10 : c + '0'; |
| 114 | hexdigest[j++] = c; |
| 115 | } |
| 116 | return PyString_FromStringAndSize((char*)hexdigest, 32); |
| 117 | } |
| 118 | |
| 119 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 120 | PyDoc_STRVAR(hexdigest_doc, |
Barry Warsaw | 3ec0fbb | 2000-08-15 05:59:44 +0000 | [diff] [blame] | 121 | "hexdigest() -> string\n\ |
| 122 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 123 | Like digest(), but returns the digest as a string of hexadecimal digits."); |
Barry Warsaw | 3ec0fbb | 2000-08-15 05:59:44 +0000 | [diff] [blame] | 124 | |
| 125 | |
| 126 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 127 | md5_copy(md5object *self) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 128 | { |
| 129 | md5object *md5p; |
| 130 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 131 | if ((md5p = newmd5object()) == NULL) |
| 132 | return NULL; |
| 133 | |
| 134 | md5p->md5 = self->md5; |
| 135 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 136 | return (PyObject *)md5p; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 139 | PyDoc_STRVAR(copy_doc, |
Guido van Rossum | d3a6a14 | 1998-10-14 13:46:57 +0000 | [diff] [blame] | 140 | "copy() -> md5 object\n\ |
| 141 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 142 | Return a copy (``clone'') of the md5 object."); |
Guido van Rossum | d3a6a14 | 1998-10-14 13:46:57 +0000 | [diff] [blame] | 143 | |
| 144 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 145 | static PyMethodDef md5_methods[] = { |
Neal Norwitz | ba3a16c | 2002-03-31 15:27:00 +0000 | [diff] [blame] | 146 | {"update", (PyCFunction)md5_update, METH_VARARGS, update_doc}, |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 147 | {"digest", (PyCFunction)md5_digest, METH_NOARGS, digest_doc}, |
| 148 | {"hexdigest", (PyCFunction)md5_hexdigest, METH_NOARGS, hexdigest_doc}, |
| 149 | {"copy", (PyCFunction)md5_copy, METH_NOARGS, copy_doc}, |
Barry Warsaw | 3ec0fbb | 2000-08-15 05:59:44 +0000 | [diff] [blame] | 150 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 151 | }; |
| 152 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 153 | static PyObject * |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 154 | md5_get_block_size(PyObject *self, void *closure) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 155 | { |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 156 | return PyInt_FromLong(64); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 159 | static PyObject * |
| 160 | md5_get_digest_size(PyObject *self, void *closure) |
| 161 | { |
| 162 | return PyInt_FromLong(16); |
| 163 | } |
| 164 | |
| 165 | static PyObject * |
| 166 | md5_get_name(PyObject *self, void *closure) |
| 167 | { |
| 168 | return PyString_FromStringAndSize("MD5", 3); |
| 169 | } |
| 170 | |
| 171 | static PyGetSetDef md5_getseters[] = { |
| 172 | {"digest_size", |
| 173 | (getter)md5_get_digest_size, NULL, |
| 174 | NULL, |
| 175 | NULL}, |
| 176 | {"block_size", |
| 177 | (getter)md5_get_block_size, NULL, |
| 178 | NULL, |
| 179 | NULL}, |
| 180 | {"name", |
| 181 | (getter)md5_get_name, NULL, |
| 182 | NULL, |
| 183 | NULL}, |
| 184 | /* the old md5 and sha modules support 'digest_size' as in PEP 247. |
| 185 | * the old sha module also supported 'digestsize'. ugh. */ |
| 186 | {"digestsize", |
| 187 | (getter)md5_get_digest_size, NULL, |
| 188 | NULL, |
| 189 | NULL}, |
| 190 | {NULL} /* Sentinel */ |
| 191 | }; |
| 192 | |
| 193 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 194 | PyDoc_STRVAR(module_doc, |
Guido van Rossum | d3a6a14 | 1998-10-14 13:46:57 +0000 | [diff] [blame] | 195 | "This module implements the interface to RSA's MD5 message digest\n\ |
| 196 | algorithm (see also Internet RFC 1321). Its use is quite\n\ |
| 197 | straightforward: use the new() to create an md5 object. You can now\n\ |
| 198 | feed this object with arbitrary strings using the update() method, and\n\ |
| 199 | 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] | 200 | 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] | 201 | fed to it so far using the digest() method.\n\ |
| 202 | \n\ |
| 203 | Functions:\n\ |
| 204 | \n\ |
| 205 | new([arg]) -- return a new md5 object, initialized with arg if provided\n\ |
| 206 | md5([arg]) -- DEPRECATED, same as new, but for compatibility\n\ |
| 207 | \n\ |
| 208 | Special Objects:\n\ |
| 209 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 210 | MD5Type -- type object for md5 objects"); |
Guido van Rossum | d3a6a14 | 1998-10-14 13:46:57 +0000 | [diff] [blame] | 211 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 212 | PyDoc_STRVAR(md5type_doc, |
Guido van Rossum | d3a6a14 | 1998-10-14 13:46:57 +0000 | [diff] [blame] | 213 | "An md5 represents the object used to calculate the MD5 checksum of a\n\ |
| 214 | string of information.\n\ |
| 215 | \n\ |
| 216 | Methods:\n\ |
| 217 | \n\ |
| 218 | update() -- updates the current digest with an additional string\n\ |
| 219 | digest() -- return the current digest value\n\ |
Neal Norwitz | 98a355d | 2002-10-11 21:53:01 +0000 | [diff] [blame] | 220 | hexdigest() -- return the current digest as a string of hexadecimal digits\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 221 | copy() -- return a copy of the current md5 object"); |
Guido van Rossum | d3a6a14 | 1998-10-14 13:46:57 +0000 | [diff] [blame] | 222 | |
Tim Peters | 0c32279 | 2002-07-17 16:49:03 +0000 | [diff] [blame] | 223 | static PyTypeObject MD5type = { |
Fred Drake | 0d40ba4 | 2000-02-04 20:33:49 +0000 | [diff] [blame] | 224 | PyObject_HEAD_INIT(NULL) |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 225 | 0, /*ob_size*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 226 | "_md5.md5", /*tp_name*/ |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 227 | sizeof(md5object), /*tp_size*/ |
| 228 | 0, /*tp_itemsize*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 229 | /* methods */ |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 230 | (destructor)md5_dealloc, /*tp_dealloc*/ |
| 231 | 0, /*tp_print*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 232 | 0, /*tp_getattr*/ |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 233 | 0, /*tp_setattr*/ |
| 234 | 0, /*tp_compare*/ |
| 235 | 0, /*tp_repr*/ |
| 236 | 0, /*tp_as_number*/ |
Guido van Rossum | d3a6a14 | 1998-10-14 13:46:57 +0000 | [diff] [blame] | 237 | 0, /*tp_as_sequence*/ |
| 238 | 0, /*tp_as_mapping*/ |
| 239 | 0, /*tp_hash*/ |
| 240 | 0, /*tp_call*/ |
| 241 | 0, /*tp_str*/ |
| 242 | 0, /*tp_getattro*/ |
| 243 | 0, /*tp_setattro*/ |
| 244 | 0, /*tp_as_buffer*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 245 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
Guido van Rossum | d3a6a14 | 1998-10-14 13:46:57 +0000 | [diff] [blame] | 246 | md5type_doc, /*tp_doc*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 247 | 0, /*tp_traverse*/ |
| 248 | 0, /*tp_clear*/ |
| 249 | 0, /*tp_richcompare*/ |
| 250 | 0, /*tp_weaklistoffset*/ |
| 251 | 0, /*tp_iter*/ |
| 252 | 0, /*tp_iternext*/ |
| 253 | md5_methods, /*tp_methods*/ |
| 254 | 0, /*tp_members*/ |
| 255 | md5_getseters, /*tp_getset*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 256 | }; |
| 257 | |
| 258 | |
| 259 | /* MD5 functions */ |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 260 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 261 | static PyObject * |
Peter Schneider-Kamp | 286da3b | 2000-07-10 12:43:58 +0000 | [diff] [blame] | 262 | MD5_new(PyObject *self, PyObject *args) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 263 | { |
| 264 | md5object *md5p; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 265 | unsigned char *cp = NULL; |
Guido van Rossum | ef38b78 | 1995-07-26 17:33:10 +0000 | [diff] [blame] | 266 | int len = 0; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 267 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 268 | if (!PyArg_ParseTuple(args, "|s#:new", &cp, &len)) |
Guido van Rossum | ef38b78 | 1995-07-26 17:33:10 +0000 | [diff] [blame] | 269 | return NULL; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 270 | |
| 271 | if ((md5p = newmd5object()) == NULL) |
| 272 | return NULL; |
| 273 | |
| 274 | if (cp) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 275 | md5_append(&md5p->md5, cp, len); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 276 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 277 | return (PyObject *)md5p; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 278 | } |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 279 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 280 | PyDoc_STRVAR(new_doc, |
Guido van Rossum | d3a6a14 | 1998-10-14 13:46:57 +0000 | [diff] [blame] | 281 | "new([arg]) -> md5 object\n\ |
| 282 | \n\ |
| 283 | Return a new md5 object. If arg is present, the method call update(arg)\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 284 | is made."); |
Guido van Rossum | d3a6a14 | 1998-10-14 13:46:57 +0000 | [diff] [blame] | 285 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 286 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 287 | /* List of functions exported by this module */ |
| 288 | |
Barry Warsaw | 8b43b19 | 1996-12-09 22:32:36 +0000 | [diff] [blame] | 289 | static PyMethodDef md5_functions[] = { |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 290 | {"new", (PyCFunction)MD5_new, METH_VARARGS, new_doc}, |
Guido van Rossum | ef38b78 | 1995-07-26 17:33:10 +0000 | [diff] [blame] | 291 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 292 | }; |
| 293 | |
| 294 | |
| 295 | /* Initialize this module. */ |
| 296 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 297 | PyMODINIT_FUNC |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 298 | init_md5(void) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 299 | { |
Fred Drake | 52a42e9 | 2001-11-02 22:05:06 +0000 | [diff] [blame] | 300 | PyObject *m, *d; |
Fred Drake | 0d40ba4 | 2000-02-04 20:33:49 +0000 | [diff] [blame] | 301 | |
| 302 | MD5type.ob_type = &PyType_Type; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 303 | if (PyType_Ready(&MD5type) < 0) |
| 304 | return; |
| 305 | m = Py_InitModule3("_md5", md5_functions, module_doc); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 306 | if (m == NULL) |
| 307 | return; |
Guido van Rossum | d3a6a14 | 1998-10-14 13:46:57 +0000 | [diff] [blame] | 308 | d = PyModule_GetDict(m); |
| 309 | PyDict_SetItemString(d, "MD5Type", (PyObject *)&MD5type); |
Fred Drake | 52a42e9 | 2001-11-02 22:05:06 +0000 | [diff] [blame] | 310 | PyModule_AddIntConstant(m, "digest_size", 16); |
Guido van Rossum | d3a6a14 | 1998-10-14 13:46:57 +0000 | [diff] [blame] | 311 | /* 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] | 312 | } |