Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1992 by Stichting Mathematisch Centrum, Amsterdam, The |
| 3 | 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 | /* MD5 module */ |
| 25 | |
| 26 | /* This module provides an interface to a message digest algorithm, |
| 27 | MD5 in this case */ |
| 28 | |
| 29 | /* MD5 objects */ |
| 30 | |
| 31 | #include "allobjects.h" |
| 32 | #include "modsupport.h" /* For getargs() etc. */ |
| 33 | |
| 34 | #include "md5.h" |
| 35 | typedef struct { |
| 36 | OB_HEAD |
| 37 | MD5_CTX md5; /* the context holder */ |
| 38 | } md5object; |
| 39 | |
| 40 | extern typeobject MD5type; /* Really static, forward */ |
| 41 | |
| 42 | #define is_md5object(v) ((v)->ob_type == &MD5type) |
| 43 | |
| 44 | static const char initialiser_name[] = "md5"; |
| 45 | |
| 46 | /* #define MD5_DEBUG */ |
| 47 | |
| 48 | static md5object * |
| 49 | newmd5object() |
| 50 | { |
| 51 | md5object *md5p; |
| 52 | |
| 53 | |
| 54 | #ifdef MD5_DEBUG |
| 55 | fputs( "md5_object() called...\n", stderr ); |
| 56 | #endif /* def MD5_DEBUG */ |
| 57 | md5p = NEWOBJ(md5object, &MD5type); |
| 58 | if (md5p == NULL) |
| 59 | return NULL; |
| 60 | |
| 61 | MD5Init(&md5p->md5); /* actual initialisation */ |
| 62 | return md5p; |
| 63 | } /* newmd5object() */ |
| 64 | |
| 65 | |
| 66 | /* MD5 methods */ |
| 67 | |
| 68 | static void |
| 69 | md5_dealloc(md5p) |
| 70 | md5object *md5p; |
| 71 | { |
| 72 | #ifdef MD5_DEBUG |
| 73 | fputs( "md5_dealloc() called...\n", stderr ); |
| 74 | #endif /* def MD5_DEBUG */ |
| 75 | |
| 76 | DEL(md5p); |
| 77 | } /* md5_dealloc() */ |
| 78 | |
| 79 | |
| 80 | /* MD5 initialisation */ |
| 81 | |
| 82 | static object * |
| 83 | MD5_md5(self, args) |
| 84 | object *self; |
| 85 | object *args; |
| 86 | { |
| 87 | md5object *md5p; |
| 88 | char *cp = (char *)NULL; |
| 89 | int len; |
| 90 | |
| 91 | |
| 92 | #ifdef MD5_DEBUG |
| 93 | fputs("MD5_md5() called...\n", stderr); |
| 94 | #endif /* def MD5_DEBUG */ |
| 95 | |
| 96 | if (!getargs(args, "")) { |
| 97 | err_clear(); |
| 98 | if (!getargs(args, "s#", &cp, &len)) |
| 99 | return NULL; |
| 100 | } |
| 101 | |
| 102 | if ((md5p = newmd5object()) == NULL) |
| 103 | return NULL; |
| 104 | |
| 105 | if (cp) |
| 106 | MD5Update(&md5p->md5, cp, len); |
| 107 | |
| 108 | return (object *)md5p; |
| 109 | } /* MD5_md5() */ |
| 110 | |
| 111 | |
| 112 | /* MD5 methods-as-attributes */ |
| 113 | static object * |
| 114 | md5_update(self, args) |
| 115 | md5object *self; |
| 116 | object *args; |
| 117 | { |
| 118 | char *cp; |
| 119 | int len; |
| 120 | |
| 121 | |
| 122 | if (!getargs(args, "s#", &cp, &len)) |
| 123 | return NULL; |
| 124 | |
| 125 | MD5Update(&self->md5, cp, len); |
| 126 | |
| 127 | INCREF(None); |
| 128 | return None; |
| 129 | } /* md5_update() */ |
| 130 | |
| 131 | static object * |
| 132 | md5_digest(self, args) |
| 133 | md5object *self; |
| 134 | object *args; |
| 135 | { |
| 136 | MD5_CTX mdContext; |
| 137 | stringobject *strobjp; |
| 138 | |
| 139 | if (!getnoarg(args)) |
| 140 | return NULL; |
| 141 | |
| 142 | /* make a temporary copy, and perform the final */ |
| 143 | mdContext = self->md5; |
| 144 | MD5Final(&mdContext); |
| 145 | |
| 146 | return newsizedstringobject((char *)mdContext.digest, 16); |
| 147 | } /* md5_digest() */ |
| 148 | |
| 149 | static object * |
| 150 | md5_copy(self, args) |
| 151 | md5object *self; |
| 152 | object *args; |
| 153 | { |
| 154 | md5object *md5p; |
| 155 | |
| 156 | |
| 157 | if (!getnoarg(args)) |
| 158 | return NULL; |
| 159 | |
| 160 | if ((md5p = newmd5object()) == NULL) |
| 161 | return NULL; |
| 162 | |
| 163 | md5p->md5 = self->md5; |
| 164 | |
| 165 | return (object *)md5p; |
| 166 | } /* md5_copy() */ |
| 167 | |
| 168 | |
| 169 | static struct methodlist md5_methods[] = { |
| 170 | {"update", md5_update}, |
| 171 | {"digest", md5_digest}, |
| 172 | {"copy", md5_copy}, |
| 173 | {NULL, NULL} /* sentinel */ |
| 174 | }; |
| 175 | |
| 176 | static object * |
| 177 | md5_getattr(self, name) |
| 178 | md5object *self; |
| 179 | char *name; |
| 180 | { |
| 181 | return findmethod(md5_methods, (object *)self, name); |
| 182 | } /* md5_getattr() */ |
| 183 | |
| 184 | |
| 185 | static typeobject MD5type = { |
| 186 | OB_HEAD_INIT(&Typetype) |
| 187 | 0, /*ob_size*/ |
| 188 | "md5", /*tp_name*/ |
| 189 | sizeof(md5object), /*tp_size*/ |
| 190 | 0, /*tp_itemsize*/ |
| 191 | /* methods */ |
| 192 | md5_dealloc, /*tp_dealloc*/ |
| 193 | 0, /*tp_print*/ |
| 194 | md5_getattr, /*tp_getattr*/ |
| 195 | 0, /*tp_setattr*/ |
| 196 | 0, /*tp_compare*/ |
| 197 | 0, /*tp_repr*/ |
| 198 | 0, /*tp_as_number*/ |
| 199 | }; |
| 200 | |
| 201 | /* List of functions exported by this module */ |
| 202 | |
| 203 | static struct methodlist md5_functions[] = { |
| 204 | {initialiser_name, MD5_md5}, |
| 205 | {NULL, NULL} /* Sentinel */ |
| 206 | }; |
| 207 | |
| 208 | |
| 209 | /* Initialize this module. */ |
| 210 | |
| 211 | void |
| 212 | initmd5() |
| 213 | { |
| 214 | #ifdef MD5_DEBUG |
| 215 | fputs( "initmd5() called...\n", stderr ); |
| 216 | #endif /* def MD5_DEBUG */ |
| 217 | (void)initmodule("md5", md5_functions); |
| 218 | } /* initmd5() */ |
| 219 | |
| 220 | #ifdef MAKEDUMMYINT |
| 221 | int _md5_dummy_int; /* XXX otherwise, we're .bss-less (DYNLOAD->Jack?) */ |
| 222 | #endif /* def MAKEDUMMYINT */ |