home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / py2s152.zip / Modules / md5module.c < prev    next >
C/C++ Source or Header  |  1999-06-27  |  7KB  |  279 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  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
  15. permission.
  16.  
  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.
  20.  
  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.
  29.  
  30. ******************************************************************/
  31.  
  32. /* MD5 module */
  33.  
  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.) */
  38.  
  39.  
  40. /* MD5 objects */
  41.  
  42. #include "Python.h"
  43. #include "md5.h"
  44.  
  45. typedef struct {
  46.     PyObject_HEAD
  47.         MD5_CTX    md5;        /* the context holder */
  48. } md5object;
  49.  
  50. staticforward PyTypeObject MD5type;
  51.  
  52. #define is_md5object(v)        ((v)->ob_type == &MD5type)
  53.  
  54. static md5object *
  55. newmd5object()
  56. {
  57.     md5object *md5p;
  58.  
  59.     md5p = PyObject_NEW(md5object, &MD5type);
  60.     if (md5p == NULL)
  61.         return NULL;
  62.  
  63.     MD5Init(&md5p->md5);    /* actual initialisation */
  64.     return md5p;
  65. }
  66.  
  67.  
  68. /* MD5 methods */
  69.  
  70. static void
  71. md5_dealloc(md5p)
  72.     md5object *md5p;
  73. {
  74.     PyMem_DEL(md5p);
  75. }
  76.  
  77.  
  78. /* MD5 methods-as-attributes */
  79.  
  80. static PyObject *
  81. md5_update(self, args)
  82.     md5object *self;
  83.     PyObject *args;
  84. {
  85.     unsigned char *cp;
  86.     int len;
  87.  
  88.     if (!PyArg_Parse(args, "s#", &cp, &len))
  89.         return NULL;
  90.  
  91.     MD5Update(&self->md5, cp, len);
  92.  
  93.     Py_INCREF(Py_None);
  94.     return Py_None;
  95. }
  96.  
  97. static char update_doc [] =
  98. "update (arg)\n\
  99. \n\
  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\
  102. arguments.";
  103.  
  104.  
  105. static PyObject *
  106. md5_digest(self, args)
  107.     md5object *self;
  108.     PyObject *args;
  109. {
  110.  
  111.     MD5_CTX mdContext;
  112.     unsigned char aDigest[16];
  113.  
  114.     if (!PyArg_NoArgs(args))
  115.         return NULL;
  116.  
  117.     /* make a temporary copy, and perform the final */
  118.     mdContext = self->md5;
  119.     MD5Final(aDigest, &mdContext);
  120.  
  121.     return PyString_FromStringAndSize((char *)aDigest, 16);
  122. }
  123.  
  124. static char digest_doc [] =
  125. "digest() -> string\n\
  126. \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.";
  130.  
  131.  
  132. static PyObject *
  133. md5_copy(self, args)
  134.     md5object *self;
  135.     PyObject *args;
  136. {
  137.     md5object *md5p;
  138.  
  139.     if (!PyArg_NoArgs(args))
  140.         return NULL;
  141.  
  142.     if ((md5p = newmd5object()) == NULL)
  143.         return NULL;
  144.  
  145.     md5p->md5 = self->md5;
  146.  
  147.     return (PyObject *)md5p;
  148. }
  149.  
  150. static char copy_doc [] =
  151. "copy() -> md5 object\n\
  152. \n\
  153. Return a copy (``clone'') of the md5 object.";
  154.  
  155.  
  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 */
  161. };
  162.  
  163. static PyObject *
  164. md5_getattr(self, name)
  165.     md5object *self;
  166.     char *name;
  167. {
  168.     return Py_FindMethod(md5_methods, (PyObject *)self, name);
  169. }
  170.  
  171. static char module_doc [] =
  172.  
  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\
  180. \n\
  181. Functions:\n\
  182. \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\
  185. \n\
  186. Special Objects:\n\
  187. \n\
  188. MD5Type -- type object for md5 objects\n\
  189. ";
  190.  
  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\
  194. \n\
  195. Methods:\n\
  196. \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\
  200. ";
  201.  
  202. statichere PyTypeObject MD5type = {
  203.     PyObject_HEAD_INIT(&PyType_Type)
  204.     0,              /*ob_size*/
  205.     "md5",              /*tp_name*/
  206.     sizeof(md5object),      /*tp_size*/
  207.     0,              /*tp_itemsize*/
  208.     /* methods */
  209.     (destructor)md5_dealloc,  /*tp_dealloc*/
  210.     0,              /*tp_print*/
  211.     (getattrfunc)md5_getattr, /*tp_getattr*/
  212.     0,              /*tp_setattr*/
  213.     0,              /*tp_compare*/
  214.     0,              /*tp_repr*/
  215.         0,              /*tp_as_number*/
  216.     0,                        /*tp_as_sequence*/
  217.     0,              /*tp_as_mapping*/
  218.     0,               /*tp_hash*/
  219.     0,              /*tp_call*/
  220.     0,              /*tp_str*/
  221.     0,              /*tp_getattro*/
  222.     0,              /*tp_setattro*/
  223.     0,                      /*tp_as_buffer*/
  224.     0,              /*tp_xxx4*/
  225.     md5type_doc,          /*tp_doc*/
  226. };
  227.  
  228.  
  229. /* MD5 functions */
  230.  
  231. static PyObject *
  232. MD5_new(self, args)
  233.     PyObject *self;
  234.     PyObject *args;
  235. {
  236.     md5object *md5p;
  237.     unsigned char *cp = NULL;
  238.     int len = 0;
  239.  
  240.     if (!PyArg_ParseTuple(args, "|s#", &cp, &len))
  241.         return NULL;
  242.  
  243.     if ((md5p = newmd5object()) == NULL)
  244.         return NULL;
  245.  
  246.     if (cp)
  247.         MD5Update(&md5p->md5, cp, len);
  248.  
  249.     return (PyObject *)md5p;
  250. }
  251.  
  252. static char new_doc [] =
  253. "new([arg]) -> md5 object\n\
  254. \n\
  255. Return a new md5 object. If arg is present, the method call update(arg)\n\
  256. is made.";
  257.  
  258.  
  259. /* List of functions exported by this module */
  260.  
  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 */
  265. };
  266.  
  267.  
  268. /* Initialize this module. */
  269.  
  270. DL_EXPORT(void)
  271. initmd5()
  272. {
  273.     PyObject *m, *d;
  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 */
  278. }
  279.