blob: 25b163c1794767210932df51ad05a3109a006e7d [file] [log] [blame]
Gregory P. Smith2f21eb32007-09-09 06:44:34 +00001/* MD5 module */
2
3/* This module provides an interface to the MD5 algorithm */
4
5/* See below for information about the original code this module was
6 based upon. Additional work performed by:
7
8 Andrew Kuchling (amk@amk.ca)
9 Greg Stein (gstein@lyra.org)
10 Trevor Perrin (trevp@trevp.net)
11
12 Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org)
13 Licensed to PSF under a Contributor Agreement.
14
15*/
16
17/* MD5 objects */
18
19#include "Python.h"
Gregory P. Smith365a1862009-02-12 07:35:29 +000020#include "hashlib.h"
Gregory P. Smith2f21eb32007-09-09 06:44:34 +000021
Martin v. Löwis501b13c2014-07-27 14:20:23 +020022/*[clinic input]
23module _md5
24class MD5Type "MD5object *" "&PyType_Type"
25[clinic start generated code]*/
26/*[clinic end generated code: output=da39a3ee5e6b4b0d input=6e5261719957a912]*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +000027
28/* Some useful types */
29
30#if SIZEOF_INT == 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000031typedef unsigned int MD5_INT32; /* 32-bit integer */
32typedef PY_LONG_LONG MD5_INT64; /* 64-bit integer */
Gregory P. Smith2f21eb32007-09-09 06:44:34 +000033#else
34/* not defined. compilation will die. */
35#endif
36
37/* The MD5 block size and message digest sizes, in bytes */
38
39#define MD5_BLOCKSIZE 64
40#define MD5_DIGESTSIZE 16
41
42/* The structure for storing MD5 info */
43
44struct md5_state {
45 MD5_INT64 length;
46 MD5_INT32 state[4], curlen;
47 unsigned char buf[MD5_BLOCKSIZE];
48};
49
50typedef struct {
51 PyObject_HEAD
52
53 struct md5_state hash_state;
54} MD5object;
55
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030056#include "clinic/md5module.c.h"
Gregory P. Smith2f21eb32007-09-09 06:44:34 +000057
58/* ------------------------------------------------------------------------
59 *
60 * This code for the MD5 algorithm was noted as public domain. The
61 * original headers are pasted below.
62 *
63 * Several changes have been made to make it more compatible with the
64 * Python environment and desired interface.
65 *
66 */
67
68/* LibTomCrypt, modular cryptographic library -- Tom St Denis
69 *
70 * LibTomCrypt is a library that provides various cryptographic
71 * algorithms in a highly modular and flexible manner.
72 *
73 * The library is free for all purposes without any express
74 * guarantee it works.
75 *
76 * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
77 */
78
79/* rotate the hard way (platform optimizations could be done) */
80#define ROLc(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
81
82/* Endian Neutral macros that work on all platforms */
83
84#define STORE32L(x, y) \
85 { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
86 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
87
88#define LOAD32L(x, y) \
89 { x = ((unsigned long)((y)[3] & 255)<<24) | \
90 ((unsigned long)((y)[2] & 255)<<16) | \
91 ((unsigned long)((y)[1] & 255)<<8) | \
92 ((unsigned long)((y)[0] & 255)); }
93
94#define STORE64L(x, y) \
95 { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \
96 (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \
97 (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
98 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
99
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000100
101/* MD5 macros */
102
103#define F(x,y,z) (z ^ (x & (y ^ z)))
104#define G(x,y,z) (y ^ (z & (y ^ x)))
105#define H(x,y,z) (x^y^z)
106#define I(x,y,z) (y^(x|(~z)))
107
108#define FF(a,b,c,d,M,s,t) \
109 a = (a + F(b,c,d) + M + t); a = ROLc(a, s) + b;
110
111#define GG(a,b,c,d,M,s,t) \
112 a = (a + G(b,c,d) + M + t); a = ROLc(a, s) + b;
113
114#define HH(a,b,c,d,M,s,t) \
115 a = (a + H(b,c,d) + M + t); a = ROLc(a, s) + b;
116
117#define II(a,b,c,d,M,s,t) \
118 a = (a + I(b,c,d) + M + t); a = ROLc(a, s) + b;
119
120
121static void md5_compress(struct md5_state *md5, unsigned char *buf)
122{
123 MD5_INT32 i, W[16], a, b, c, d;
124
125 assert(md5 != NULL);
126 assert(buf != NULL);
127
128 /* copy the state into 512-bits into W[0..15] */
129 for (i = 0; i < 16; i++) {
130 LOAD32L(W[i], buf + (4*i));
131 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000133 /* copy state */
134 a = md5->state[0];
135 b = md5->state[1];
136 c = md5->state[2];
137 d = md5->state[3];
138
139 FF(a,b,c,d,W[0],7,0xd76aa478UL)
140 FF(d,a,b,c,W[1],12,0xe8c7b756UL)
141 FF(c,d,a,b,W[2],17,0x242070dbUL)
142 FF(b,c,d,a,W[3],22,0xc1bdceeeUL)
143 FF(a,b,c,d,W[4],7,0xf57c0fafUL)
144 FF(d,a,b,c,W[5],12,0x4787c62aUL)
145 FF(c,d,a,b,W[6],17,0xa8304613UL)
146 FF(b,c,d,a,W[7],22,0xfd469501UL)
147 FF(a,b,c,d,W[8],7,0x698098d8UL)
148 FF(d,a,b,c,W[9],12,0x8b44f7afUL)
149 FF(c,d,a,b,W[10],17,0xffff5bb1UL)
150 FF(b,c,d,a,W[11],22,0x895cd7beUL)
151 FF(a,b,c,d,W[12],7,0x6b901122UL)
152 FF(d,a,b,c,W[13],12,0xfd987193UL)
153 FF(c,d,a,b,W[14],17,0xa679438eUL)
154 FF(b,c,d,a,W[15],22,0x49b40821UL)
155 GG(a,b,c,d,W[1],5,0xf61e2562UL)
156 GG(d,a,b,c,W[6],9,0xc040b340UL)
157 GG(c,d,a,b,W[11],14,0x265e5a51UL)
158 GG(b,c,d,a,W[0],20,0xe9b6c7aaUL)
159 GG(a,b,c,d,W[5],5,0xd62f105dUL)
160 GG(d,a,b,c,W[10],9,0x02441453UL)
161 GG(c,d,a,b,W[15],14,0xd8a1e681UL)
162 GG(b,c,d,a,W[4],20,0xe7d3fbc8UL)
163 GG(a,b,c,d,W[9],5,0x21e1cde6UL)
164 GG(d,a,b,c,W[14],9,0xc33707d6UL)
165 GG(c,d,a,b,W[3],14,0xf4d50d87UL)
166 GG(b,c,d,a,W[8],20,0x455a14edUL)
167 GG(a,b,c,d,W[13],5,0xa9e3e905UL)
168 GG(d,a,b,c,W[2],9,0xfcefa3f8UL)
169 GG(c,d,a,b,W[7],14,0x676f02d9UL)
170 GG(b,c,d,a,W[12],20,0x8d2a4c8aUL)
171 HH(a,b,c,d,W[5],4,0xfffa3942UL)
172 HH(d,a,b,c,W[8],11,0x8771f681UL)
173 HH(c,d,a,b,W[11],16,0x6d9d6122UL)
174 HH(b,c,d,a,W[14],23,0xfde5380cUL)
175 HH(a,b,c,d,W[1],4,0xa4beea44UL)
176 HH(d,a,b,c,W[4],11,0x4bdecfa9UL)
177 HH(c,d,a,b,W[7],16,0xf6bb4b60UL)
178 HH(b,c,d,a,W[10],23,0xbebfbc70UL)
179 HH(a,b,c,d,W[13],4,0x289b7ec6UL)
180 HH(d,a,b,c,W[0],11,0xeaa127faUL)
181 HH(c,d,a,b,W[3],16,0xd4ef3085UL)
182 HH(b,c,d,a,W[6],23,0x04881d05UL)
183 HH(a,b,c,d,W[9],4,0xd9d4d039UL)
184 HH(d,a,b,c,W[12],11,0xe6db99e5UL)
185 HH(c,d,a,b,W[15],16,0x1fa27cf8UL)
186 HH(b,c,d,a,W[2],23,0xc4ac5665UL)
187 II(a,b,c,d,W[0],6,0xf4292244UL)
188 II(d,a,b,c,W[7],10,0x432aff97UL)
189 II(c,d,a,b,W[14],15,0xab9423a7UL)
190 II(b,c,d,a,W[5],21,0xfc93a039UL)
191 II(a,b,c,d,W[12],6,0x655b59c3UL)
192 II(d,a,b,c,W[3],10,0x8f0ccc92UL)
193 II(c,d,a,b,W[10],15,0xffeff47dUL)
194 II(b,c,d,a,W[1],21,0x85845dd1UL)
195 II(a,b,c,d,W[8],6,0x6fa87e4fUL)
196 II(d,a,b,c,W[15],10,0xfe2ce6e0UL)
197 II(c,d,a,b,W[6],15,0xa3014314UL)
198 II(b,c,d,a,W[13],21,0x4e0811a1UL)
199 II(a,b,c,d,W[4],6,0xf7537e82UL)
200 II(d,a,b,c,W[11],10,0xbd3af235UL)
201 II(c,d,a,b,W[2],15,0x2ad7d2bbUL)
202 II(b,c,d,a,W[9],21,0xeb86d391UL)
203
204 md5->state[0] = md5->state[0] + a;
205 md5->state[1] = md5->state[1] + b;
206 md5->state[2] = md5->state[2] + c;
207 md5->state[3] = md5->state[3] + d;
208}
209
210
211/**
212 Initialize the hash state
213 @param sha1 The hash state you wish to initialize
214*/
doko@ubuntu.comc2b46732012-06-21 17:26:06 +0200215static void
216md5_init(struct md5_state *md5)
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000217{
218 assert(md5 != NULL);
219 md5->state[0] = 0x67452301UL;
220 md5->state[1] = 0xefcdab89UL;
221 md5->state[2] = 0x98badcfeUL;
222 md5->state[3] = 0x10325476UL;
223 md5->curlen = 0;
224 md5->length = 0;
225}
226
227/**
228 Process a block of memory though the hash
229 @param sha1 The hash state
230 @param in The data to hash
231 @param inlen The length of the data (octets)
232*/
doko@ubuntu.comc2b46732012-06-21 17:26:06 +0200233static void
234md5_process(struct md5_state *md5, const unsigned char *in, Py_ssize_t inlen)
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000235{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000236 Py_ssize_t n;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000237
238 assert(md5 != NULL);
239 assert(in != NULL);
240 assert(md5->curlen <= sizeof(md5->buf));
241
242 while (inlen > 0) {
243 if (md5->curlen == 0 && inlen >= MD5_BLOCKSIZE) {
244 md5_compress(md5, (unsigned char *)in);
245 md5->length += MD5_BLOCKSIZE * 8;
246 in += MD5_BLOCKSIZE;
247 inlen -= MD5_BLOCKSIZE;
248 } else {
Victor Stinner640c35c2013-06-04 23:14:37 +0200249 n = Py_MIN(inlen, (Py_ssize_t)(MD5_BLOCKSIZE - md5->curlen));
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000250 memcpy(md5->buf + md5->curlen, in, (size_t)n);
Victor Stinner56cb1252012-10-31 00:33:57 +0100251 md5->curlen += (MD5_INT32)n;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000252 in += n;
253 inlen -= n;
254 if (md5->curlen == MD5_BLOCKSIZE) {
255 md5_compress(md5, md5->buf);
256 md5->length += 8*MD5_BLOCKSIZE;
257 md5->curlen = 0;
258 }
259 }
260 }
261}
262
263/**
264 Terminate the hash to get the digest
265 @param sha1 The hash state
266 @param out [out] The destination of the hash (16 bytes)
267*/
doko@ubuntu.comc2b46732012-06-21 17:26:06 +0200268static void
269md5_done(struct md5_state *md5, unsigned char *out)
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000270{
271 int i;
272
273 assert(md5 != NULL);
274 assert(out != NULL);
275 assert(md5->curlen < sizeof(md5->buf));
276
277 /* increase the length of the message */
278 md5->length += md5->curlen * 8;
279
280 /* append the '1' bit */
281 md5->buf[md5->curlen++] = (unsigned char)0x80;
282
283 /* if the length is currently above 56 bytes we append zeros
284 * then compress. Then we can fall back to padding zeros and length
285 * encoding like normal.
286 */
287 if (md5->curlen > 56) {
288 while (md5->curlen < 64) {
289 md5->buf[md5->curlen++] = (unsigned char)0;
290 }
291 md5_compress(md5, md5->buf);
292 md5->curlen = 0;
293 }
294
295 /* pad upto 56 bytes of zeroes */
296 while (md5->curlen < 56) {
297 md5->buf[md5->curlen++] = (unsigned char)0;
298 }
299
300 /* store length */
301 STORE64L(md5->length, md5->buf+56);
302 md5_compress(md5, md5->buf);
303
304 /* copy output */
305 for (i = 0; i < 4; i++) {
306 STORE32L(md5->state[i], out+(4*i));
307 }
308}
309
310/* .Source: /cvs/libtom/libtomcrypt/src/hashes/md5.c,v $ */
311/* .Revision: 1.10 $ */
312/* .Date: 2007/05/12 14:25:28 $ */
313
314/*
315 * End of copied MD5 code.
316 *
317 * ------------------------------------------------------------------------
318 */
319
320static PyTypeObject MD5type;
321
322
323static MD5object *
324newMD5object(void)
325{
326 return (MD5object *)PyObject_New(MD5object, &MD5type);
327}
328
329
330/* Internal methods for a hash object */
331
332static void
333MD5_dealloc(PyObject *ptr)
334{
335 PyObject_Del(ptr);
336}
337
338
339/* External methods for a hash object */
340
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200341/*[clinic input]
342MD5Type.copy
343
344Return a copy of the hash object.
345[clinic start generated code]*/
346
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200347static PyObject *
348MD5Type_copy_impl(MD5object *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300349/*[clinic end generated code: output=596eb36852f02071 input=2c09e6d2493f3079]*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000350{
351 MD5object *newobj;
352
Christian Heimes90aa7642007-12-19 02:45:37 +0000353 if (Py_TYPE(self) == &MD5type) {
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000354 if ( (newobj = newMD5object())==NULL)
355 return NULL;
356 } else {
357 if ( (newobj = newMD5object())==NULL)
358 return NULL;
359 }
360
361 newobj->hash_state = self->hash_state;
362 return (PyObject *)newobj;
363}
364
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200365/*[clinic input]
366MD5Type.digest
367
368Return the digest value as a string of binary data.
369[clinic start generated code]*/
370
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200371static PyObject *
372MD5Type_digest_impl(MD5object *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300373/*[clinic end generated code: output=eb691dc4190a07ec input=7b96e65389412a34]*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000374{
375 unsigned char digest[MD5_DIGESTSIZE];
376 struct md5_state temp;
377
378 temp = self->hash_state;
379 md5_done(&temp, digest);
Christian Heimes72b710a2008-05-26 13:28:38 +0000380 return PyBytes_FromStringAndSize((const char *)digest, MD5_DIGESTSIZE);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000381}
382
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200383/*[clinic input]
384MD5Type.hexdigest
385
386Return the digest value as a string of hexadecimal digits.
387[clinic start generated code]*/
388
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200389static PyObject *
390MD5Type_hexdigest_impl(MD5object *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300391/*[clinic end generated code: output=17badced1f3ac932 input=b60b19de644798dd]*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000392{
393 unsigned char digest[MD5_DIGESTSIZE];
394 struct md5_state temp;
395 PyObject *retval;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200396 Py_UCS1 *hex_digest;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000397 int i, j;
398
399 /* Get the raw (binary) digest value */
400 temp = self->hash_state;
401 md5_done(&temp, digest);
402
403 /* Create a new string */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200404 retval = PyUnicode_New(MD5_DIGESTSIZE * 2, 127);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000405 if (!retval)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200407 hex_digest = PyUnicode_1BYTE_DATA(retval);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000408
409 /* Make hex version of the digest */
410 for(i=j=0; i<MD5_DIGESTSIZE; i++) {
Victor Stinnerf5cff562011-10-14 02:13:11 +0200411 unsigned char c;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000412 c = (digest[i] >> 4) & 0xf;
Victor Stinnerf5cff562011-10-14 02:13:11 +0200413 hex_digest[j++] = Py_hexdigits[c];
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000414 c = (digest[i] & 0xf);
Victor Stinnerf5cff562011-10-14 02:13:11 +0200415 hex_digest[j++] = Py_hexdigits[c];
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000416 }
Christian Heimesf402e922013-01-03 09:21:55 +0100417#ifdef Py_DEBUG
Victor Stinner8f825062012-04-27 13:55:39 +0200418 assert(_PyUnicode_CheckConsistency(retval, 1));
Christian Heimesf402e922013-01-03 09:21:55 +0100419#endif
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000420 return retval;
421}
422
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200423/*[clinic input]
424MD5Type.update
425
426 obj: object
427 /
428
429Update this hash object's state with the provided string.
430[clinic start generated code]*/
431
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000432static PyObject *
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200433MD5Type_update(MD5object *self, PyObject *obj)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300434/*[clinic end generated code: output=f6ad168416338423 input=6e1efcd9ecf17032]*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000435{
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000436 Py_buffer buf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437
Gregory P. Smith365a1862009-02-12 07:35:29 +0000438 GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
439
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000440 md5_process(&self->hash_state, buf.buf, buf.len);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000441
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000442 PyBuffer_Release(&buf);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000443 Py_INCREF(Py_None);
444 return Py_None;
445}
446
447static PyMethodDef MD5_methods[] = {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200448 MD5TYPE_COPY_METHODDEF
449 MD5TYPE_DIGEST_METHODDEF
450 MD5TYPE_HEXDIGEST_METHODDEF
451 MD5TYPE_UPDATE_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 {NULL, NULL} /* sentinel */
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000453};
454
455static PyObject *
456MD5_get_block_size(PyObject *self, void *closure)
457{
Christian Heimes217cfd12007-12-02 14:31:20 +0000458 return PyLong_FromLong(MD5_BLOCKSIZE);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000459}
460
461static PyObject *
462MD5_get_name(PyObject *self, void *closure)
463{
Christian Heimes37d5ceb2013-08-15 18:31:48 +0200464 return PyUnicode_FromStringAndSize("md5", 3);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000465}
466
467static PyObject *
468md5_get_digest_size(PyObject *self, void *closure)
469{
Christian Heimes217cfd12007-12-02 14:31:20 +0000470 return PyLong_FromLong(MD5_DIGESTSIZE);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000471}
472
473
474static PyGetSetDef MD5_getseters[] = {
475 {"block_size",
476 (getter)MD5_get_block_size, NULL,
477 NULL,
478 NULL},
479 {"name",
480 (getter)MD5_get_name, NULL,
481 NULL,
482 NULL},
483 {"digest_size",
484 (getter)md5_get_digest_size, NULL,
485 NULL,
486 NULL},
487 {NULL} /* Sentinel */
488};
489
490static PyTypeObject MD5type = {
491 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 "_md5.md5", /*tp_name*/
493 sizeof(MD5object), /*tp_size*/
494 0, /*tp_itemsize*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000495 /* methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 MD5_dealloc, /*tp_dealloc*/
497 0, /*tp_print*/
498 0, /*tp_getattr*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000499 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000500 0, /*tp_reserved*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000501 0, /*tp_repr*/
502 0, /*tp_as_number*/
503 0, /*tp_as_sequence*/
504 0, /*tp_as_mapping*/
505 0, /*tp_hash*/
506 0, /*tp_call*/
507 0, /*tp_str*/
508 0, /*tp_getattro*/
509 0, /*tp_setattro*/
510 0, /*tp_as_buffer*/
511 Py_TPFLAGS_DEFAULT, /*tp_flags*/
512 0, /*tp_doc*/
513 0, /*tp_traverse*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 0, /*tp_clear*/
515 0, /*tp_richcompare*/
516 0, /*tp_weaklistoffset*/
517 0, /*tp_iter*/
518 0, /*tp_iternext*/
519 MD5_methods, /* tp_methods */
520 NULL, /* tp_members */
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000521 MD5_getseters, /* tp_getset */
522};
523
524
525/* The single module-level function: new() */
526
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200527/*[clinic input]
528_md5.md5
529
530 string: object(c_default="NULL") = b''
531
532Return a new MD5 hash object; optionally initialized with a string.
533[clinic start generated code]*/
534
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200535static PyObject *
536_md5_md5_impl(PyModuleDef *module, PyObject *string)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300537/*[clinic end generated code: output=3527436a2090b956 input=d12ef8f72d684f7b]*/
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200538{
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000539 MD5object *new;
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000540 Py_buffer buf;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000541
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200542 if (string)
543 GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
Gregory P. Smith365a1862009-02-12 07:35:29 +0000544
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000545 if ((new = newMD5object()) == NULL) {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200546 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000547 PyBuffer_Release(&buf);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000548 return NULL;
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000549 }
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000550
551 md5_init(&new->hash_state);
552
553 if (PyErr_Occurred()) {
554 Py_DECREF(new);
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200555 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000556 PyBuffer_Release(&buf);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000557 return NULL;
558 }
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200559 if (string) {
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000560 md5_process(&new->hash_state, buf.buf, buf.len);
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000561 PyBuffer_Release(&buf);
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000562 }
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000563
564 return (PyObject *)new;
565}
566
567
568/* List of functions exported by this module */
569
570static struct PyMethodDef MD5_functions[] = {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200571 _MD5_MD5_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 {NULL, NULL} /* Sentinel */
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000573};
574
575
576/* Initialize this module. */
577
578#define insint(n,v) { PyModule_AddIntConstant(m,n,v); }
579
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000580
Martin v. Löwis1a214512008-06-11 05:26:20 +0000581static struct PyModuleDef _md5module = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 PyModuleDef_HEAD_INIT,
583 "_md5",
584 NULL,
585 -1,
586 MD5_functions,
587 NULL,
588 NULL,
589 NULL,
590 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000591};
592
593PyMODINIT_FUNC
594PyInit__md5(void)
595{
Christian Heimes327dd732013-10-22 15:05:23 +0200596 PyObject *m;
597
Christian Heimes90aa7642007-12-19 02:45:37 +0000598 Py_TYPE(&MD5type) = &PyType_Type;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000599 if (PyType_Ready(&MD5type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000600 return NULL;
Christian Heimes327dd732013-10-22 15:05:23 +0200601
602 m = PyModule_Create(&_md5module);
603 if (m == NULL)
604 return NULL;
605
606 Py_INCREF((PyObject *)&MD5type);
607 PyModule_AddObject(m, "MD5Type", (PyObject *)&MD5type);
608 return m;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000609}