summaryrefslogtreecommitdiffstats
path: root/Modules/md5module.c
diff options
context:
space:
mode:
authorBarry Warsaw <barry@python.org>2000-08-15 05:59:44 (GMT)
committerBarry Warsaw <barry@python.org>2000-08-15 05:59:44 (GMT)
commit3ec0fbb2995d9ceb6cbb4f7e8234b3ad80dc5c05 (patch)
tree185f73231d616a6c4bd605c0dea714a07ac474bc /Modules/md5module.c
parent34c391d7437a9005bb581d643fcad4f957dfbe88 (diff)
downloadcpython-3ec0fbb2995d9ceb6cbb4f7e8234b3ad80dc5c05.zip
cpython-3ec0fbb2995d9ceb6cbb4f7e8234b3ad80dc5c05.tar.gz
cpython-3ec0fbb2995d9ceb6cbb4f7e8234b3ad80dc5c05.tar.bz2
md5_hexdigest(): After a brief conversation with TP, added hexdigest()
to this module to mirror sha's hexdigest() method.
Diffstat (limited to 'Modules/md5module.c')
-rw-r--r--Modules/md5module.c50
1 files changed, 41 insertions, 9 deletions
diff --git a/Modules/md5module.c b/Modules/md5module.c
index 591ba28..5eac97b 100644
--- a/Modules/md5module.c
+++ b/Modules/md5module.c
@@ -81,8 +81,7 @@ arguments.";
static PyObject *
md5_digest(md5object *self, PyObject *args)
{
-
- MD5_CTX mdContext;
+ MD5_CTX mdContext;
unsigned char aDigest[16];
if (!PyArg_NoArgs(args))
@@ -104,6 +103,41 @@ including null bytes.";
static PyObject *
+md5_hexdigest(md5object *self, PyObject *args)
+{
+ MD5_CTX mdContext;
+ unsigned char digest[16];
+ unsigned char hexdigest[32];
+ int i, j;
+
+ if (!PyArg_NoArgs(args))
+ return NULL;
+
+ /* make a temporary copy, and perform the final */
+ mdContext = self->md5;
+ MD5Final(digest, &mdContext);
+
+ /* Make hex version of the digest */
+ for(i=j=0; i<16; i++) {
+ char c;
+ c = (digest[i] >> 4) & 0xf;
+ c = (c>9) ? c+'a'-10 : c + '0';
+ hexdigest[j++] = c;
+ c = (digest[i] & 0xf);
+ c = (c>9) ? c+'a'-10 : c + '0';
+ hexdigest[j++] = c;
+ }
+ return PyString_FromStringAndSize((char*)hexdigest, 32);
+}
+
+
+static char hexdigest_doc [] =
+"hexdigest() -> string\n\
+\n\
+Like digest(), but returns the digest as a string of hexadecimal digits.";
+
+
+static PyObject *
md5_copy(md5object *self, PyObject *args)
{
md5object *md5p;
@@ -126,13 +160,11 @@ Return a copy (``clone'') of the md5 object.";
static PyMethodDef md5_methods[] = {
- {"update", (PyCFunction)md5_update,
- METH_OLDARGS, update_doc},
- {"digest", (PyCFunction)md5_digest,
- METH_OLDARGS, digest_doc},
- {"copy", (PyCFunction)md5_copy,
- METH_OLDARGS, copy_doc},
- {NULL, NULL} /* sentinel */
+ {"update", (PyCFunction)md5_update, METH_OLDARGS, update_doc},
+ {"digest", (PyCFunction)md5_digest, METH_OLDARGS, digest_doc},
+ {"hexdigest", (PyCFunction)md5_hexdigest, METH_OLDARGS, hexdigest_doc},
+ {"copy", (PyCFunction)md5_copy, METH_OLDARGS, copy_doc},
+ {NULL, NULL} /* sentinel */
};
static PyObject *