1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI not be used in advertising or publicity pertaining to
13 distribution of the software without specific, written prior permission.
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ******************************************************************/
27 /* 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.) */
35 #include "allobjects.h"
36 #include "modsupport.h"
42 MD5_CTX md5
; /* the context holder */
45 staticforward typeobject MD5type
;
47 #define is_md5object(v) ((v)->ob_type == &MD5type)
54 md5p
= NEWOBJ(md5object
, &MD5type
);
58 MD5Init(&md5p
->md5
); /* actual initialisation */
73 /* MD5 methods-as-attributes */
76 md5_update(self
, args
)
83 if (!getargs(args
, "s#", &cp
, &len
))
86 MD5Update(&self
->md5
, cp
, len
);
93 md5_digest(self
, args
)
99 unsigned char aDigest
[16];
104 /* make a temporary copy, and perform the final */
105 mdContext
= self
->md5
;
106 MD5Final(aDigest
, &mdContext
);
108 return newsizedstringobject((char *)aDigest
, 16);
121 if ((md5p
= newmd5object()) == NULL
)
124 md5p
->md5
= self
->md5
;
126 return (object
*)md5p
;
129 static struct methodlist md5_methods
[] = {
130 {"update", (method
)md5_update
},
131 {"digest", (method
)md5_digest
},
132 {"copy", (method
)md5_copy
},
133 {NULL
, NULL
} /* sentinel */
137 md5_getattr(self
, name
)
141 return findmethod(md5_methods
, (object
*)self
, name
);
144 static typeobject MD5type
= {
145 OB_HEAD_INIT(&Typetype
)
148 sizeof(md5object
), /*tp_size*/
151 (destructor
)md5_dealloc
, /*tp_dealloc*/
153 (getattrfunc
)md5_getattr
, /*tp_getattr*/
169 unsigned char *cp
= NULL
;
172 if (!getargs(args
, "")) {
174 if (!getargs(args
, "s#", &cp
, &len
))
178 if ((md5p
= newmd5object()) == NULL
)
182 MD5Update(&md5p
->md5
, cp
, len
);
184 return (object
*)md5p
;
188 /* List of functions exported by this module */
190 static struct methodlist md5_functions
[] = {
191 {"new", (method
)MD5_new
},
192 {"md5", (method
)MD5_new
}, /* Backward compatibility */
193 {NULL
, NULL
} /* Sentinel */
197 /* Initialize this module. */
202 (void)initmodule("md5", md5_functions
);