blob: 8bfbafb7201c11f63f933c2fe89d3a63174fa0df [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
44static const char initialiser_name[] = "md5";
45
46/* #define MD5_DEBUG */
47
48static md5object *
49newmd5object()
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
68static void
69md5_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
82static object *
83MD5_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 */
113static object *
114md5_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
Guido van Rossum13ecc7a1993-11-01 16:19:05 +0000131#define DIGESTLEN 16 /* this is used twice--walrus@umich.edu */
Guido van Rossum5f59d601992-12-14 16:59:51 +0000132static object *
133md5_digest(self, args)
134 md5object *self;
135 object *args;
136{
Guido van Rossum13ecc7a1993-11-01 16:19:05 +0000137
Guido van Rossum5f59d601992-12-14 16:59:51 +0000138 MD5_CTX mdContext;
Guido van Rossum13ecc7a1993-11-01 16:19:05 +0000139 char aDigest[DIGESTLEN];
140
Guido van Rossum5f59d601992-12-14 16:59:51 +0000141
142 if (!getnoarg(args))
143 return NULL;
144
145 /* make a temporary copy, and perform the final */
146 mdContext = self->md5;
Guido van Rossum13ecc7a1993-11-01 16:19:05 +0000147 MD5Final(aDigest, &mdContext);
Guido van Rossum5f59d601992-12-14 16:59:51 +0000148
Guido van Rossum13ecc7a1993-11-01 16:19:05 +0000149 return newsizedstringobject((char *)aDigest, DIGESTLEN);
Guido van Rossum5f59d601992-12-14 16:59:51 +0000150} /* md5_digest() */
Guido van Rossum13ecc7a1993-11-01 16:19:05 +0000151#undef DIGESTLEN
Guido van Rossum5f59d601992-12-14 16:59:51 +0000152
153static object *
154md5_copy(self, args)
155 md5object *self;
156 object *args;
157{
158 md5object *md5p;
159
160
161 if (!getnoarg(args))
162 return NULL;
163
164 if ((md5p = newmd5object()) == NULL)
165 return NULL;
166
167 md5p->md5 = self->md5;
168
169 return (object *)md5p;
170} /* md5_copy() */
171
172
173static struct methodlist md5_methods[] = {
174 {"update", md5_update},
175 {"digest", md5_digest},
176 {"copy", md5_copy},
177 {NULL, NULL} /* sentinel */
178};
179
180static object *
181md5_getattr(self, name)
182 md5object *self;
183 char *name;
184{
185 return findmethod(md5_methods, (object *)self, name);
186} /* md5_getattr() */
187
Guido van Rossum13ecc7a1993-11-01 16:19:05 +0000188#ifndef _AIX
189static
190#endif
191typeobject MD5type = {
Guido van Rossum5f59d601992-12-14 16:59:51 +0000192 OB_HEAD_INIT(&Typetype)
193 0, /*ob_size*/
194 "md5", /*tp_name*/
195 sizeof(md5object), /*tp_size*/
196 0, /*tp_itemsize*/
197 /* methods */
198 md5_dealloc, /*tp_dealloc*/
199 0, /*tp_print*/
200 md5_getattr, /*tp_getattr*/
201 0, /*tp_setattr*/
202 0, /*tp_compare*/
203 0, /*tp_repr*/
204 0, /*tp_as_number*/
205};
206
207/* List of functions exported by this module */
208
209static struct methodlist md5_functions[] = {
210 {initialiser_name, MD5_md5},
211 {NULL, NULL} /* Sentinel */
212};
213
214
215/* Initialize this module. */
216
217void
218initmd5()
219{
220#ifdef MD5_DEBUG
221 fputs( "initmd5() called...\n", stderr );
222#endif /* def MD5_DEBUG */
223 (void)initmodule("md5", md5_functions);
224} /* initmd5() */
225
226#ifdef MAKEDUMMYINT
227int _md5_dummy_int; /* XXX otherwise, we're .bss-less (DYNLOAD->Jack?) */
228#endif /* def MAKEDUMMYINT */