blob: ae659edbaf0316c61b727c06fb9fee1bac7cadc7 [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
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossum5f59d601992-12-14 16:59:51 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossum5f59d601992-12-14 16:59:51 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossum5f59d601992-12-14 16:59:51 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossum5f59d601992-12-14 16:59:51 +000029
30******************************************************************/
Guido van Rossumb6775db1994-08-01 11:34:53 +000031
Guido van Rossum5f59d601992-12-14 16:59:51 +000032/* MD5 module */
33
Guido van Rossumb6775db1994-08-01 11:34:53 +000034/* 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 Rossum5f59d601992-12-14 16:59:51 +000039
40/* MD5 objects */
41
42#include "allobjects.h"
Guido van Rossumb6775db1994-08-01 11:34:53 +000043#include "modsupport.h"
Guido van Rossum5f59d601992-12-14 16:59:51 +000044
45#include "md5.h"
Guido van Rossumb6775db1994-08-01 11:34:53 +000046
Guido van Rossum5f59d601992-12-14 16:59:51 +000047typedef struct {
48 OB_HEAD
49 MD5_CTX md5; /* the context holder */
50} md5object;
51
Guido van Rossumb6775db1994-08-01 11:34:53 +000052staticforward typeobject MD5type;
Guido van Rossum5f59d601992-12-14 16:59:51 +000053
54#define is_md5object(v) ((v)->ob_type == &MD5type)
55
Guido van Rossum5f59d601992-12-14 16:59:51 +000056static md5object *
57newmd5object()
58{
59 md5object *md5p;
60
Guido van Rossum5f59d601992-12-14 16:59:51 +000061 md5p = NEWOBJ(md5object, &MD5type);
62 if (md5p == NULL)
63 return NULL;
64
65 MD5Init(&md5p->md5); /* actual initialisation */
66 return md5p;
Guido van Rossumb6775db1994-08-01 11:34:53 +000067}
Guido van Rossum5f59d601992-12-14 16:59:51 +000068
69
70/* MD5 methods */
71
72static void
73md5_dealloc(md5p)
74 md5object *md5p;
75{
Guido van Rossum5f59d601992-12-14 16:59:51 +000076 DEL(md5p);
Guido van Rossumb6775db1994-08-01 11:34:53 +000077}
Guido van Rossum5f59d601992-12-14 16:59:51 +000078
79
Guido van Rossumb6775db1994-08-01 11:34:53 +000080/* MD5 methods-as-attributes */
81
82static object *
83md5_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
99static object *
100md5_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
118static object *
119md5_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
136static 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
143static object *
144md5_getattr(self, name)
145 md5object *self;
146 char *name;
147{
148 return findmethod(md5_methods, (object *)self, name);
149}
150
Guido van Rossuma320fd31995-03-09 12:14:15 +0000151statichere typeobject MD5type = {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000152 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 Rossum5f59d601992-12-14 16:59:51 +0000169
170static object *
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000171MD5_new(self, args)
Guido van Rossum5f59d601992-12-14 16:59:51 +0000172 object *self;
173 object *args;
174{
175 md5object *md5p;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000176 unsigned char *cp = NULL;
Guido van Rossumef38b781995-07-26 17:33:10 +0000177 int len = 0;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000178
Guido van Rossumef38b781995-07-26 17:33:10 +0000179 if (!newgetargs(args, "|s#", &cp, &len))
180 return NULL;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000181
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 Rossumb6775db1994-08-01 11:34:53 +0000189}
Guido van Rossum5f59d601992-12-14 16:59:51 +0000190
191
Guido van Rossum5f59d601992-12-14 16:59:51 +0000192/* List of functions exported by this module */
193
194static struct methodlist md5_functions[] = {
Guido van Rossumef38b781995-07-26 17:33:10 +0000195 {"new", (method)MD5_new, 1},
196 {"md5", (method)MD5_new, 1}, /* Backward compatibility */
197 {NULL, NULL} /* Sentinel */
Guido van Rossum5f59d601992-12-14 16:59:51 +0000198};
199
200
201/* Initialize this module. */
202
203void
204initmd5()
205{
Guido van Rossum5f59d601992-12-14 16:59:51 +0000206 (void)initmodule("md5", md5_functions);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000207}