1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
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
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.
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.
30 ******************************************************************/
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.) */
47 MD5_CTX md5
; /* the context holder */
50 staticforward PyTypeObject MD5type
;
52 #define is_md5object(v) ((v)->ob_type == &MD5type)
59 md5p
= PyObject_NEW(md5object
, &MD5type
);
63 MD5Init(&md5p
->md5
); /* actual initialisation */
78 /* MD5 methods-as-attributes */
81 md5_update(self
, args
)
88 if (!PyArg_Parse(args
, "s#", &cp
, &len
))
91 MD5Update(&self
->md5
, cp
, len
);
97 static char update_doc
[] =
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\
106 md5_digest(self
, args
)
112 unsigned char aDigest
[16];
114 if (!PyArg_NoArgs(args
))
117 /* make a temporary copy, and perform the final */
118 mdContext
= self
->md5
;
119 MD5Final(aDigest
, &mdContext
);
121 return PyString_FromStringAndSize((char *)aDigest
, 16);
124 static char digest_doc
[] =
125 "digest() -> string\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.";
139 if (!PyArg_NoArgs(args
))
142 if ((md5p
= newmd5object()) == NULL
)
145 md5p
->md5
= self
->md5
;
147 return (PyObject
*)md5p
;
150 static char copy_doc
[] =
151 "copy() -> md5 object\n\
153 Return a copy (``clone'') of the md5 object.";
156 static PyMethodDef md5_methods
[] = {
157 {"update", (PyCFunction
)md5_update
, 0, update_doc
},
158 {"digest", (PyCFunction
)md5_digest
, 0, digest_doc
},
159 {"copy", (PyCFunction
)md5_copy
, 0, copy_doc
},
160 {NULL
, NULL
} /* sentinel */
164 md5_getattr(self
, name
)
168 return Py_FindMethod(md5_methods
, (PyObject
*)self
, name
);
171 static char module_doc
[] =
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\
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\
188 MD5Type -- type object for md5 objects\n\
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\
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\
202 statichere PyTypeObject MD5type
= {
203 PyObject_HEAD_INIT(&PyType_Type
)
206 sizeof(md5object
), /*tp_size*/
209 (destructor
)md5_dealloc
, /*tp_dealloc*/
211 (getattrfunc
)md5_getattr
, /*tp_getattr*/
216 0, /*tp_as_sequence*/
225 md5type_doc
, /*tp_doc*/
237 unsigned char *cp
= NULL
;
240 if (!PyArg_ParseTuple(args
, "|s#", &cp
, &len
))
243 if ((md5p
= newmd5object()) == NULL
)
247 MD5Update(&md5p
->md5
, cp
, len
);
249 return (PyObject
*)md5p
;
252 static char new_doc
[] =
253 "new([arg]) -> md5 object\n\
255 Return a new md5 object. If arg is present, the method call update(arg)\n\
259 /* List of functions exported by this module */
261 static PyMethodDef md5_functions
[] = {
262 {"new", (PyCFunction
)MD5_new
, 1, new_doc
},
263 {"md5", (PyCFunction
)MD5_new
, 1, new_doc
}, /* Backward compatibility */
264 {NULL
, NULL
} /* Sentinel */
268 /* Initialize this module. */
274 m
= Py_InitModule3("md5", md5_functions
, module_doc
);
275 d
= PyModule_GetDict(m
);
276 PyDict_SetItemString(d
, "MD5Type", (PyObject
*)&MD5type
);
277 /* No need to check the error here, the caller will do that */