home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Modules / md5module.c < prev    next >
C/C++ Source or Header  |  1994-02-15  |  4KB  |  203 lines

  1. /***********************************************************
  2. Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
  3. Amsterdam, 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 not be used in advertising or publicity pertaining to
  13. distribution of the software without specific, written prior permission.
  14.  
  15. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  18. FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25. /* MD5 module */
  26.  
  27. /* This module provides an interface to the RSA Data Security,
  28.    Inc. MD5 Message-Digest Algorithm, described in RFC 1321.
  29.    It requires the files md5c.c and md5.h (which are slightly changed
  30.    from the versions in the RFC to avoid the "global.h" file.) */
  31.  
  32.  
  33. /* MD5 objects */
  34.  
  35. #include "allobjects.h"
  36. #include "modsupport.h"
  37.  
  38. #include "md5.h"
  39.  
  40. typedef struct {
  41.     OB_HEAD
  42.         MD5_CTX    md5;        /* the context holder */
  43. } md5object;
  44.  
  45. staticforward typeobject MD5type;
  46.  
  47. #define is_md5object(v)        ((v)->ob_type == &MD5type)
  48.  
  49. static md5object *
  50. newmd5object()
  51. {
  52.     md5object *md5p;
  53.  
  54.     md5p = NEWOBJ(md5object, &MD5type);
  55.     if (md5p == NULL)
  56.         return NULL;
  57.  
  58.     MD5Init(&md5p->md5);    /* actual initialisation */
  59.     return md5p;
  60. }
  61.  
  62.  
  63. /* MD5 methods */
  64.  
  65. static void
  66. md5_dealloc(md5p)
  67.     md5object *md5p;
  68. {
  69.     DEL(md5p);
  70. }
  71.  
  72.  
  73. /* MD5 methods-as-attributes */
  74.  
  75. static object *
  76. md5_update(self, args)
  77.     md5object *self;
  78.     object *args;
  79. {
  80.     unsigned char *cp;
  81.     int len;
  82.  
  83.     if (!getargs(args, "s#", &cp, &len))
  84.         return NULL;
  85.  
  86.     MD5Update(&self->md5, cp, len);
  87.  
  88.     INCREF(None);
  89.     return None;
  90. }
  91.  
  92. static object *
  93. md5_digest(self, args)
  94.     md5object *self;
  95.     object *args;
  96. {
  97.  
  98.     MD5_CTX mdContext;
  99.     unsigned char aDigest[16];
  100.  
  101.     if (!getnoarg(args))
  102.         return NULL;
  103.  
  104.     /* make a temporary copy, and perform the final */
  105.     mdContext = self->md5;
  106.     MD5Final(aDigest, &mdContext);
  107.  
  108.     return newsizedstringobject((char *)aDigest, 16);
  109. }
  110.  
  111. static object *
  112. md5_copy(self, args)
  113.     md5object *self;
  114.     object *args;
  115. {
  116.     md5object *md5p;
  117.  
  118.     if (!getnoarg(args))
  119.         return NULL;
  120.  
  121.     if ((md5p = newmd5object()) == NULL)
  122.         return NULL;
  123.  
  124.     md5p->md5 = self->md5;
  125.  
  126.     return (object *)md5p;
  127. }
  128.  
  129. static struct methodlist md5_methods[] = {
  130.     {"update",        (method)md5_update},
  131.     {"digest",        (method)md5_digest},
  132.     {"copy",        (method)md5_copy},
  133.     {NULL,            NULL}        /* sentinel */
  134. };
  135.  
  136. static object *
  137. md5_getattr(self, name)
  138.     md5object *self;
  139.     char *name;
  140. {
  141.     return findmethod(md5_methods, (object *)self, name);
  142. }
  143.  
  144. static typeobject MD5type = {
  145.     OB_HEAD_INIT(&Typetype)
  146.     0,            /*ob_size*/
  147.     "md5",            /*tp_name*/
  148.     sizeof(md5object),    /*tp_size*/
  149.     0,            /*tp_itemsize*/
  150.     /* methods */
  151.     (destructor)md5_dealloc, /*tp_dealloc*/
  152.     0,            /*tp_print*/
  153.     (getattrfunc)md5_getattr, /*tp_getattr*/
  154.     0,            /*tp_setattr*/
  155.     0,            /*tp_compare*/
  156.     0,            /*tp_repr*/
  157.         0,            /*tp_as_number*/
  158. };
  159.  
  160.  
  161. /* MD5 functions */
  162.  
  163. static object *
  164. MD5_md5(self, args)
  165.     object *self;
  166.     object *args;
  167. {
  168.     md5object *md5p;
  169.     unsigned char *cp = NULL;
  170.     int len;
  171.  
  172.     if (!getargs(args, "")) {
  173.         err_clear();
  174.         if (!getargs(args, "s#", &cp, &len))
  175.             return NULL;
  176.     }
  177.  
  178.     if ((md5p = newmd5object()) == NULL)
  179.         return NULL;
  180.  
  181.     if (cp)
  182.         MD5Update(&md5p->md5, cp, len);
  183.  
  184.     return (object *)md5p;
  185. }
  186.  
  187.  
  188. /* List of functions exported by this module */
  189.  
  190. static struct methodlist md5_functions[] = {
  191.     {"md5",            (method)MD5_md5},
  192.     {NULL,            NULL}         /* Sentinel */
  193. };
  194.  
  195.  
  196. /* Initialize this module. */
  197.  
  198. void
  199. initmd5()
  200. {
  201.     (void)initmodule("md5", md5_functions);
  202. }
  203.