blob: 51c6f6d88502479146e0279fc23ada57c040a285 [file] [log] [blame]
Guido van Rossum5f59d601992-12-14 16:59:51 +00001/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossum5f59d601992-12-14 16:59:51 +00004
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******************************************************************/
Guido van Rossumb6775db1994-08-01 11:34:53 +000024
Guido van Rossum5f59d601992-12-14 16:59:51 +000025/* MD5 module */
26
Guido van Rossumb6775db1994-08-01 11:34:53 +000027/* 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 Rossum5f59d601992-12-14 16:59:51 +000032
33/* MD5 objects */
34
35#include "allobjects.h"
Guido van Rossumb6775db1994-08-01 11:34:53 +000036#include "modsupport.h"
Guido van Rossum5f59d601992-12-14 16:59:51 +000037
38#include "md5.h"
Guido van Rossumb6775db1994-08-01 11:34:53 +000039
Guido van Rossum5f59d601992-12-14 16:59:51 +000040typedef struct {
41 OB_HEAD
42 MD5_CTX md5; /* the context holder */
43} md5object;
44
Guido van Rossumb6775db1994-08-01 11:34:53 +000045staticforward typeobject MD5type;
Guido van Rossum5f59d601992-12-14 16:59:51 +000046
47#define is_md5object(v) ((v)->ob_type == &MD5type)
48
Guido van Rossum5f59d601992-12-14 16:59:51 +000049static md5object *
50newmd5object()
51{
52 md5object *md5p;
53
Guido van Rossum5f59d601992-12-14 16:59:51 +000054 md5p = NEWOBJ(md5object, &MD5type);
55 if (md5p == NULL)
56 return NULL;
57
58 MD5Init(&md5p->md5); /* actual initialisation */
59 return md5p;
Guido van Rossumb6775db1994-08-01 11:34:53 +000060}
Guido van Rossum5f59d601992-12-14 16:59:51 +000061
62
63/* MD5 methods */
64
65static void
66md5_dealloc(md5p)
67 md5object *md5p;
68{
Guido van Rossum5f59d601992-12-14 16:59:51 +000069 DEL(md5p);
Guido van Rossumb6775db1994-08-01 11:34:53 +000070}
Guido van Rossum5f59d601992-12-14 16:59:51 +000071
72
Guido van Rossumb6775db1994-08-01 11:34:53 +000073/* MD5 methods-as-attributes */
74
75static object *
76md5_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
92static object *
93md5_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
111static object *
112md5_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
129static 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
136static object *
137md5_getattr(self, name)
138 md5object *self;
139 char *name;
140{
141 return findmethod(md5_methods, (object *)self, name);
142}
143
Guido van Rossuma320fd31995-03-09 12:14:15 +0000144statichere typeobject MD5type = {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000145 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 Rossum5f59d601992-12-14 16:59:51 +0000162
163static object *
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000164MD5_new(self, args)
Guido van Rossum5f59d601992-12-14 16:59:51 +0000165 object *self;
166 object *args;
167{
168 md5object *md5p;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000169 unsigned char *cp = NULL;
Guido van Rossumef38b781995-07-26 17:33:10 +0000170 int len = 0;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000171
Guido van Rossumef38b781995-07-26 17:33:10 +0000172 if (!newgetargs(args, "|s#", &cp, &len))
173 return NULL;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000174
175 if ((md5p = newmd5object()) == NULL)
176 return NULL;
177
178 if (cp)
179 MD5Update(&md5p->md5, cp, len);
180
181 return (object *)md5p;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000182}
Guido van Rossum5f59d601992-12-14 16:59:51 +0000183
184
Guido van Rossum5f59d601992-12-14 16:59:51 +0000185/* List of functions exported by this module */
186
187static struct methodlist md5_functions[] = {
Guido van Rossumef38b781995-07-26 17:33:10 +0000188 {"new", (method)MD5_new, 1},
189 {"md5", (method)MD5_new, 1}, /* Backward compatibility */
190 {NULL, NULL} /* Sentinel */
Guido van Rossum5f59d601992-12-14 16:59:51 +0000191};
192
193
194/* Initialize this module. */
195
196void
197initmd5()
198{
Guido van Rossum5f59d601992-12-14 16:59:51 +0000199 (void)initmodule("md5", md5_functions);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000200}