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.) */
13 #include "structmember.h"
18 md5_state_t md5
; /* the context holder */
21 static PyTypeObject MD5type
;
23 #define is_md5object(v) ((v)->ob_type == &MD5type)
30 md5p
= PyObject_New(md5object
, &MD5type
);
34 md5_init(&md5p
->md5
); /* actual initialisation */
42 md5_dealloc(md5object
*md5p
)
48 /* MD5 methods-as-attributes */
51 md5_update(md5object
*self
, PyObject
*args
)
57 if (!PyArg_ParseTuple(args
, "s*:update", &view
))
61 buf
= (unsigned char *) view
.buf
;
68 md5_append(&self
->md5
, buf
,
69 Py_SAFE_DOWNCAST(nbytes
, Py_ssize_t
, unsigned int));
74 PyBuffer_Release(&view
);
78 PyDoc_STRVAR(update_doc
,
81 Update the md5 object with the string arg. Repeated calls are\n\
82 equivalent to a single call with the concatenation of all the\n\
87 md5_digest(md5object
*self
)
89 md5_state_t mdContext
;
90 unsigned char aDigest
[16];
92 /* make a temporary copy, and perform the final */
93 mdContext
= self
->md5
;
94 md5_finish(&mdContext
, aDigest
);
96 return PyString_FromStringAndSize((char *)aDigest
, 16);
99 PyDoc_STRVAR(digest_doc
,
100 "digest() -> string\n\
102 Return the digest of the strings passed to the update() method so\n\
103 far. This is a 16-byte string which may contain non-ASCII characters,\n\
104 including null bytes.");
108 md5_hexdigest(md5object
*self
)
110 md5_state_t mdContext
;
111 unsigned char digest
[16];
112 unsigned char hexdigest
[32];
115 /* make a temporary copy, and perform the final */
116 mdContext
= self
->md5
;
117 md5_finish(&mdContext
, digest
);
119 /* Make hex version of the digest */
120 for(i
=j
=0; i
<16; i
++) {
122 c
= (digest
[i
] >> 4) & 0xf;
123 c
= (c
>9) ? c
+'a'-10 : c
+ '0';
125 c
= (digest
[i
] & 0xf);
126 c
= (c
>9) ? c
+'a'-10 : c
+ '0';
129 return PyString_FromStringAndSize((char*)hexdigest
, 32);
133 PyDoc_STRVAR(hexdigest_doc
,
134 "hexdigest() -> string\n\
136 Like digest(), but returns the digest as a string of hexadecimal digits.");
140 md5_copy(md5object
*self
)
144 if ((md5p
= newmd5object()) == NULL
)
147 md5p
->md5
= self
->md5
;
149 return (PyObject
*)md5p
;
152 PyDoc_STRVAR(copy_doc
,
153 "copy() -> md5 object\n\
155 Return a copy (``clone'') of the md5 object.");
158 static PyMethodDef md5_methods
[] = {
159 {"update", (PyCFunction
)md5_update
, METH_VARARGS
, update_doc
},
160 {"digest", (PyCFunction
)md5_digest
, METH_NOARGS
, digest_doc
},
161 {"hexdigest", (PyCFunction
)md5_hexdigest
, METH_NOARGS
, hexdigest_doc
},
162 {"copy", (PyCFunction
)md5_copy
, METH_NOARGS
, copy_doc
},
163 {NULL
, NULL
} /* sentinel */
167 md5_get_block_size(PyObject
*self
, void *closure
)
169 return PyInt_FromLong(64);
173 md5_get_digest_size(PyObject
*self
, void *closure
)
175 return PyInt_FromLong(16);
179 md5_get_name(PyObject
*self
, void *closure
)
181 return PyString_FromStringAndSize("MD5", 3);
184 static PyGetSetDef md5_getseters
[] = {
186 (getter
)md5_get_digest_size
, NULL
,
190 (getter
)md5_get_block_size
, NULL
,
194 (getter
)md5_get_name
, NULL
,
197 /* the old md5 and sha modules support 'digest_size' as in PEP 247.
198 * the old sha module also supported 'digestsize'. ugh. */
200 (getter
)md5_get_digest_size
, NULL
,
203 {NULL
} /* Sentinel */
207 PyDoc_STRVAR(module_doc
,
208 "This module implements the interface to RSA's MD5 message digest\n\
209 algorithm (see also Internet RFC 1321). Its use is quite\n\
210 straightforward: use the new() to create an md5 object. You can now\n\
211 feed this object with arbitrary strings using the update() method, and\n\
212 at any point you can ask it for the digest (a strong kind of 128-bit\n\
213 checksum, a.k.a. ``fingerprint'') of the concatenation of the strings\n\
214 fed to it so far using the digest() method.\n\
218 new([arg]) -- return a new md5 object, initialized with arg if provided\n\
219 md5([arg]) -- DEPRECATED, same as new, but for compatibility\n\
223 MD5Type -- type object for md5 objects");
225 PyDoc_STRVAR(md5type_doc
,
226 "An md5 represents the object used to calculate the MD5 checksum of a\n\
227 string of information.\n\
231 update() -- updates the current digest with an additional string\n\
232 digest() -- return the current digest value\n\
233 hexdigest() -- return the current digest as a string of hexadecimal digits\n\
234 copy() -- return a copy of the current md5 object");
236 static PyTypeObject MD5type
= {
237 PyVarObject_HEAD_INIT(NULL
, 0)
238 "_md5.md5", /*tp_name*/
239 sizeof(md5object
), /*tp_size*/
242 (destructor
)md5_dealloc
, /*tp_dealloc*/
249 0, /*tp_as_sequence*/
257 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
258 md5type_doc
, /*tp_doc*/
261 0, /*tp_richcompare*/
262 0, /*tp_weaklistoffset*/
265 md5_methods
, /*tp_methods*/
267 md5_getseters
, /*tp_getset*/
274 MD5_new(PyObject
*self
, PyObject
*args
)
277 Py_buffer view
= { 0 };
281 if (!PyArg_ParseTuple(args
, "|s*:new", &view
))
284 if ((md5p
= newmd5object()) == NULL
) {
285 PyBuffer_Release(&view
);
290 buf
= (unsigned char *) view
.buf
;
297 md5_append(&md5p
->md5
, buf
,
298 Py_SAFE_DOWNCAST(nbytes
, Py_ssize_t
, unsigned int));
302 PyBuffer_Release(&view
);
304 return (PyObject
*)md5p
;
307 PyDoc_STRVAR(new_doc
,
308 "new([arg]) -> md5 object\n\
310 Return a new md5 object. If arg is present, the method call update(arg)\n\
314 /* List of functions exported by this module */
316 static PyMethodDef md5_functions
[] = {
317 {"new", (PyCFunction
)MD5_new
, METH_VARARGS
, new_doc
},
318 {NULL
, NULL
} /* Sentinel */
322 /* Initialize this module. */
329 Py_TYPE(&MD5type
) = &PyType_Type
;
330 if (PyType_Ready(&MD5type
) < 0)
332 m
= Py_InitModule3("_md5", md5_functions
, module_doc
);
335 d
= PyModule_GetDict(m
);
336 PyDict_SetItemString(d
, "MD5Type", (PyObject
*)&MD5type
);
337 PyModule_AddIntConstant(m
, "digest_size", 16);
338 /* No need to check the error here, the caller will do that */