Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 524b588 | 1995-01-04 19:10:35 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 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 | ******************************************************************/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 24 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 25 | /* MD5 module */ |
| 26 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 32 | |
| 33 | /* MD5 objects */ |
| 34 | |
| 35 | #include "allobjects.h" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 36 | #include "modsupport.h" |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 37 | |
| 38 | #include "md5.h" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 39 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 40 | typedef struct { |
| 41 | OB_HEAD |
| 42 | MD5_CTX md5; /* the context holder */ |
| 43 | } md5object; |
| 44 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 45 | staticforward typeobject MD5type; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 46 | |
| 47 | #define is_md5object(v) ((v)->ob_type == &MD5type) |
| 48 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 49 | static md5object * |
| 50 | newmd5object() |
| 51 | { |
| 52 | md5object *md5p; |
| 53 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 54 | md5p = NEWOBJ(md5object, &MD5type); |
| 55 | if (md5p == NULL) |
| 56 | return NULL; |
| 57 | |
| 58 | MD5Init(&md5p->md5); /* actual initialisation */ |
| 59 | return md5p; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 60 | } |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 61 | |
| 62 | |
| 63 | /* MD5 methods */ |
| 64 | |
| 65 | static void |
| 66 | md5_dealloc(md5p) |
| 67 | md5object *md5p; |
| 68 | { |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 69 | DEL(md5p); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 70 | } |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 71 | |
| 72 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 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 */ |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 162 | |
| 163 | static object * |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 164 | MD5_new(self, args) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 165 | object *self; |
| 166 | object *args; |
| 167 | { |
| 168 | md5object *md5p; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 169 | unsigned char *cp = NULL; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 170 | int len; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 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; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 185 | } |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 186 | |
| 187 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 188 | /* List of functions exported by this module */ |
| 189 | |
| 190 | static struct methodlist md5_functions[] = { |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 191 | {"new", (method)MD5_new}, |
| 192 | {"md5", (method)MD5_new}, /* Backward compatibility */ |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 193 | {NULL, NULL} /* Sentinel */ |
| 194 | }; |
| 195 | |
| 196 | |
| 197 | /* Initialize this module. */ |
| 198 | |
| 199 | void |
| 200 | initmd5() |
| 201 | { |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 202 | (void)initmodule("md5", md5_functions); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 203 | } |