blob: 65f5c35a620311fc69cc2ff84f01515fd8d5fec0 [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
56
57/* ------------------------------------------------------------------------
58 *
59 * This code for the MD5 algorithm was noted as public domain. The
60 * original headers are pasted below.
61 *
62 * Several changes have been made to make it more compatible with the
63 * Python environment and desired interface.
64 *
65 */
66
67/* LibTomCrypt, modular cryptographic library -- Tom St Denis
68 *
69 * LibTomCrypt is a library that provides various cryptographic
70 * algorithms in a highly modular and flexible manner.
71 *
72 * The library is free for all purposes without any express
73 * guarantee it works.
74 *
75 * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
76 */
77
78/* rotate the hard way (platform optimizations could be done) */
79#define ROLc(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
80
81/* Endian Neutral macros that work on all platforms */
82
83#define STORE32L(x, y) \
84 { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
85 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
86
87#define LOAD32L(x, y) \
88 { x = ((unsigned long)((y)[3] & 255)<<24) | \
89 ((unsigned long)((y)[2] & 255)<<16) | \
90 ((unsigned long)((y)[1] & 255)<<8) | \
91 ((unsigned long)((y)[0] & 255)); }
92
93#define STORE64L(x, y) \
94 { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \
95 (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \
96 (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
97 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
98
Gregory P. Smith2f21eb32007-09-09 06:44:34 +000099
100/* MD5 macros */
101
102#define F(x,y,z) (z ^ (x & (y ^ z)))
103#define G(x,y,z) (y ^ (z & (y ^ x)))
104#define H(x,y,z) (x^y^z)
105#define I(x,y,z) (y^(x|(~z)))
106
107#define FF(a,b,c,d,M,s,t) \
108 a = (a + F(b,c,d) + M + t); a = ROLc(a, s) + b;
109
110#define GG(a,b,c,d,M,s,t) \
111 a = (a + G(b,c,d) + M + t); a = ROLc(a, s) + b;
112
113#define HH(a,b,c,d,M,s,t) \
114 a = (a + H(b,c,d) + M + t); a = ROLc(a, s) + b;
115
116#define II(a,b,c,d,M,s,t) \
117 a = (a + I(b,c,d) + M + t); a = ROLc(a, s) + b;
118
119
120static void md5_compress(struct md5_state *md5, unsigned char *buf)
121{
122 MD5_INT32 i, W[16], a, b, c, d;
123
124 assert(md5 != NULL);
125 assert(buf != NULL);
126
127 /* copy the state into 512-bits into W[0..15] */
128 for (i = 0; i < 16; i++) {
129 LOAD32L(W[i], buf + (4*i));
130 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000132 /* copy state */
133 a = md5->state[0];
134 b = md5->state[1];
135 c = md5->state[2];
136 d = md5->state[3];
137
138 FF(a,b,c,d,W[0],7,0xd76aa478UL)
139 FF(d,a,b,c,W[1],12,0xe8c7b756UL)
140 FF(c,d,a,b,W[2],17,0x242070dbUL)
141 FF(b,c,d,a,W[3],22,0xc1bdceeeUL)
142 FF(a,b,c,d,W[4],7,0xf57c0fafUL)
143 FF(d,a,b,c,W[5],12,0x4787c62aUL)
144 FF(c,d,a,b,W[6],17,0xa8304613UL)
145 FF(b,c,d,a,W[7],22,0xfd469501UL)
146 FF(a,b,c,d,W[8],7,0x698098d8UL)
147 FF(d,a,b,c,W[9],12,0x8b44f7afUL)
148 FF(c,d,a,b,W[10],17,0xffff5bb1UL)
149 FF(b,c,d,a,W[11],22,0x895cd7beUL)
150 FF(a,b,c,d,W[12],7,0x6b901122UL)
151 FF(d,a,b,c,W[13],12,0xfd987193UL)
152 FF(c,d,a,b,W[14],17,0xa679438eUL)
153 FF(b,c,d,a,W[15],22,0x49b40821UL)
154 GG(a,b,c,d,W[1],5,0xf61e2562UL)
155 GG(d,a,b,c,W[6],9,0xc040b340UL)
156 GG(c,d,a,b,W[11],14,0x265e5a51UL)
157 GG(b,c,d,a,W[0],20,0xe9b6c7aaUL)
158 GG(a,b,c,d,W[5],5,0xd62f105dUL)
159 GG(d,a,b,c,W[10],9,0x02441453UL)
160 GG(c,d,a,b,W[15],14,0xd8a1e681UL)
161 GG(b,c,d,a,W[4],20,0xe7d3fbc8UL)
162 GG(a,b,c,d,W[9],5,0x21e1cde6UL)
163 GG(d,a,b,c,W[14],9,0xc33707d6UL)
164 GG(c,d,a,b,W[3],14,0xf4d50d87UL)
165 GG(b,c,d,a,W[8],20,0x455a14edUL)
166 GG(a,b,c,d,W[13],5,0xa9e3e905UL)
167 GG(d,a,b,c,W[2],9,0xfcefa3f8UL)
168 GG(c,d,a,b,W[7],14,0x676f02d9UL)
169 GG(b,c,d,a,W[12],20,0x8d2a4c8aUL)
170 HH(a,b,c,d,W[5],4,0xfffa3942UL)
171 HH(d,a,b,c,W[8],11,0x8771f681UL)
172 HH(c,d,a,b,W[11],16,0x6d9d6122UL)
173 HH(b,c,d,a,W[14],23,0xfde5380cUL)
174 HH(a,b,c,d,W[1],4,0xa4beea44UL)
175 HH(d,a,b,c,W[4],11,0x4bdecfa9UL)
176 HH(c,d,a,b,W[7],16,0xf6bb4b60UL)
177 HH(b,c,d,a,W[10],23,0xbebfbc70UL)
178 HH(a,b,c,d,W[13],4,0x289b7ec6UL)
179 HH(d,a,b,c,W[0],11,0xeaa127faUL)
180 HH(c,d,a,b,W[3],16,0xd4ef3085UL)
181 HH(b,c,d,a,W[6],23,0x04881d05UL)
182 HH(a,b,c,d,W[9],4,0xd9d4d039UL)
183 HH(d,a,b,c,W[12],11,0xe6db99e5UL)
184 HH(c,d,a,b,W[15],16,0x1fa27cf8UL)
185 HH(b,c,d,a,W[2],23,0xc4ac5665UL)
186 II(a,b,c,d,W[0],6,0xf4292244UL)
187 II(d,a,b,c,W[7],10,0x432aff97UL)
188 II(c,d,a,b,W[14],15,0xab9423a7UL)
189 II(b,c,d,a,W[5],21,0xfc93a039UL)
190 II(a,b,c,d,W[12],6,0x655b59c3UL)
191 II(d,a,b,c,W[3],10,0x8f0ccc92UL)
192 II(c,d,a,b,W[10],15,0xffeff47dUL)
193 II(b,c,d,a,W[1],21,0x85845dd1UL)
194 II(a,b,c,d,W[8],6,0x6fa87e4fUL)
195 II(d,a,b,c,W[15],10,0xfe2ce6e0UL)
196 II(c,d,a,b,W[6],15,0xa3014314UL)
197 II(b,c,d,a,W[13],21,0x4e0811a1UL)
198 II(a,b,c,d,W[4],6,0xf7537e82UL)
199 II(d,a,b,c,W[11],10,0xbd3af235UL)
200 II(c,d,a,b,W[2],15,0x2ad7d2bbUL)
201 II(b,c,d,a,W[9],21,0xeb86d391UL)
202
203 md5->state[0] = md5->state[0] + a;
204 md5->state[1] = md5->state[1] + b;
205 md5->state[2] = md5->state[2] + c;
206 md5->state[3] = md5->state[3] + d;
207}
208
209
210/**
211 Initialize the hash state
212 @param sha1 The hash state you wish to initialize
213*/
doko@ubuntu.comc2b46732012-06-21 17:26:06 +0200214static void
215md5_init(struct md5_state *md5)
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000216{
217 assert(md5 != NULL);
218 md5->state[0] = 0x67452301UL;
219 md5->state[1] = 0xefcdab89UL;
220 md5->state[2] = 0x98badcfeUL;
221 md5->state[3] = 0x10325476UL;
222 md5->curlen = 0;
223 md5->length = 0;
224}
225
226/**
227 Process a block of memory though the hash
228 @param sha1 The hash state
229 @param in The data to hash
230 @param inlen The length of the data (octets)
231*/
doko@ubuntu.comc2b46732012-06-21 17:26:06 +0200232static void
233md5_process(struct md5_state *md5, const unsigned char *in, Py_ssize_t inlen)
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000234{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000235 Py_ssize_t n;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000236
237 assert(md5 != NULL);
238 assert(in != NULL);
239 assert(md5->curlen <= sizeof(md5->buf));
240
241 while (inlen > 0) {
242 if (md5->curlen == 0 && inlen >= MD5_BLOCKSIZE) {
243 md5_compress(md5, (unsigned char *)in);
244 md5->length += MD5_BLOCKSIZE * 8;
245 in += MD5_BLOCKSIZE;
246 inlen -= MD5_BLOCKSIZE;
247 } else {
Victor Stinner640c35c2013-06-04 23:14:37 +0200248 n = Py_MIN(inlen, (Py_ssize_t)(MD5_BLOCKSIZE - md5->curlen));
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000249 memcpy(md5->buf + md5->curlen, in, (size_t)n);
Victor Stinner56cb1252012-10-31 00:33:57 +0100250 md5->curlen += (MD5_INT32)n;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000251 in += n;
252 inlen -= n;
253 if (md5->curlen == MD5_BLOCKSIZE) {
254 md5_compress(md5, md5->buf);
255 md5->length += 8*MD5_BLOCKSIZE;
256 md5->curlen = 0;
257 }
258 }
259 }
260}
261
262/**
263 Terminate the hash to get the digest
264 @param sha1 The hash state
265 @param out [out] The destination of the hash (16 bytes)
266*/
doko@ubuntu.comc2b46732012-06-21 17:26:06 +0200267static void
268md5_done(struct md5_state *md5, unsigned char *out)
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000269{
270 int i;
271
272 assert(md5 != NULL);
273 assert(out != NULL);
274 assert(md5->curlen < sizeof(md5->buf));
275
276 /* increase the length of the message */
277 md5->length += md5->curlen * 8;
278
279 /* append the '1' bit */
280 md5->buf[md5->curlen++] = (unsigned char)0x80;
281
282 /* if the length is currently above 56 bytes we append zeros
283 * then compress. Then we can fall back to padding zeros and length
284 * encoding like normal.
285 */
286 if (md5->curlen > 56) {
287 while (md5->curlen < 64) {
288 md5->buf[md5->curlen++] = (unsigned char)0;
289 }
290 md5_compress(md5, md5->buf);
291 md5->curlen = 0;
292 }
293
294 /* pad upto 56 bytes of zeroes */
295 while (md5->curlen < 56) {
296 md5->buf[md5->curlen++] = (unsigned char)0;
297 }
298
299 /* store length */
300 STORE64L(md5->length, md5->buf+56);
301 md5_compress(md5, md5->buf);
302
303 /* copy output */
304 for (i = 0; i < 4; i++) {
305 STORE32L(md5->state[i], out+(4*i));
306 }
307}
308
309/* .Source: /cvs/libtom/libtomcrypt/src/hashes/md5.c,v $ */
310/* .Revision: 1.10 $ */
311/* .Date: 2007/05/12 14:25:28 $ */
312
313/*
314 * End of copied MD5 code.
315 *
316 * ------------------------------------------------------------------------
317 */
318
319static PyTypeObject MD5type;
320
321
322static MD5object *
323newMD5object(void)
324{
325 return (MD5object *)PyObject_New(MD5object, &MD5type);
326}
327
328
329/* Internal methods for a hash object */
330
331static void
332MD5_dealloc(PyObject *ptr)
333{
334 PyObject_Del(ptr);
335}
336
337
338/* External methods for a hash object */
339
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200340/*[clinic input]
341MD5Type.copy
342
343Return a copy of the hash object.
344[clinic start generated code]*/
345
346PyDoc_STRVAR(MD5Type_copy__doc__,
347"copy($self, /)\n"
348"--\n"
349"\n"
350"Return a copy of the hash object.");
351
352#define MD5TYPE_COPY_METHODDEF \
353 {"copy", (PyCFunction)MD5Type_copy, METH_NOARGS, MD5Type_copy__doc__},
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000354
355static PyObject *
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200356MD5Type_copy_impl(MD5object *self);
357
358static PyObject *
359MD5Type_copy(MD5object *self, PyObject *Py_UNUSED(ignored))
360{
361 return MD5Type_copy_impl(self);
362}
363
364static PyObject *
365MD5Type_copy_impl(MD5object *self)
366/*[clinic end generated code: output=3b3a88920b3dc7f4 input=2c09e6d2493f3079]*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000367{
368 MD5object *newobj;
369
Christian Heimes90aa7642007-12-19 02:45:37 +0000370 if (Py_TYPE(self) == &MD5type) {
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000371 if ( (newobj = newMD5object())==NULL)
372 return NULL;
373 } else {
374 if ( (newobj = newMD5object())==NULL)
375 return NULL;
376 }
377
378 newobj->hash_state = self->hash_state;
379 return (PyObject *)newobj;
380}
381
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200382/*[clinic input]
383MD5Type.digest
384
385Return the digest value as a string of binary data.
386[clinic start generated code]*/
387
388PyDoc_STRVAR(MD5Type_digest__doc__,
389"digest($self, /)\n"
390"--\n"
391"\n"
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000392"Return the digest value as a string of binary data.");
393
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200394#define MD5TYPE_DIGEST_METHODDEF \
395 {"digest", (PyCFunction)MD5Type_digest, METH_NOARGS, MD5Type_digest__doc__},
396
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000397static PyObject *
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200398MD5Type_digest_impl(MD5object *self);
399
400static PyObject *
401MD5Type_digest(MD5object *self, PyObject *Py_UNUSED(ignored))
402{
403 return MD5Type_digest_impl(self);
404}
405
406static PyObject *
407MD5Type_digest_impl(MD5object *self)
408/*[clinic end generated code: output=7a796b28fa89485f input=7b96e65389412a34]*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000409{
410 unsigned char digest[MD5_DIGESTSIZE];
411 struct md5_state temp;
412
413 temp = self->hash_state;
414 md5_done(&temp, digest);
Christian Heimes72b710a2008-05-26 13:28:38 +0000415 return PyBytes_FromStringAndSize((const char *)digest, MD5_DIGESTSIZE);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000416}
417
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200418/*[clinic input]
419MD5Type.hexdigest
420
421Return the digest value as a string of hexadecimal digits.
422[clinic start generated code]*/
423
424PyDoc_STRVAR(MD5Type_hexdigest__doc__,
425"hexdigest($self, /)\n"
426"--\n"
427"\n"
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000428"Return the digest value as a string of hexadecimal digits.");
429
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200430#define MD5TYPE_HEXDIGEST_METHODDEF \
431 {"hexdigest", (PyCFunction)MD5Type_hexdigest, METH_NOARGS, MD5Type_hexdigest__doc__},
432
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000433static PyObject *
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200434MD5Type_hexdigest_impl(MD5object *self);
435
436static PyObject *
437MD5Type_hexdigest(MD5object *self, PyObject *Py_UNUSED(ignored))
438{
439 return MD5Type_hexdigest_impl(self);
440}
441
442static PyObject *
443MD5Type_hexdigest_impl(MD5object *self)
444/*[clinic end generated code: output=daa73609f94f92e1 input=b60b19de644798dd]*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000445{
446 unsigned char digest[MD5_DIGESTSIZE];
447 struct md5_state temp;
448 PyObject *retval;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200449 Py_UCS1 *hex_digest;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000450 int i, j;
451
452 /* Get the raw (binary) digest value */
453 temp = self->hash_state;
454 md5_done(&temp, digest);
455
456 /* Create a new string */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200457 retval = PyUnicode_New(MD5_DIGESTSIZE * 2, 127);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000458 if (!retval)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200460 hex_digest = PyUnicode_1BYTE_DATA(retval);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000461
462 /* Make hex version of the digest */
463 for(i=j=0; i<MD5_DIGESTSIZE; i++) {
Victor Stinnerf5cff562011-10-14 02:13:11 +0200464 unsigned char c;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000465 c = (digest[i] >> 4) & 0xf;
Victor Stinnerf5cff562011-10-14 02:13:11 +0200466 hex_digest[j++] = Py_hexdigits[c];
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000467 c = (digest[i] & 0xf);
Victor Stinnerf5cff562011-10-14 02:13:11 +0200468 hex_digest[j++] = Py_hexdigits[c];
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000469 }
Christian Heimesf402e922013-01-03 09:21:55 +0100470#ifdef Py_DEBUG
Victor Stinner8f825062012-04-27 13:55:39 +0200471 assert(_PyUnicode_CheckConsistency(retval, 1));
Christian Heimesf402e922013-01-03 09:21:55 +0100472#endif
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000473 return retval;
474}
475
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200476/*[clinic input]
477MD5Type.update
478
479 obj: object
480 /
481
482Update this hash object's state with the provided string.
483[clinic start generated code]*/
484
485PyDoc_STRVAR(MD5Type_update__doc__,
486"update($self, obj, /)\n"
487"--\n"
488"\n"
489"Update this hash object\'s state with the provided string.");
490
491#define MD5TYPE_UPDATE_METHODDEF \
492 {"update", (PyCFunction)MD5Type_update, METH_O, MD5Type_update__doc__},
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000493
494static PyObject *
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200495MD5Type_update(MD5object *self, PyObject *obj)
496/*[clinic end generated code: output=9d09b6c6cdc6cac3 input=6e1efcd9ecf17032]*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000497{
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000498 Py_buffer buf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499
Gregory P. Smith365a1862009-02-12 07:35:29 +0000500 GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
501
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000502 md5_process(&self->hash_state, buf.buf, buf.len);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000503
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000504 PyBuffer_Release(&buf);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000505 Py_INCREF(Py_None);
506 return Py_None;
507}
508
509static PyMethodDef MD5_methods[] = {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200510 MD5TYPE_COPY_METHODDEF
511 MD5TYPE_DIGEST_METHODDEF
512 MD5TYPE_HEXDIGEST_METHODDEF
513 MD5TYPE_UPDATE_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 {NULL, NULL} /* sentinel */
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000515};
516
517static PyObject *
518MD5_get_block_size(PyObject *self, void *closure)
519{
Christian Heimes217cfd12007-12-02 14:31:20 +0000520 return PyLong_FromLong(MD5_BLOCKSIZE);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000521}
522
523static PyObject *
524MD5_get_name(PyObject *self, void *closure)
525{
Christian Heimes37d5ceb2013-08-15 18:31:48 +0200526 return PyUnicode_FromStringAndSize("md5", 3);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000527}
528
529static PyObject *
530md5_get_digest_size(PyObject *self, void *closure)
531{
Christian Heimes217cfd12007-12-02 14:31:20 +0000532 return PyLong_FromLong(MD5_DIGESTSIZE);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000533}
534
535
536static PyGetSetDef MD5_getseters[] = {
537 {"block_size",
538 (getter)MD5_get_block_size, NULL,
539 NULL,
540 NULL},
541 {"name",
542 (getter)MD5_get_name, NULL,
543 NULL,
544 NULL},
545 {"digest_size",
546 (getter)md5_get_digest_size, NULL,
547 NULL,
548 NULL},
549 {NULL} /* Sentinel */
550};
551
552static PyTypeObject MD5type = {
553 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 "_md5.md5", /*tp_name*/
555 sizeof(MD5object), /*tp_size*/
556 0, /*tp_itemsize*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000557 /* methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 MD5_dealloc, /*tp_dealloc*/
559 0, /*tp_print*/
560 0, /*tp_getattr*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000561 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000562 0, /*tp_reserved*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000563 0, /*tp_repr*/
564 0, /*tp_as_number*/
565 0, /*tp_as_sequence*/
566 0, /*tp_as_mapping*/
567 0, /*tp_hash*/
568 0, /*tp_call*/
569 0, /*tp_str*/
570 0, /*tp_getattro*/
571 0, /*tp_setattro*/
572 0, /*tp_as_buffer*/
573 Py_TPFLAGS_DEFAULT, /*tp_flags*/
574 0, /*tp_doc*/
575 0, /*tp_traverse*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 0, /*tp_clear*/
577 0, /*tp_richcompare*/
578 0, /*tp_weaklistoffset*/
579 0, /*tp_iter*/
580 0, /*tp_iternext*/
581 MD5_methods, /* tp_methods */
582 NULL, /* tp_members */
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000583 MD5_getseters, /* tp_getset */
584};
585
586
587/* The single module-level function: new() */
588
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200589/*[clinic input]
590_md5.md5
591
592 string: object(c_default="NULL") = b''
593
594Return a new MD5 hash object; optionally initialized with a string.
595[clinic start generated code]*/
596
597PyDoc_STRVAR(_md5_md5__doc__,
598"md5($module, /, string=b\'\')\n"
599"--\n"
600"\n"
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000601"Return a new MD5 hash object; optionally initialized with a string.");
602
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200603#define _MD5_MD5_METHODDEF \
604 {"md5", (PyCFunction)_md5_md5, METH_VARARGS|METH_KEYWORDS, _md5_md5__doc__},
605
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000606static PyObject *
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200607_md5_md5_impl(PyModuleDef *module, PyObject *string);
608
609static PyObject *
610_md5_md5(PyModuleDef *module, PyObject *args, PyObject *kwargs)
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000611{
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200612 PyObject *return_value = NULL;
613 static char *_keywords[] = {"string", NULL};
614 PyObject *string = NULL;
615
616 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
617 "|O:md5", _keywords,
618 &string))
619 goto exit;
620 return_value = _md5_md5_impl(module, string);
621
622exit:
623 return return_value;
624}
625
626static PyObject *
627_md5_md5_impl(PyModuleDef *module, PyObject *string)
628/*[clinic end generated code: output=1039e912d919880e input=d12ef8f72d684f7b]*/
629{
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000630 MD5object *new;
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000631 Py_buffer buf;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000632
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200633 if (string)
634 GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
Gregory P. Smith365a1862009-02-12 07:35:29 +0000635
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000636 if ((new = newMD5object()) == NULL) {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200637 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000638 PyBuffer_Release(&buf);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000639 return NULL;
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000640 }
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000641
642 md5_init(&new->hash_state);
643
644 if (PyErr_Occurred()) {
645 Py_DECREF(new);
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200646 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000647 PyBuffer_Release(&buf);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000648 return NULL;
649 }
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200650 if (string) {
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000651 md5_process(&new->hash_state, buf.buf, buf.len);
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000652 PyBuffer_Release(&buf);
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000653 }
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000654
655 return (PyObject *)new;
656}
657
658
659/* List of functions exported by this module */
660
661static struct PyMethodDef MD5_functions[] = {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200662 _MD5_MD5_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 {NULL, NULL} /* Sentinel */
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000664};
665
666
667/* Initialize this module. */
668
669#define insint(n,v) { PyModule_AddIntConstant(m,n,v); }
670
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000671
Martin v. Löwis1a214512008-06-11 05:26:20 +0000672static struct PyModuleDef _md5module = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 PyModuleDef_HEAD_INIT,
674 "_md5",
675 NULL,
676 -1,
677 MD5_functions,
678 NULL,
679 NULL,
680 NULL,
681 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000682};
683
684PyMODINIT_FUNC
685PyInit__md5(void)
686{
Christian Heimes327dd732013-10-22 15:05:23 +0200687 PyObject *m;
688
Christian Heimes90aa7642007-12-19 02:45:37 +0000689 Py_TYPE(&MD5type) = &PyType_Type;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000690 if (PyType_Ready(&MD5type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000691 return NULL;
Christian Heimes327dd732013-10-22 15:05:23 +0200692
693 m = PyModule_Create(&_md5module);
694 if (m == NULL)
695 return NULL;
696
697 Py_INCREF((PyObject *)&MD5type);
698 PyModule_AddObject(m, "MD5Type", (PyObject *)&MD5type);
699 return m;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000700}