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 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 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. |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 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. |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 31 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 32 | /* MD5 module */ |
| 33 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 39 | |
| 40 | /* MD5 objects */ |
| 41 | |
| 42 | #include "allobjects.h" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 43 | #include "modsupport.h" |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 44 | |
| 45 | #include "md5.h" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 47 | typedef struct { |
| 48 | OB_HEAD |
| 49 | MD5_CTX md5; /* the context holder */ |
| 50 | } md5object; |
| 51 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 52 | staticforward typeobject MD5type; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 53 | |
| 54 | #define is_md5object(v) ((v)->ob_type == &MD5type) |
| 55 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 56 | static md5object * |
| 57 | newmd5object() |
| 58 | { |
| 59 | md5object *md5p; |
| 60 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 61 | md5p = NEWOBJ(md5object, &MD5type); |
| 62 | if (md5p == NULL) |
| 63 | return NULL; |
| 64 | |
| 65 | MD5Init(&md5p->md5); /* actual initialisation */ |
| 66 | return md5p; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 67 | } |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 68 | |
| 69 | |
| 70 | /* MD5 methods */ |
| 71 | |
| 72 | static void |
| 73 | md5_dealloc(md5p) |
| 74 | md5object *md5p; |
| 75 | { |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 76 | DEL(md5p); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 77 | } |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 78 | |
| 79 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 80 | /* MD5 methods-as-attributes */ |
| 81 | |
| 82 | static object * |
| 83 | md5_update(self, args) |
| 84 | md5object *self; |
| 85 | object *args; |
| 86 | { |
| 87 | unsigned char *cp; |
| 88 | int len; |
| 89 | |
| 90 | if (!getargs(args, "s#", &cp, &len)) |
| 91 | return NULL; |
| 92 | |
| 93 | MD5Update(&self->md5, cp, len); |
| 94 | |
| 95 | INCREF(None); |
| 96 | return None; |
| 97 | } |
| 98 | |
| 99 | static object * |
| 100 | md5_digest(self, args) |
| 101 | md5object *self; |
| 102 | object *args; |
| 103 | { |
| 104 | |
| 105 | MD5_CTX mdContext; |
| 106 | unsigned char aDigest[16]; |
| 107 | |
| 108 | if (!getnoarg(args)) |
| 109 | return NULL; |
| 110 | |
| 111 | /* make a temporary copy, and perform the final */ |
| 112 | mdContext = self->md5; |
| 113 | MD5Final(aDigest, &mdContext); |
| 114 | |
| 115 | return newsizedstringobject((char *)aDigest, 16); |
| 116 | } |
| 117 | |
| 118 | static object * |
| 119 | md5_copy(self, args) |
| 120 | md5object *self; |
| 121 | object *args; |
| 122 | { |
| 123 | md5object *md5p; |
| 124 | |
| 125 | if (!getnoarg(args)) |
| 126 | return NULL; |
| 127 | |
| 128 | if ((md5p = newmd5object()) == NULL) |
| 129 | return NULL; |
| 130 | |
| 131 | md5p->md5 = self->md5; |
| 132 | |
| 133 | return (object *)md5p; |
| 134 | } |
| 135 | |
| 136 | static struct methodlist md5_methods[] = { |
| 137 | {"update", (method)md5_update}, |
| 138 | {"digest", (method)md5_digest}, |
| 139 | {"copy", (method)md5_copy}, |
| 140 | {NULL, NULL} /* sentinel */ |
| 141 | }; |
| 142 | |
| 143 | static object * |
| 144 | md5_getattr(self, name) |
| 145 | md5object *self; |
| 146 | char *name; |
| 147 | { |
| 148 | return findmethod(md5_methods, (object *)self, name); |
| 149 | } |
| 150 | |
Guido van Rossum | a320fd3 | 1995-03-09 12:14:15 +0000 | [diff] [blame] | 151 | statichere typeobject MD5type = { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 152 | OB_HEAD_INIT(&Typetype) |
| 153 | 0, /*ob_size*/ |
| 154 | "md5", /*tp_name*/ |
| 155 | sizeof(md5object), /*tp_size*/ |
| 156 | 0, /*tp_itemsize*/ |
| 157 | /* methods */ |
| 158 | (destructor)md5_dealloc, /*tp_dealloc*/ |
| 159 | 0, /*tp_print*/ |
| 160 | (getattrfunc)md5_getattr, /*tp_getattr*/ |
| 161 | 0, /*tp_setattr*/ |
| 162 | 0, /*tp_compare*/ |
| 163 | 0, /*tp_repr*/ |
| 164 | 0, /*tp_as_number*/ |
| 165 | }; |
| 166 | |
| 167 | |
| 168 | /* MD5 functions */ |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 169 | |
| 170 | static object * |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 171 | MD5_new(self, args) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 172 | object *self; |
| 173 | object *args; |
| 174 | { |
| 175 | md5object *md5p; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 176 | unsigned char *cp = NULL; |
Guido van Rossum | ef38b78 | 1995-07-26 17:33:10 +0000 | [diff] [blame] | 177 | int len = 0; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 178 | |
Guido van Rossum | ef38b78 | 1995-07-26 17:33:10 +0000 | [diff] [blame] | 179 | if (!newgetargs(args, "|s#", &cp, &len)) |
| 180 | return NULL; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 181 | |
| 182 | if ((md5p = newmd5object()) == NULL) |
| 183 | return NULL; |
| 184 | |
| 185 | if (cp) |
| 186 | MD5Update(&md5p->md5, cp, len); |
| 187 | |
| 188 | return (object *)md5p; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 189 | } |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 190 | |
| 191 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 192 | /* List of functions exported by this module */ |
| 193 | |
| 194 | static struct methodlist md5_functions[] = { |
Guido van Rossum | ef38b78 | 1995-07-26 17:33:10 +0000 | [diff] [blame] | 195 | {"new", (method)MD5_new, 1}, |
| 196 | {"md5", (method)MD5_new, 1}, /* Backward compatibility */ |
| 197 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | |
| 201 | /* Initialize this module. */ |
| 202 | |
| 203 | void |
| 204 | initmd5() |
| 205 | { |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 206 | (void)initmodule("md5", md5_functions); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 207 | } |