blob: 6230cee1937701991b279bf2932d0951fb3bbb04 [file] [log] [blame]
Guido van Rossum5f59d601992-12-14 16:59:51 +00001/***********************************************************
2Copyright 1992 by Stichting Mathematisch Centrum, Amsterdam, The
3Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF 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"
35typedef struct {
36 OB_HEAD
37 MD5_CTX md5; /* the context holder */
38} md5object;
39
40extern typeobject MD5type; /* Really static, forward */
41
42#define is_md5object(v) ((v)->ob_type == &MD5type)
43
Guido van Rossum5f59d601992-12-14 16:59:51 +000044/* #define MD5_DEBUG */
45
46static md5object *
47newmd5object()
48{
49 md5object *md5p;
50
51
52#ifdef MD5_DEBUG
53 fputs( "md5_object() called...\n", stderr );
54#endif /* def MD5_DEBUG */
55 md5p = NEWOBJ(md5object, &MD5type);
56 if (md5p == NULL)
57 return NULL;
58
59 MD5Init(&md5p->md5); /* actual initialisation */
60 return md5p;
61} /* newmd5object() */
62
63
64/* MD5 methods */
65
66static void
67md5_dealloc(md5p)
68 md5object *md5p;
69{
70#ifdef MD5_DEBUG
71 fputs( "md5_dealloc() called...\n", stderr );
72#endif /* def MD5_DEBUG */
73
74 DEL(md5p);
75} /* md5_dealloc() */
76
77
78/* MD5 initialisation */
79
80static object *
81MD5_md5(self, args)
82 object *self;
83 object *args;
84{
85 md5object *md5p;
86 char *cp = (char *)NULL;
87 int len;
88
89
90#ifdef MD5_DEBUG
91 fputs("MD5_md5() called...\n", stderr);
92#endif /* def MD5_DEBUG */
93
94 if (!getargs(args, "")) {
95 err_clear();
96 if (!getargs(args, "s#", &cp, &len))
97 return NULL;
98 }
99
100 if ((md5p = newmd5object()) == NULL)
101 return NULL;
102
103 if (cp)
104 MD5Update(&md5p->md5, cp, len);
105
106 return (object *)md5p;
107} /* MD5_md5() */
108
109
110/* MD5 methods-as-attributes */
111static object *
112md5_update(self, args)
113 md5object *self;
114 object *args;
115{
116 char *cp;
117 int len;
118
119
120 if (!getargs(args, "s#", &cp, &len))
121 return NULL;
122
123 MD5Update(&self->md5, cp, len);
124
125 INCREF(None);
126 return None;
127} /* md5_update() */
128
Guido van Rossum13ecc7a1993-11-01 16:19:05 +0000129#define DIGESTLEN 16 /* this is used twice--walrus@umich.edu */
Guido van Rossum5f59d601992-12-14 16:59:51 +0000130static object *
131md5_digest(self, args)
132 md5object *self;
133 object *args;
134{
Guido van Rossum13ecc7a1993-11-01 16:19:05 +0000135
Guido van Rossum5f59d601992-12-14 16:59:51 +0000136 MD5_CTX mdContext;
Guido van Rossum13ecc7a1993-11-01 16:19:05 +0000137 char aDigest[DIGESTLEN];
138
Guido van Rossum5f59d601992-12-14 16:59:51 +0000139
140 if (!getnoarg(args))
141 return NULL;
142
143 /* make a temporary copy, and perform the final */
144 mdContext = self->md5;
Guido van Rossum13ecc7a1993-11-01 16:19:05 +0000145 MD5Final(aDigest, &mdContext);
Guido van Rossum5f59d601992-12-14 16:59:51 +0000146
Guido van Rossum13ecc7a1993-11-01 16:19:05 +0000147 return newsizedstringobject((char *)aDigest, DIGESTLEN);
Guido van Rossum5f59d601992-12-14 16:59:51 +0000148} /* md5_digest() */
Guido van Rossum13ecc7a1993-11-01 16:19:05 +0000149#undef DIGESTLEN
Guido van Rossum5f59d601992-12-14 16:59:51 +0000150
151static object *
152md5_copy(self, args)
153 md5object *self;
154 object *args;
155{
156 md5object *md5p;
157
158
159 if (!getnoarg(args))
160 return NULL;
161
162 if ((md5p = newmd5object()) == NULL)
163 return NULL;
164
165 md5p->md5 = self->md5;
166
167 return (object *)md5p;
168} /* md5_copy() */
169
170
171static struct methodlist md5_methods[] = {
172 {"update", md5_update},
173 {"digest", md5_digest},
174 {"copy", md5_copy},
175 {NULL, NULL} /* sentinel */
176};
177
178static object *
179md5_getattr(self, name)
180 md5object *self;
181 char *name;
182{
183 return findmethod(md5_methods, (object *)self, name);
184} /* md5_getattr() */
185
Guido van Rossum13ecc7a1993-11-01 16:19:05 +0000186#ifndef _AIX
187static
188#endif
189typeobject MD5type = {
Guido van Rossum5f59d601992-12-14 16:59:51 +0000190 OB_HEAD_INIT(&Typetype)
191 0, /*ob_size*/
192 "md5", /*tp_name*/
193 sizeof(md5object), /*tp_size*/
194 0, /*tp_itemsize*/
195 /* methods */
196 md5_dealloc, /*tp_dealloc*/
197 0, /*tp_print*/
198 md5_getattr, /*tp_getattr*/
199 0, /*tp_setattr*/
200 0, /*tp_compare*/
201 0, /*tp_repr*/
202 0, /*tp_as_number*/
203};
204
205/* List of functions exported by this module */
206
207static struct methodlist md5_functions[] = {
Sjoerd Mullender66bca321993-12-03 16:54:45 +0000208 {"md5", MD5_md5},
Guido van Rossum5f59d601992-12-14 16:59:51 +0000209 {NULL, NULL} /* Sentinel */
210};
211
212
213/* Initialize this module. */
214
215void
216initmd5()
217{
218#ifdef MD5_DEBUG
219 fputs( "initmd5() called...\n", stderr );
220#endif /* def MD5_DEBUG */
221 (void)initmodule("md5", md5_functions);
222} /* initmd5() */
223
224#ifdef MAKEDUMMYINT
225int _md5_dummy_int; /* XXX otherwise, we're .bss-less (DYNLOAD->Jack?) */
226#endif /* def MAKEDUMMYINT */